A pure Python chess engine with UCI protocol support.
🇷🇺 Русская версия (Russian version)
- ✅ All piece movements (pawn, knight, bishop, rook, queen, king)
- ✅ Castling (kingside O-O and queenside O-O-O)
- ✅ En passant capture
- ✅ Pawn promotion (to queen, rook, bishop, knight)
- ✅ Check, checkmate, stalemate
- ✅ 50-move rule
- ✅ Threefold repetition rule
- ✅ Insufficient material for checkmate
- ✅
uci- engine identification - ✅
isready/readyok- readiness check - ✅
ucinewgame- new game - ✅
position startpos [moves ...]- starting position - ✅
position fen <fen> [moves ...]- position from FEN - ✅
go depth <n>- search to depth n - ✅
stop- stop search - ✅
quit- exit
- ✅ Minimax with Alpha-Beta pruning
- ✅ Iterative Deepening
- ✅ Quiescence search
- ✅ Move ordering (MVV-LVA)
- ✅ Piece-square tables for position evaluation
- ✅ Transposition Table (Zobrist hashing, 64MB cache)
- ✅ TT move ordering (best move from cache first)
- ✅ Null Move Pruning (skipping a move for cutoff)
- ✅ Late Move Reductions (reduced search depth for late moves)
- ✅ Killer Move Heuristic (remembering moves that caused beta-cutoffs)
- ✅ History Heuristic (statistics of successful moves)
- ✅ Principal Variation Search (PVS)
- ✅ Aspiration Windows (narrowing the alpha-beta window)
- ✅ Static Exchange Evaluation (SEE for capture evaluation)
- ✅ Futility Pruning (pruning unpromising moves)
- ✅ Check Extensions (search extensions on checks)
- ✅ Internal Iterative Deepening (IID for positions without a TT move)
Hash— Transposition table size (1-1024 MB, default 64)Depth— Default search depth (1-30, default 6)Ponder— Enable ponder move outputUseTranspositionTable— Enable/disable TTUseNullMove— Enable/disable Null Move PruningUseLMR— Enable/disable Late Move ReductionsUseIID— Enable/disable Internal Iterative DeepeningUseRazoring— Enable/disable RazoringUseReverseFutility— Enable/disable Reverse Futility PruningUseLMP— Enable/disable Late Move PruningUseProbcut— Enable/disable ProbcutUseSingularExtensions— Enable/disable Singular ExtensionsUseCountermove— Enable/disable Countermove HeuristicClear Hash— Clear transposition table
- ✅ Pawn structure (doubled, isolated, passed, chains)
- ✅ King safety (pawn shield, open files)
- ✅ Piece activity (bishop pair, rooks on open files, on the 7th rank)
- ✅ Piece mobility (knights, bishops, rooks, queen)
- ✅ Center control
- ✅ KQ vs K — Queen vs King (driving to the edge)
- ✅ KR vs K — Rook vs King (forced mate)
- ✅ KBN vs K — Bishop + Knight vs King
- ✅ KP vs K — Square rule, rook pawns
- ✅ KR vs KP — Rook vs Pawn
python main.pyinfo depth 1 score cp 82 nodes 45 time 16 nps 2723 hashfull 0 pv e2e4
info depth 2 score cp 0 nodes 322 time 118 nps 2718 hashfull 0 pv d2d4 d7d5
info depth 3 score cp 69 nodes 971 time 368 nps 2635 hashfull 0 pv e2e4 d7d5 b1c3
info depth 4 score cp 0 nodes 4048 time 1544 nps 2621 hashfull 0 pv b1c3 e7e5 e2e4 b8c6
info depth 5 score cp 61 nodes 4751 time 2150 nps 2208 hashfull 2 pv g1f3 g8f6 b1c3 d7d5 d2d4
bestmove g1f3 ponder g8f6
depth— current search depthscore cp— centipawn evaluation (+ indicates white is better)nodes— number of positions exploredtime— time in millisecondsnps— nodes per second (speed)hashfull— hash table occupancy (0-1000)pv— principal variation (best line of moves)ponder— expected opponent's response
After launching, the engine waits for UCI commands from stdin.
- Open any UCI-compatible program (Arena, CuteChess, etc.)
- Add the engine: point it to the
main.pyfile - Start playing!
> uci
id name OpusChess 2.0
id author AI Assistant
option name Hash type spin default 64 min 1 max 1024
option name Depth type spin default 6 min 1 max 30
...
uciok
> isready
readyok
> position startpos
> go depth 5
info depth 1 score cp 82 nodes 45 time 16 nps 2723 hashfull 0 pv e2e4
...
bestmove g1f3 ponder g8f6
> quit
├── main.py # Entry point
├── board.py # Board representation, FEN, moves
├── move_generator.py # Legal move generation
├── evaluation.py # Position evaluation
├── search.py # Best move search
├── uci.py # UCI protocol implementation
└── README.md # Documentation
Additional commands (not part of standard UCI):
d- display board in text formatperft <depth>- node count (for testing)bench- performance benchmark
Sequence of improvements in chronological order:
- Board Representation (
board.py) — 64-element array, FEN parsing, make/unmake move - Move Generator (
move_generator.py) — all legal FIDE moves - Basic Evaluation (
evaluation.py) — material + piece-square tables - Search (Minimax) (
search.py) — alpha-beta pruning - UCI Protocol (
uci.py) — full UCI support
- Transposition Table — Zobrist hashing, position caching
- Null Move Pruning — skipping a move to force a cutoff
- Late Move Reductions (LMR) — depth reduction for late moves
- Killer Move Heuristic — remembering beta-cutoff moves
- History Heuristic — successful move statistics
- Principal Variation Search (PVS) — PV node optimization
- Aspiration Windows — narrowing the alpha-beta window
- Static Exchange Evaluation (SEE) — fast capture evaluation
- Futility Pruning — pruning unpromising quiet moves
- Check Extensions — extending search on checks
- Internal Iterative Deepening (IID) — TT move search
- Pawn Structure — doubled, isolated, passed, chains
- King Safety — pawn shield, open files
- Piece Activity — bishop pair, rooks on 7th, open files
- Mobility — counting available squares for pieces
- Center Control — bonus for central pawns
- KQ vs K — Queen vs King
- KR vs K — Rook vs King
- KBN vs K — Bishop + Knight vs King
- KP vs K — Square rule, opposition
- KR vs KP — Rook vs Pawn
- UCI Options — configurable settings (Hash, Depth, flags)
- Info callback — statistics output at each depth (depth, nodes, nps, pv, hashfull)
- Contempt — draw score penalty in a winning position
- Repetition avoidance — avoiding draw by repetition
- Ponder move — outputting expected opponent response
- Razoring — aggressive pruning at low depths
- Reverse Futility Pruning — Static Null Move Pruning
- Late Move Pruning (LMP) — pruning late quiet moves
- Probcut — early cutoffs at high depths
- Singular Extensions — extensions for singularly good moves
- Countermove Heuristic — remembering best replies to moves
Comparison for depth 5 on the starting position:
| Configuration | Nodes | Time | Speedup |
|---|---|---|---|
| All optimizations OFF | 15,352 | 6.23s | 1.0x |
| All optimizations ON | 4,751 | 2.15s | 2.9x |
Node reduction: 69%
- Python 3.8+
- No external dependencies
MIT License