I am working on the tic tac toe program. It was due days ago but I didn't turn it in because it was incomplete (I was having trouble with the function). In the function I had to return whether there was a tie, a winner or to let the users keep playing if the board wasn't full yet. I managed to do most of the function but I am still having trouble when the function checks if there was a tie. I noticed the game ends in a tie if one of the diagonal lines of the board is full. I spent all day trying to figure out what I was doing wrong but I still can't find the solution.
Any help or clues I receive to fix this will be deeply appreciated.
// Checks the whole board if there is a winner.
// We know the board is 3 by 3 so we don't need to have the number of rows as
// a parameter.
//
// WRITE THIS FUNCTION
//
char checkWinner3by3(char board[][3])
{
//Check all rows
for (int row = 0; row < 3; row++)
{
if ((board[row][0] == board[row][1]) && (board[row][0] == board[row][2]))
{
return board[row][0];
}
}
// Check all columns
for (int col = 0; col < 3; col++)
{
if ((board[0][col] == board[1][col]) && (board[0][col] == board[2][col]))
{
return board[0][col];
}
}
// Check diagional from upper left to lower right
if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]))
{
return board[0][0];
}
// Check diagonal from upper right to lower left
if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]))
{
return board[0][2];
}
// Otherwise, if all spaces are filled, the game is a tie
//THIS IS WHERE I AM HAVING TROUBLE
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (!(board[row][col] == 'X') || !(board[row][col] == 'O'))
{
return'T';
}
else
{
break;
}
}
}
// If none of the conditions above are true, return a blank space
return' ';
}
Looking at your code fro line 236 on I can see some problems.
The if statement at line 241 could be a problem. When using two or more "!" in the if statement usually "&&" is used instead of the "||" you have.
The if/else will only let you check board[0][0] or board[1][0] or board[2][0] not the entire array. Or you return from the loops to early.
At this point you have already checked for a winner and found this to be false. So, now my thought is that you need to check the array/board for a potential winner and if this would be false then return "T" otherwise return " ".
I am also thinking that say if "X" starts then this section should only be done after "O"s' turn.
In the end you will need to check the entire board for a potential win unless you find the entire board filled and no moves left before you call it a tie.
I will have to load up the program and play with it later. It looks like fun for the day. Should have somethinf later unless someone else does.
// Otherwise, if all spaces are filled, the game is a tie
//THIS IS WHERE I AM HAVING TROUBLE
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (((board[row][col] == 'X') || (board[row][col] == 'O')) && board[row][col] != ' ')
{
tie = true; // <--- "tie" defined as a bool initialized to "false".
}
else
{
tie = false;
}
}
}
std::cout << tie << std::endl;
// If none of the conditions above are true, return a blank space
return (tie)?'T':' ';
}