Tic Tac Toe

Started by
10 comments, last by Rob Loach 19 years, 4 months ago
I would like to know how to make a simple tic tac toe game, i know that it will use arrays and some input but how do i make it work and make it pretty compact on the code side of things?
Lord_BuctreeDead Grass Interactive
Advertisement
The best way to find out is to try [smile]. Open up your compiler, start a new project, and code away. Do some planning in your head before as well. Think about how you'll do it.
Rob Loach [Website] [Projects] [Contact]
here is a little hint

use a vector as the board itself
and each part of the square of the board is a part of that vector

1 | 2 | 3
----------
3 | 4 | 5
----------
6 | 7 | 8

besides that its pretty straigt forward

i am pretty sure it would be better to use more than one function also
Quote:Original post by Rob Loach
The best way to find out is to try [smile]. Open up your compiler, start a new project, and code away. Do some planning in your head before as well. Think about how you'll do it.

Robs right, you should try and plan out how you are going to do it. You won't ever learn if you don't try and do it yourself. Then if you can't do it ask for help.
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="88" height="70" id="H2lvl" align="middle"> <param name="allowScriptAccess" value="sameDomain" /> <param name="movie" value="http://www.ironhive.com/CFCs/H2lvl.swf?gamertag=jBaddog" /> <param name="quality" value="high" /> <param name="wmode" value="transparent"> <embed src="http://www.ironhive.com/CFCs/H2lvl.swf?gamertag=jBaddog" quality="high" bgcolor="#ffffff" width="88" height="70" name="H2lvl" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> </object>
First, make it work.

Then, learn to refactor, and do it to the best of your ability.

With experience, you will learn to apply refactorings as you are working, and even avoid code smells before they are created. Tic-tac-toe is a small enough project that you won't get hopelessly lost by leaving the cleanup to the end. (With proper refactoring, it should probably end up around 100-300 lines depending on how much UI functionality you put in, and whether you try to do an AI.)

Oh, and array indexing starts at 0 - work with this, not against it. Humans like to work with counting that starts at 1 - do your conversion right at the "outside layer" of the program. E.g. if you ask the user to input his move as a number 1-9, convert that to the 0-8 range right away (i.e. subtract 1) and use that value internally. You will find that it makes it MUCH easier to do certain mathematical tricks which, while they might look a bit strange, will allow for a *big* reduction in the overall complexity.
Quote:Original post by baddogj
Quote:Original post by Rob Loach
The best way to find out is to try [smile]. Open up your compiler, start a new project, and code away. Do some planning in your head before as well. Think about how you'll do it.

Robs right, you should try and plan out how you are going to do it. You won't ever learn if you don't try and do it yourself.


While I agree with you two, you also have to keep in mind that this approach is useless if you have no idea what you are doing [wink].

lord_buctree this is the main process of making a tic tac toe game:

Think about if I challegend you to a game, would would you do?
*First you would need to draw a board correct. As masonpwns says, use a vector or matrix to keep the board. Personaly id use a int Board[3][3] as my board.
*Then you would need to see who goes first, player one or player 2. This can be stored with a variable such as an int, 0 means p1 and 1 means p2, or whatever you want
*The next thing is that a player will get to choose a place to make a move.
- If they are player one, they will place and X
- If they are player 2 they will place a O
- But, before they move, you must check to see if the board at that location is already taken or not, right? If it is, they do not get to move, if it is empty, they can move, and the other player gets a turn.
- After every move you will need to see if there is a winner. To do this, you will compare the values of the rows across and cols up and down, as well as diagonals to see if they contain the same value, if they do, then that player wins.
- If the board is full and no winner yet, then it is a draw and you start over.

Now on for some more concepts:

Board - int Board[3][3];

int PlayerTurn = 1; // Player 1

you want to initialize all the values to -1, so you know they are empty (hint: use a nested for loop)

Thank you masonpwns for the artwork ;)

Board[0][0] | Board[0][1] | Board[0][2]
----------------------------------------
Board[1][0] | Board[1][1] | Board[1][2]
----------------------------------------
Board[2][0] | Board[2][1] | Board[2][2]

So when you are checking for winners you will check the rows, cols, and diags of those indexes.

When a player moves, lets say to the top left square, you will be checking to see if Board[0][0] == -1, if it does then you can set it to whosever turn it is, then change the current turn.

When you are drawing the board, you want to loop thorugh the board (nested for loop again) and if it is a -1, you will draw nothing, 1 and X, and 2 a O.

I hope you have a sense of direction now! Goodluck! Since tic tac toe is a simply game, dont worry about it compact on the code side yet. Work on getting it to work at fist, then make it smaller afterwards.

Is this a good start:

#include<iostream>

using namespace std;

int main()
{
int board[3][3]; //This is the array
int en1, en2; //The enter coordinats variables

cout<<"Welcome\n"; //This is the welcome part
cout<<" 1|2|3\n -----\n 4|5|6\n -----\n 7|8|9\n"; //this is the board
cout<<"Please Enter A Coordinate: "; //As it says enter the coordinate
cin>> en1, en2; //Enter the coordinates
Lord_BuctreeDead Grass Interactive
Yes, but you may want to say like:

cout << "Please Enter A Coordinate X <space> Y: ";

so they know to enter in two.

Also your board is: int board[3][3];
you need to initialize all the values to -1 or some value to represent an empty space.

Instead of:

cout<<" 1|2|3\n -----\n 4|5|6\n -----\n 7|8|9\n"; //this is the board

you will need to be outputting the board itself: ie

cout << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << "\n";
...etc

At first you will not be able to see the X and O's, just the -1,s 1 and 2, ut you can change this later.
This is what i have now:

#include <iostream>

using namespace std;

int main();
{
int board[3][3]; //This is the array
int en1, en2; //The enter coordinats variables

cout<<"Welcome\n"; //This is the welcome par
cout<<"Player One You Start";
cout<<"board[0][0]|board [0][1]|board[0][2]\n"; //this is the board
cout<<"board[1][0]|board [1][1]|board[1][2]\n";
cout<<"board[2][0]|board [2][1]|board[2][2]\n";
cout<<"Please Enter The X and Y Coordinates: "; //As it says enter the coordinate
cin>> en1, en2; //Enter the coordinates

Now what do i do?
Lord_BuctreeDead Grass Interactive
Well I can't go every little step, since there are so many, but if you go back to my post, now you need to see if the coordinates they entered are legal. Check to make sure they are > -1 and < 3 then check to see if that board spot is open (-1). If it is open, assign that Board[en1][en2] to the current player's turn. Modify the turn and repeat the process. (Don't forget to check to see if the game is over) (Also you may want to use functions, because the game should go until it is over, right now it will just get input once)

This topic is closed to new replies.

Advertisement