Chess AI
A desktop chess game where you play against an engine I wrote in Python. It searches with minimax and alpha-beta pruning under a time limit, and scores positions with material, piece square tables and a few extra heuristics. Below, a browser port plays an opening in a loop, compares both search modes, and lets you take over as White.
- Role
- Solo developer
- Year
- 2022
- Stack
- Python, python-chess, Pygame
- Search
- Minimax + alpha-beta
A TypeScript port of the evaluation and search from minimax.py, running in your browser. The original is a Pygame desktop app that uses python-chess for the rules; here the move generator is written from scratch for the port and checked against standard perft counts.
01
Learning adversarial search by building it
I wanted to understand adversarial search by building it, not just reading about it. python-chess handles the rules and legal moves, Pygame draws the board, and the decision making is mine.
02
Look ahead, assume the best reply
The engine looks a fixed number of moves ahead and assumes the opponent always plays its best reply. Alpha-beta pruning skips branches that cannot change the final decision, so the same depth costs fewer evaluations.
The search also watches the clock. When the time limit runs out, it returns the best move found so far instead of freezing the game. The readout next to the board shows nodes visited and branches pruned for every move.
Porting it to TypeScript, I noticed the minimizing branch never updates beta, so the original code almost never prunes. The lab alternates between the original and the beta update: a completed search picks the same move with fewer nodes. The engine plays on its own, and the controls let you take over as White or compare the two modes.
minimax.py
Alpha-beta, one branch at a time
- alpha
- 3
- beta
- ·
- Nodes visited
- 10/13
- Leaves pruned
- 3/9
- Best move
- e4
Illustrative tree and scores. Same order and pruning rule as the engine with the beta update.
Pure Python is slow, so the design is about spending a small search budget well.
03
What a position is worth
At the leaves the engine adds up material plus a per square bonus for each piece, 10 for giving check, infinity for checkmate, and a small reward for each legal move that lands on a central square.
evaluate
What the engine adds up at a leaf
Base piece values
- 10
- 30
- 30
- 50
- 90
The king is worth 9000, so losing it outweighs everything else.
Good squares bonus
Center moves bonus: +2
Bonus per legal move landing on each square: 1 for the four center squares, 0.5 for the ring around them.
Example: a lone knight on f3 has eight legal moves; two of them land on center squares.
Trade-offs
- 01
Shallow search with a time budget
I kept the depth low and added a time limit so the game stays responsive. The README documents depth 2 with a 10 second limit; the current code searches at depth 3.
- 02
Hand written heuristics
Simple tables and rules are easy to read and tune, at the cost of strength compared to modern engines.
- 03
A library for the rules
python-chess takes care of legal moves, check and mate, so the project could focus on search and evaluation.
See it play
There is a demo video and the full source on GitHub. The board above plays on its own. The controls let you take over as White, compare the search modes, and resume autoplay.