A Connect 4 game featuring two AI opponents with different strategies and difficulty levels
About • Features • AI Algorithms • Installation • Usage • Project Structure
This project implements the classic Connect 4 game in Python with Pygame, featuring two AI opponents of varying difficulty. The game showcases fundamental game theory concepts including the Minimax algorithm with position evaluation heuristics.
Optimal Play = Minimax(game_tree, depth) + Evaluate(board_position) + Heuristics(threats)
The Hard AI uses a depth-limited Minimax search combined with strategic position evaluation to find optimal moves, while the Easy AI provides a more casual experience with randomized play.
| Feature | Description |
|---|---|
| Two AI Modes | Easy (random) and Hard (Minimax) opponents |
| Visual Effects | Gradient backgrounds, 3D token shading |
| Sound System | Audio feedback for moves, wins, and menu |
| Responsive UI | Clean menus with keyboard controls |
| Key | Action |
|---|---|
1 |
Select Easy AI / Play again |
2 |
Select Hard AI / Play again |
ESC |
Quit game |
Mouse |
Drop token in column |
The Easy AI implements a reactive strategy with three priority levels:
def get_move(board, player):
1. If winning move exists → Play it
2. If opponent can win next → Block it
3. Otherwise → Random columnCharacteristics:
- Predictable but not trivial
- Good for beginners
- Fast computation
The Hard AI uses a sophisticated Minimax search with evaluation heuristics:
def minimax(node, depth, maximizing_player):
if depth == 0 or terminal_state:
return evaluate(position)
if maximizing:
return max(minimax(child) for child in children)
else:
return min(minimax(child) for child in children)Evaluation Function Components:
| Component | Weight | Description |
|---|---|---|
| Win Detection | ±1,000,000 | Terminal state detection |
| Threat Analysis | 5-10x | Consecutive tokens in all directions |
| Position Value | 80x | Center positions score higher |
Position Value Matrix:
[ 3, 4, 5, 7, 5, 4, 3]
[ 4, 6, 8, 10, 8, 6, 4]
[ 5, 8, 11, 13, 11, 8, 5]
[ 5, 8, 11, 13, 11, 8, 5]
[ 4, 6, 8, 10, 8, 6, 4]
[ 3, 4, 5, 7, 5, 4, 3]
Characteristics:
- Search depth: 4 moves ahead
- Opening optimization: Always plays center
- Very challenging to beat
- Python 3.7+
- NumPy
- Pygame
# Clone the repository
git clone https://github.com/mathisdelsart/Connect4Zero.git
cd Connect4Zero
# Install dependencies
pip install pygame numpy
# Run the game
python main.py- Launch with
python main.py - Select difficulty:
1for Easy,2for Hard - Click columns to drop your red tokens
- Connect 4 horizontally, vertically, or diagonally to win
Compare AI performance with statistical analysis:
python montecarlo.pyRuns tournaments between Hard AI and Easy AI, generating win rate statistics and visualization graphs.
Connect4_AI/
├── main.py # Entry point
├── montecarlo.py # AI performance benchmarking
├── src/
│ ├── configs.py # Game settings, colors, fonts, sounds
│ ├── functions.py # Core logic: board ops, win detection, minimax
│ ├── game.py # Game loop, rendering, event handling
│ └── players/
│ ├── player.py # Human player (console mode)
│ ├── easy_ai.py # Easy AI - random with basic strategy
│ └── hard_ai.py # Hard AI - minimax algorithm
└── assets/
├── sounds/ # Audio files (drop, win, lose, select)
└── *.png # Game images
- Players alternate turns dropping tokens into columns
- Tokens fall to the lowest available row
- First to connect 4 tokens in a row wins
- Connections: horizontal, vertical, or diagonal
- Draw if all 42 cells are filled

