Welcome to the Tic Tac Toe Game! This is a console-based implementation of the classic Tic Tac Toe game in C++. The game can be played in either single-player mode against the computer or in Two-player mode.
- Two game modes: Single-player and Two-player.
- Single-player mode with two difficulty levels: Easy and Hard.
- Visual representation of the Tic Tac Toe board.
- Input validation for row and column entries.
- Reset the board for a new game after each round.
- A C++ compiler (e.g.,
g++).
To compile the code, use the following command:
g++ -o tictactoe tictactoe.cppAfter compiling, run the game with the following command:
./tictactoeWhen you start the game, you will be prompted to choose between single-player and two-player mode:
- Enter
1for single-player mode. - Enter
2for two-player mode.
In single-player mode, you will play against the computer. You will be asked to enter your name and choose the difficulty level:
- Enter
1for Easy mode. - Enter
2for Hard mode.
In two-player mode, you and another player will take turns. You will be asked to enter the names for Player X and Player O.
During the game, you will be prompted to enter the row and column numbers (1-3) to place your mark (X or O) on the board.
The game will check for a win or a draw after each move. The game ends when one player wins or the board is full (draw).
After each game, you will be asked if you want to play again. Enter y for yes or n for no.
- display(): Displays the current state of the board.
- isCellEmpty(int row, int col): Checks if a cell is empty.
- setCell(int row, int col, Cell cell): Sets a cell with X or O.
- isFull(): Checks if the board is full.
- checkWin(Cell player): Checks if a player has won.
- reset(): Resets the board to its initial state.
- start()_: Starts the Tic Tac Toe game.
- findBestMove(): Uses the Minimax algorithm to find the best move for the computer in Hard mode.
- minimax(int depth, bool isMax): Minimax algorithm to evaluate the best move.
Initializes and starts the Tic Tac Toe game. Example
Welcome to Tic Tac Toe!
Do you want to play single or duo mode? (1 for single, 2 for duo): 1
Enter name for Player X: Alice
Select difficulty level: (1 for Easy, 2 for Hard): 2
Alice's turn. Enter row and column (1-3): 1 1
Computer chose: 2, 2The minimax function is used to determine the best move for the computer in hard mode. It recursively evaluates all possible moves and their outcomes, considering the maximum possible score for the computer and the minimum possible score for the player