A fully functional Connect Four game implementation in Python with AI opponent using Minimax algorithm with alpha-beta pruning.
- Complete Connect Four gameplay with standard 6x7 board
- AI opponent using Minimax algorithm with alpha-beta pruning
- Heuristic evaluation for intelligent AI moves
- Human vs AI or Human vs Human modes
- Comprehensive test suite for board logic and AI behavior
- Command-line interface for easy gameplay
Connect_Four/
├── board.py # Board class with game logic
├── ai.py # MinimaxAI implementation
├── cli.py # Command-line interface
├── verify.py # Verification script
├── run_tests.py # Test runner
├── tests/
│ ├── __init__.py
│ ├── test_board.py # Board tests
│ └── test_ai.py # AI tests
└── README.md # This file
Cellenum: Represents cell states (EMPTY, P1, P2)Boardclass: Game board with methods for:- Dropping pieces
- Checking legal moves
- Detecting winners (rows, columns, diagonals)
- Board display and copying
MinimaxAIclass: AI player using:- Minimax algorithm with alpha-beta pruning
- Configurable search depth (default: 4)
- Heuristic evaluation function
- Strategic position scoring
- Game loop for playing Connect Four
- Human input handling
- AI move execution
- Game state display
python cli.py- Human vs AI (default): Set
vs_ai=Trueincli.py - Human vs Human: Set
vs_ai=Falseincli.py
- The board displays with column numbers (0-6)
- Player 1 (X) goes first
- Enter column number to drop your piece
- AI (O) responds automatically
- First to connect 4 wins!
0 1 2 3 4 5 6
-------------
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
X . . . . . .
X O . O . . .
.= Empty cellX= Player 1O= Player 2 (AI)
python verify.pyThis runs automated tests covering:
- Board creation
- Piece dropping
- AI decision making
- Winner detection
- AI blocking strategy
python run_tests.pyOr run individual test files:
python tests/test_board.py
python tests/test_ai.pyThe AI uses Minimax with Alpha-Beta Pruning:
- Searches game tree to configurable depth
- Evaluates positions using heuristics:
- Center column preference (+3 points per piece)
- Window evaluation (2, 3, 4 in a row)
- Blocking opponent threats
- Immediate win/loss detection
The game checks for 4 consecutive pieces in:
- Horizontal rows
- Vertical columns
- Diagonal lines (both directions)
- Python 3.6+
- No external dependencies required