Reputation: 57
I am creating a game of tic tac toe as a project with a 2dimensional array and I am having some trouble checking the spots to see if there is a winner. Can someone help me debug this? Here is the checkwinner function and the main function.
char CheckWinner( char board[3][3])
{
int i = 0;
int j = 0;
if ( board[i][j] =='X' && board[i][j+1] == 'X' && board[i][j+2]== 'X' )
{ W = X;}
else if (board[i+1][j] && board[i+1][i+1] && board[i+1][j+2]== 'X' )
{ W = X;}
else if (board[i+2][j] && board[i+2][j+1] && board[i+2][j+2]== 'X')
{ W = X;}
else if (board[i][j] && board[i+1][j] && board[i+2][j]== 'X')
{ W = X;}
else if (board[i][j+1] && board[i+1][j+1] && board[i+2][j+1]== 'X')
{ W = X;}
else if (board[i][j+2] && board[i+1][j+2] && board[i+2][j+2]== 'X')
{ W =X;}
else if (board[i][j] && board[i+1][j+1] && board[i+2][j+2]=='X')
{ W = X;}
else if (board[i+2][j] && board[i+1][j+1] && board[i][j+2]== 'X')
{ W = X;}
else if (board[i][j] && board[i][j+1] && board[i][j+2]== 'O' )
{ W = O;}
else if (board[i+1][j] && board[i+1][j+1] && board[i+1][j+2]== 'O')
{ W = O;}
else if (board[i+2][j] && board[i+2][j+1] && board[i+2][j+2]== 'O')
{ W = O;}
else if (board[i][j] && board[i+1][j] && board[i+2][j]== 'O')
{ W = O;}
else if (board[i][j+1] && board[i+1][j+1] && board[i+2][j+1]== 'O')
{ W = O;}
else if (board[i][j+2] && board[i+1][j+2] && board[i+2][j+2]== 'O')
{ W = O;}
else if (board[i][j] && board[i+1][j+1] && board[i+2][j+2]== 'O')
{ W = O;}
else if (board[i+2][j] && board[i+1][j+1] && board[i][j+2]== 'O')
{ W = O;}
return W;
}
int main ()
{
char board[3][3];
char Win = CheckWinner(board);
int r = 0;
InitializeBoard(board);
for (int r = 0; r < 4 ; r++)
{
PlayX(board);
PlayO(board);
PrintBoard(board);
}
CheckWinner(board);
cout << Win ;
if (Win == X)
{
cout << "The winner is Player 1.";
}
else if (Win == O)
{
cout << "The winner is Player 2.";
}
else if (Win == TIE)
{
cout << " IT'S A TIE";
}
else;
system("PAUSE");
return 0;
Upvotes: 0
Views: 9230
Reputation: 76240
You can optimize your conditional code with the following:
typedef char piece;
piece iswin() const
{
piece ret = 'T';
// Checks for horizontal win
for (int i = 0; i < 3; ++i)
if (*arr[i] == arr[i][1] && arr[i][1] == arr[i][2])
if ((ret = *arr[i]) != 'T')
return ret;
// Checks for vertical win
for (int i = 0; i < 3; ++i)
if (arr[0][i] == arr[1][i] && arr[1][i] == arr[2][i])
if ((ret = arr[0][i]) != 'T')
return ret;
// Check for diagonal win (upper left to bottom right)
if (**arr == arr[1][1] && arr[1][1] == arr[2][2])
if ((ret = **arr) != 'T')
return ret;
// Check for diagonal win (upper right to bottom left)
if (arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0])
if ((ret = arr[0][2]) != 'T')
return ret;
return ret;
}
// checking the result:
switch (winner) {
case 'T': /* tie */ break;
case 'X': /* X won */ break;
case 'O': /* O won */ break;
}
This is little bit cleaner. And it does 6 loops instead of 9.
Upvotes: 3
Reputation: 220904
You immediately assign the CheckWinner
result to Win
, then play the game out, then call CheckWinner
again without assigning its result to Win
. Thus when you check Win
in the following line, you have the original result from when the board wasn't even initialized.
Upvotes: 1
Reputation: 5279
Probably has to do with assigning a literal O or X into a return value that is supposed to be a char. Thus instead of O or X it should be 'O' or 'X'. Unless O or X is defined as a char variable outside the posted code.
Upvotes: 0