A chess game implementation featuring bot vs Player (or another bot) gameplay. Contains many different search algorithms like MTD(f) safe fix, Negamax alpha beta, and Best Node Search (still WIP). Also contains other features like transposition tables, and move ordering heuristics. Built with Python, Pygame, and python-chess.
- Player vs Bot Gameplay: GUI with mouse controls, pawn promotion selection, and move highlighting.
- Bot vs Bot Gameplay: Have both bots compete against each other.
- Advanced Search Algorithms: MTD(f) variants, Negamax with alpha-beta pruning.
- Transposition Tables: LRU cache for storing evaluated positions.
- Opening Book Support: Polyglot format books for strong opening play.
- Move Ordering Heuristics: MVV/LVA capture ordering, promotions, and more in the works.
- Game State Detection: Checkmate, stalemate, threefold repetition (WIP), insufficient material, fifty-move rule.
- Configurable Settings: Adjustable search depth, table sizes, piece values, and more.
- Debugging Tools: Move arrows, search statistics.
- Bot Comparison Suite: WIP testing on Bratko-Kopec positions for performance evaluation.
- Clone the repository:
git clone https://github.com/nemeott/chess-ai.git
cd chess-ai- Set up the virtual environment:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
# Or
uv venv
source .venv/bin/activate
uv pip install -r requirements.txt- Run the program:
python game.pyTo play as a human against the bot:
- Set
IS_BOT = Falseinconstants.py. - Run the game.
- Click on pieces to select and move them.
- The AI will respond with its move.
- For pawn promotions, a selection menu will appear.
To watch two AIs play against each other:
- Set
IS_BOT = Trueinconstants.py. - Run the game.
- The game will run automatically, displaying moves and board updates.
- Useful for testing and demonstration purposes.
game.py: Main game loop, board rendering, and user interface.player.py: Human player class handling input and moves.bot5.py: Bot with MTD(f) safe fix, transposition tables, and other optimizations.score.py: Position evaluation and scoring logic.tt_entry.py: Transposition table entry definitions.constants.py: Game constants like search depth, piece values.colors.py: ANSI color codes for console output.requirements.txt: Python dependencies.
Key constants can be adjusted in constants.py:
DEPTH: Search depth for the bot (default: 5).TT_SIZE: Transposition table size in MB (default: 32).OPENING_BOOK_PATH: Path to Polyglot opening book file (optional).WHITE_USE_OPENING_BOOK/BLACK_USE_OPENING_BOOK: Enable/disable opening book for each color (when both players are bots.
IS_BOT: Set toTruefor bot vs bot mode,Falsefor human vs bot.STARTING_FEN: Custom starting position (FEN string, default: standard position).LAST_MOVE_ARROW: Display arrow highlighting the last move.
CHECKING_MOVE_ARROW: Display arrows for moves being checked (debug mode).UPDATE_DELAY_MS: Delay between visual updates in milliseconds (default: 30).- Only really required when when using the move arrow since displaying every move checked is intensive.
RENDER_DEPTH: Depth to render checking moves (set toDEPTHfor root moves).BREAK_TURN: Break after a specific number of turns (for debugging).
The position evaluation function assesses the relative strength of a chess position using several components:
- Material Balance: Piece values derived from Stockfish (pawn: 208, knight: 781, bishop: 825, rook: 1276, queen: 2538, king: 32000 centipawns).
- Piece-Square Tables: Position-dependent bonuses/penalties for each piece type, with separate tables for midgame and endgame phases (tables from Rofchade).
- Pawn Structure: Penalties for isolated pawns (-20) and doubled pawns (-10).
- Bishop Pair Bonus: Additional value (half a pawn) for having both bishops on the board.
- Tapered Evaluation: Smooth interpolation between midgame and endgame scores based on remaining non-pawn material.
- Data Structures: Numpy arrays for piece-square tables and numerical computations; LRU cache for transposition tables; doubly-linked lists (llist) for move history and repetition detection.
- Performance Optimizations: Function caching, incremental score updates using Numba during search, move ordering heuristics (MVV/LVA), MTD(f) null window searches.
- Memory Management: Configurable transposition table sizes, efficient board state copying using python-chess.
- Rendering: SVG-based piece rendering converted to Pygame surfaces using CairoSVG and PIL.
- Testing Framework: Automated bot comparison on Bratko-Kopec test positions using multiprocessing for parallel evaluation.
MIT License.