This project implements a Tic-Tac-Toe game with an Monte Carlo Tree Search opponent using the Monte Carlo Tree Search (MCTS) algorithm. The game features a graphical user interface built with Pygame, allowing human players to compete against the Monte Carlo Tree Search.
- Tic-Tac-Toe game implementation
- Monte Carlo Tree Search
- Pygame-based graphical user interface
- Visual representation of Monte Carlo Tree Search's move evaluation
- Python 3.x
- NumPy
- Matplotlib
- Pygame
- PySpiel
- Clone this repository or download the source code.
- Install the required dependencies:
pip install numpy matplotlib pygame open_spiel
To start the game, run the main script:
python main.py
The game will open in a Pygame window. The human player is 'O', and the Monte Carlo Tree Search is 'X'. Enter your move in the terminal. The Monte Carlo Tree Search will automatically make its move after you.
mcts.py
: Implementation of the Monte Carlo Tree Search algorithmgui.py
: Pygame-based graphical user interfacemain.py
: Main game loop and integration of MCTS with GUI
- The game starts with an empty 3x3 grid.
- Players take turns placing their symbol ('O' for human, 'X' for Monte Carlo Tree Search) in empty cells.
- The Monte Carlo Tree Search uses MCTS to evaluate possible moves and choose the best one.
- After each Monte Carlo Tree Search move, a heatmap is displayed showing the Monte Carlo Tree Search's evaluation of different positions.
- The game ends when a player wins or the board is full (draw).
The Monte Carlo Tree Search uses MCTS to determine its moves:
- Selection: Choose a promising leaf node in the game tree.
- Expansion: Add child nodes to the selected node.
- Simulation: Play out random games from the new nodes.
- Backpropagation: Update node statistics based on simulation results.
The Monte Carlo Tree Search repeats this process for a set number of iterations before choosing the best move.
After each Monte Carlo Tree Search move, the program displays a heatmap showing the Monte Carlo Tree Search's evaluation of different board positions. Warmer colors (red) indicate moves the Monte Carlo Tree Search considers more favorable, while cooler colors (blue) represent less favorable moves.
For educational purposes and tinkering.
This project is open-source and available under the MIT License.