This project implements a Tic-Tac-Toe game with an AI opponent using the Minimax algorithm. The graphical interface is built using Pygame, allowing a user to play against a computer that makes optimal decisions.
The AI evaluates possible future board states and selects moves that maximize its chances of winning while minimizing the opponent’s chances.
-
Play Tic-Tac-Toe against an AI opponent
-
Choose to play as X or O
-
AI uses the Minimax algorithm for decision making
-
Detects:
- Wins
- Losses
- Draws
-
Interactive graphical interface using Pygame
-
Restart option after game completion
project/
│
├── runner.py # Main game interface using Pygame
├── tictactoe.py # Game logic and Minimax AI implementation
├── OpenSans-Regular.ttf
└── README.md
Handles:
- Game window
- Player input
- Rendering board and moves
- AI turn management
Contains the core game logic:
- Board representation
- Determining valid moves
- Checking winners
- Minimax algorithm for AI decision making
Install the required library:
pip install pygame
Python version:
Python 3.8+
-
Navigate to the project directory.
-
Run the main game file:
python runner.py
-
Choose whether to play as X or O.
-
Click on the grid to place your move.
- The game is played on a 3 × 3 board.
- Players take turns placing X and O.
- The first player to align three marks in a row, column, or diagonal wins.
- If all spaces are filled and no player wins, the game ends in a draw.
The AI uses the Minimax algorithm, a decision-making algorithm used in game theory.
-
Generate all possible moves from the current board state.
-
Simulate each move recursively.
-
Assign scores to terminal states:
- X win → +1
- O win → −1
- Draw → 0
-
Choose the move that maximizes the AI's chance of winning.
The algorithm ensures the AI always selects the optimal move.
- Player selects X.
- Player clicks a square.
- AI evaluates all possible board states using Minimax.
- AI makes the best move automatically.
This project demonstrates several Artificial Intelligence and programming concepts:
- Minimax algorithm
- Game state evaluation
- Recursive search
- Adversarial search
- Python object-oriented programming
- GUI development using Pygame
Future enhancements may include:
- Alpha-Beta pruning for faster search
- Improved UI design
- Difficulty levels
- Move animations
- Score tracking
Developed as part of an Artificial Intelligence / Game AI programming exercise.