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