1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
#include <iostream> using namespace std; // Function to initialize the board void initializeBoard(char board[3][3]) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i][j] = ' '; } } } // Function to display the board void displayBoard(char board[3][3]) { cout << " 1 2 3" << endl; for (int i = 0; i < 3; i++) { cout << i + 1 << " "; for (int j = 0; j < 3; j++) { cout << board[i][j]; if (j < 2) cout << "|"; } cout << endl; if (i < 2) cout << " -+-+-" << endl; } } // Function to check for a win bool checkWin(char board[3][3], char player) { // Check rows and columns for (int i = 0; i < 3; i++) { if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) || (board[0][i] == player && board[1][i] == player && board[2][i] == player)) { return true; } } // Check diagonals if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) || (board[0][2] == player && board[1][1] == player && board[2][0] == player)) { return true; } return false; } // Function to check for a tie bool checkTie(char board[3][3]) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == ' ') return false; } } return true; } // Function to handle player input bool makeMove(char board[3][3], char player) { int row, col; cout << "Player " << player << ", enter your move (row and column): "; cin >> row >> col; if (row < 1 || row > 3 || col < 1 || col > 3 || board[row-1][col-1] != ' ') { cout << "Invalid move. Try again." << endl; return false; } board[row-1][col-1] = player; return true; } int main() { char board[3][3]; char currentPlayer = 'X'; bool gameWon = false; initializeBoard(board); while (true) { displayBoard(board); if (makeMove(board, currentPlayer)) { if (checkWin(board, currentPlayer)) { displayBoard(board); cout << "Player " << currentPlayer << " wins!" << endl; gameWon = true; break; } else if (checkTie(board)) { displayBoard(board); cout << "It's a tie!" << endl; break; } currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; // Switch player } } return 0; } |
Explanation
- Board Initialization:
initializeBoard()
: This function sets up the Tic-Tac-Toe board by filling it with empty spaces (' '
).
- Displaying the Board:
displayBoard()
: This function displays the current state of the board in a 3×3 grid format. It adds separators to visualize the rows and columns.
- Checking for a Win:
checkWin()
: This function checks if the current player has won by getting three of their symbols ('X'
or'O'
) in a row, column, or diagonal.
- Checking for a Tie:
checkTie()
: This function checks if the board is full without any player winning, resulting in a tie.
- Handling Player Input:
makeMove()
: This function takes the player’s input (row and column) to place their symbol on the board. It validates the input to ensure the move is within bounds and that the chosen cell is empty.
- Main Game Loop:
- The
main()
function handles the game loop. It alternates between players'X'
and'O'
, allowing them to make moves until either a player wins or the game ends in a tie. - After each move, it checks if the game has been won or tied and displays the final outcome.
- The