Welcome to the Tic-Tac-Toe game implemented in C++ with Minimax algorithm and Alpha-Beta Pruning! This console-based game allows you to play against an AI opponent that uses the Minimax algorithm with Alpha-Beta Pruning to make optimal moves.
This Tic-Tac-Toe game is implemented in C++ and features a simple console interface. The AI opponent is powered by the Minimax algorithm, a decision-making algorithm widely used in two-player games. The implementation also includes Alpha-Beta Pruning to optimize the search space and improve performance.
Before running the game, ensure that you have a C++ compiler installed on your system.
-
Clone the repository to your local machine:
git clone https://github.com/MohammadrezaAmani/TicTocToe
-
Navigate to the project directory:
cd TicTocToe
-
Compile the code using your C++ compiler:
g++ -o tic_tac_toe main.cpp
-
Run the executable:
./tic_tac_toe
- Enter your move by specifying the row and column (1-3) when prompted.
- The AI opponent uses the Minimax algorithm to make optimal moves.
- Watch out for strategic plays, as the AI aims to win or force a tie.
- The
printBoard
function displays the current state of the Tic-Tac-Toe board.
- The
checkWin
function checks if a player has won the game.
- The
makeMove
function allows players to make a move on the board.
- The
minimax
function implements the Minimax algorithm for decision-making.
- The Minimax algorithm is enhanced with Alpha-Beta Pruning for improved efficiency.
Here's an example of how of using game:
int main()
{
TicTacToe game;
int currentPlayer = 1;
while (true)
{
game.printBoard();
if (currentPlayer == 1)
{
std::cout << "Player 1, enter your move (row and column): ";
int row, col;
std::cin >> row >> col;
if (game.makeMove(row - 1, col - 1, currentPlayer))
{
if (game.checkWin(currentPlayer))
{
game.printBoard();
std::cout << "Player 1 wins!" << std::endl;
break;
}
else if (game.isBoardFull())
{
game.printBoard();
std::cout << "It's a tie!" << std::endl;
break;
}
}
else
{
std::cout << "Invalid move. Try again." << std::endl;
continue;
}
}
else
{
game.makeAIMove();
if (game.checkWin(2))
{
game.printBoard();
std::cout << "AI wins!" << std::endl;
break;
}
else if (game.isBoardFull())
{
game.printBoard();
std::cout << "It's a tie!" << std::endl;
break;
}
}
currentPlayer = (currentPlayer == 1) ? 2 : 1;
}
return 0;
}
Follow the on-screen instructions to make your moves and enjoy the game!
Contributions are welcome! If you have ideas for improvements or find any issues, please open an issue or submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.