Reputation: 304
I've written (with a bit of help) a tic tac toe program. However the program does not utilise arrays to hold the moves but uses char variables. This means that there is a lot of repetitive code. I would like to use arrays but although I understand its concept, I am having difficulty in figuring out how to implement it. What type of array do I use? And I am also assuming that the bulk of the code (checking for valid moves, checking for wins, etc...) will change.
#include <iostream>
using namespace std;
int main()
{
char cell1 = '1';
char cell2 = '2';
char cell3 = '3';
char cell4 = '4';
char cell5 = '5';
char cell6 = '6';
char cell7 = '7';
char cell8 = '8';
char cell9 = '9';
char player = '1';
char mark;
bool gameover = false;
do
{
cout << cell1 <<"|"<< cell2 << "|" << cell3 << endl;
cout <<"-+-+-"<< endl;
cout << cell4 <<"|"<< cell5 << "|" << cell6 << endl;
cout <<"-+-+-"<< endl;
cout << cell7 <<"|"<< cell8 << "|" << cell9 << endl;
cout <<"-+-+-"<< endl;
//printBoard();
//Handle player marker
if (player == '1')
{
mark = 'X';
}
else
{
mark = 'O';
}
bool invalid;
do
{
cout << "Player "<<player<< ": \n";
char move;
cin >> move;
invalid = false;
//Validate move
if (move == '1' && cell1 == '1')
{
cell1 = mark;
}
else if (move == '2' && cell2 == '2')
{
cell2 = mark;
}
else if (move == '3' && cell3 == '3')
{
cell3 = mark;
}
else if (move == '4' && cell4 == '4')
{
cell4 = mark;
}
else if (move == '5' && cell5 == '5')
{
cell5 = mark;
}
else if (move == '6' && cell6 == '6')
{
cell6 = mark;
}
else if (move == '7' && cell7 == '7')
{
cell7 = mark;
}
else if (move == '8' && cell8 == '8')
{
cell8 = mark;
}
else if (move == '9' && cell9 == '9')
{
cell9 = mark;
}
else
{
cout <<" Invalid Move"<<endl;;
invalid = true;
}
}
while (invalid);
//Check for winning & draw conditions
bool draw = false;
if (cell1 != '1')
{
if (cell1 == cell2 && cell1 == cell3)
{
gameover = true;
}
else if (cell1 == cell4 && cell1 == cell7)
{
gameover = true;
}
}
if (cell9 != '9')
{
if (cell9 == cell3 && cell9 == cell6)
{
gameover = true;
}
else if (cell9 == cell8 && cell9 == cell7)
{
gameover = true;
}
}
if (cell5 != '5')
{
if (cell5 == cell2 && cell5 == cell8)
{
gameover = true;
}
else if (cell5 == cell4 && cell5 == cell6)
{
gameover = true;
}
else if (cell5 == cell1 && cell5 == cell9)
{
gameover = true;
}
else if (cell5 == cell3 && cell5 == cell7)
{
gameover = true;
}
}
// Check for draw conditions
if ( cell1 != '1' && cell2 != '2' && cell3 != '3' &&
cell4 != '4' && cell5 != '5' && cell6 != '6' &&
cell7 != '7' && cell8 != '8' && cell9 != '9')
{
cout <<" Its a draw"<<endl;
gameover = true;
draw = true;
}
if (gameover)
{
if (draw == false)
{
cout << "Player "<< player << " wins!"<<endl;
cout <<endl;
}
//printBoard();
cout << cell1 <<"|"<< cell2 << "|" << cell3 << endl;
cout <<"-+-+-"<< endl;
cout << cell4 <<"|"<< cell5 << "|" << cell6 << endl;
cout <<"-+-+-"<< endl;
cout << cell7 <<"|"<< cell8 << "|" << cell9 << endl;
cout <<"-+-+-"<< endl;
cout <<endl;
cout<< "Play again? (y/n)"<< endl;
char playAgain;
cin >> playAgain;
//Reset board if user wants to play again
if (playAgain == 'y')
{
gameover = false;
cell1 = '1';
cell2 = '2';
cell3 = '3';
cell4 = '4';
cell5 = '5';
cell6 = '6';
cell7 = '7';
cell8 = '8';
cell9 = '9';
player = '1';
}
}
else
{
//Alternate players
if (player == '1')
{
player = '2';
}
else
{
player = '1';
}
}
}
while (gameover == false);
return 0;
}
Upvotes: 0
Views: 7502
Reputation: 2761
What type of array do I use?
You'll want to use a two dimensional array (3 x 3). So instead of having cell1, cell2, ... cell9, you'll have a single (two-dimensional) array like so:
cell[3][3];
The first set of brackets represents the rows (horizontal) and the second set of brackets represents the columns (vertical).
Here is how the 2d array would replace your 9 variables:
cell1 --> cell[0][0]
cell2 --> cell[0][1]
cell3 --> cell[0][2]
cell4 --> cell[1][0]
cell5 --> cell[1][1]
cell6 --> cell[1][2]
cell7 --> cell[2][0]
cell8 --> cell[2][1]
cell9 --> cell[2][2]
And I am also assuming that the bulk of the code (checking for valid moves, checking for wins, etc...) will change.
Then, as long as you change all the cell# variables to the corresponding 2d array slot your program should work.
Upvotes: 6
Reputation: 2590
Tic-Tac-Toe consists of a 3x3 playing space. You could use a 2D array to represent it like so:
char board[ 3 ][ 3 ];
Now to set the top-left square to a value of 1 you would do something like this:
board[ 0 ][ 0 ] = 1;
To set the top-right square to a value of 5 you would do this:
board[ 2 ][ 0 ] = 5;
Likewise if you wanted to say, access the value of the bottom middle, you could do this:
char bottomMiddle = board[ 1 ][ 2 ];
Can you see the general gist here? The first set of square brackets is like your x coordinate on the board, and the second set your y coordinate on the board (with 0 being the first instead of 1, as you might think intuitively)
A great thing about arrays is that they can be easily looped through to reduce repetitive code in many places.
For instance, say you want to set every square to 0, instead of this:
cell1 = 0;
cell2 = 0;
cell3 = 0;
...
...you could do this:
for ( unsigned i = 0; i < 3; ++i )
for ( unsigned ii = 0; ii < 3; ++ii )
board[ i ][ ii ] = 0;
This can be extremely powerful, especially when you are dealing with very large arrays.
Upvotes: 1