// Check three values to see if they are the same. If so, we have a winner. bool tic_tac_toe_row(char chOne, char chTwo, char chThree) { if ((chOne == chTwo) && (chOne == chThree)) { return true; } return false; } // Check board for a win by looping through rows, columns and checking diagonals. // If any of them are true, then there is a winning condition. bool tic_tac_toe_win(char grid[][3]) { // Loop through the rows for (int i = 0; i < 3; i++) { if (tic_tac_toe_row(grid[i][0], grid[i][1], grid[i][2])) { return true; } } // Loop through the columns for (int i = 0; i < 3; i++) { if (tic_tac_toe_row(grid[0][i], grid[1][i], grid[2][i])) { return true; } } // Check diagonals if (tic_tac_toe_row(grid[0][0], grid[1][1], grid[2][2])) { return true; } if (tic_tac_toe_row(grid[0][2], grid[1][1], grid[2][0])) { return true; } return false; }
Posted: March 20, 2023
Return to the snippets listing