well the best way to think of this is that you are going to need some loops. A loop which checks a row inside a loop which loops through each row. Same with columns. Then you just need to check diagonals.
Each of these winning checks should be in its own function. Make three functions called "checkRows()" which will be used to check each row for three of a kind. Another called "checkColumns()" which will then check each column. Last "checkDiagonals()" to check the diagonals.
Here is how you might check your rows...
cpp
// Returns true if a win, false if not. playerChar is either 'X' or 'O'
bool checkRows(char grid[][3], char playerChar) {
// Loop through each row
for (int i = 0; i < 3; i++) {
// Count will keep track player chars in row
int count = 0;
// Loop through each column checking if char
// If it is, increment count
for (int j = 0; j < 3; j++) {
if (grid[i][j] == playerChar) { count++; }
}
// If we have 3, it means they have the row so they win.
if (count == 3) { return true; }
}
// Nope, no win for rows
return false;
}
So that will give you the general idea of how you can check for horizontal row wins for a player. You would pass it the grid after each player's turn and that player's char and it will check rows. So for checking for a win you will call all three checks: horizontal, vertical and diagonals.
This should get you started. Enjoy!
"At DIC we be tic tac toe winning code ninjas... in our version of war games, joshua always wins because we coded it!"