Skip to content

eng2007/PythonUCIChessEngine

Repository files navigation

OpusChess - UCI Chess Engine

Python License Release

A pure Python chess engine with UCI protocol support.

πŸ‡·πŸ‡Ί Русская вСрсия (Russian version)

Features

Chess Rules (FIDE)

  • βœ… 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 Protocol

  • βœ… 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

Search Engine

  • βœ… 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)

UCI Options

  • Hash β€” Transposition table size (1-1024 MB, default 64)
  • Depth β€” Default search depth (1-30, default 6)
  • Ponder β€” Enable ponder move output
  • UseTranspositionTable β€” Enable/disable TT
  • UseNullMove β€” Enable/disable Null Move Pruning
  • UseLMR β€” Enable/disable Late Move Reductions
  • UseIID β€” Enable/disable Internal Iterative Deepening
  • UseRazoring β€” Enable/disable Razoring
  • UseReverseFutility β€” Enable/disable Reverse Futility Pruning
  • UseLMP β€” Enable/disable Late Move Pruning
  • UseProbcut β€” Enable/disable Probcut
  • UseSingularExtensions β€” Enable/disable Singular Extensions
  • UseCountermove β€” Enable/disable Countermove Heuristic
  • Clear Hash β€” Clear transposition table

Improved Position Evaluation

  • βœ… 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

Endgame Knowledge

  • βœ… 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

Running

python main.py

Statistics Output Example:

info 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

Fields Explanation:

  • depth β€” current search depth
  • score cp β€” centipawn evaluation (+ indicates white is better)
  • nodes β€” number of positions explored
  • time β€” time in milliseconds
  • nps β€” 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.

Usage with GUI

  1. Open any UCI-compatible program (Arena, CuteChess, etc.)
  2. Add the engine: point it to the main.py file
  3. Start playing!

Console Usage Example

> 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

Project Structure

β”œβ”€β”€ 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

Debug Commands

Additional commands (not part of standard UCI):

  • d - display board in text format
  • perft <depth> - node count (for testing)
  • bench - performance benchmark

Development History

Sequence of improvements in chronological order:

Stage 1: Basic Implementation

  1. Board Representation (board.py) β€” 64-element array, FEN parsing, make/unmake move
  2. Move Generator (move_generator.py) β€” all legal FIDE moves
  3. Basic Evaluation (evaluation.py) β€” material + piece-square tables
  4. Search (Minimax) (search.py) β€” alpha-beta pruning
  5. UCI Protocol (uci.py) β€” full UCI support

Stage 2: Search Optimizations

  1. Transposition Table β€” Zobrist hashing, position caching
  2. Null Move Pruning β€” skipping a move to force a cutoff
  3. Late Move Reductions (LMR) β€” depth reduction for late moves
  4. Killer Move Heuristic β€” remembering beta-cutoff moves
  5. History Heuristic β€” successful move statistics
  6. Principal Variation Search (PVS) β€” PV node optimization

Stage 3: Advanced Optimizations

  1. Aspiration Windows β€” narrowing the alpha-beta window
  2. Static Exchange Evaluation (SEE) β€” fast capture evaluation
  3. Futility Pruning β€” pruning unpromising quiet moves
  4. Check Extensions β€” extending search on checks
  5. Internal Iterative Deepening (IID) β€” TT move search

Stage 4: Enhanced Evaluation

  1. Pawn Structure β€” doubled, isolated, passed, chains
  2. King Safety β€” pawn shield, open files
  3. Piece Activity β€” bishop pair, rooks on 7th, open files
  4. Mobility β€” counting available squares for pieces
  5. Center Control β€” bonus for central pawns

Stage 5: Endgame Knowledge

  1. KQ vs K β€” Queen vs King
  2. KR vs K β€” Rook vs King
  3. KBN vs K β€” Bishop + Knight vs King
  4. KP vs K β€” Square rule, opposition
  5. KR vs KP β€” Rook vs Pawn

Stage 6: UCI Extensions

  1. UCI Options β€” configurable settings (Hash, Depth, flags)
  2. Info callback β€” statistics output at each depth (depth, nodes, nps, pv, hashfull)
  3. Contempt β€” draw score penalty in a winning position
  4. Repetition avoidance β€” avoiding draw by repetition
  5. Ponder move β€” outputting expected opponent response

Stage 7: Advanced Algorithms

  1. Razoring β€” aggressive pruning at low depths
  2. Reverse Futility Pruning β€” Static Null Move Pruning
  3. Late Move Pruning (LMP) β€” pruning late quiet moves
  4. Probcut β€” early cutoffs at high depths
  5. Singular Extensions β€” extensions for singularly good moves
  6. Countermove Heuristic β€” remembering best replies to moves

Performance

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%

Requirements

  • Python 3.8+
  • No external dependencies

License

MIT License

About

UCI-compatible chess engine written on Python by Claude Opus 4.5

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors