Note
Personal Learning Project
This is a personal hobby project created for learning chess programming and OCaml.
It has no specific roadmap, goals, or directionโjust exploration and experimentation.
Code quality and features evolve organically as I learn. Contributions and suggestions
are welcome, but expect things to change arbitrarily as I try new ideas!
I will concider this project done when I can no longer beat it ๐
A bitboard chess engine written in OCaml with UCI & XBoard protocol support.
Play chess against ChessML using any UCI- or XBoard-compatible GUI! โ๏ธ
Learn with me here: https://alexanderbrevig.github.io/ChessML/
- โกFast Move Generation: Efficient bitboard-based move generation
- ๐ง Alpha-Beta Search: Minimax search with alpha-beta pruning
- ๐พTransposition Tables: Position caching for faster search
- ๐Opening Book: Support for Polyglot opening books generated from PGN
- ๐๏ธBook Generator: 1M+ position-move combinations from 9M+ games
- ๐ฅ๏ธUCI Protocol: Compatible with chess GUIs like Arena and ChessBase
- ๐ฎXBoard Protocol: Play via the xboard/winboard interface
# Ubuntu/Debian
sudo apt install opam stockfish
# Initialize OCaml environment
opam init
opam switch create 5.3.0
opam switch 5.3.0
eval (opam env)
opam install dune alcotest ocaml-lsp-server ocamlformat# Development build (debug mode, faster compilation)
dune build
# Release build (optimized, ~30-40% faster performance)
dune build --profile=release
โ ๏ธ Performance Note: For benchmarks, testing, or actual gameplay, always use--profile=release! Release mode enables critical optimizations (inlining, flambda, bounds check elimination) that significantly improve search speed.
- Debug mode: ~86K NPS
- Release mode: ~120K NPS (+40% faster)
![WARN] You will need 4.5 GiB RAM to run this. It takes ~15 minutes to complete the process and generates 18MB of data.
./scripts/download_openings.fish # about 6 minutes and 30 seconds
dune build --profile=release && CHESSML_PARALLEL=8 ./_build/default/bin/create_book.exe # about 8 minutesYou should the see something like
๐ Processed 9065131 games total (avg 7.9 plies per game)
๐ Building book entries (min 3 games)...
โข 1113046 unique position-move combinations
โข 682561 unique positions
๐พ Writing book.bin...
โข 17808736 bytes
โข 1113046 moves
ChessML will automatically look for the opening book in the following locations (in order):
- Current directory:
./book.bin - XDG data directory:
$XDG_DATA_HOME/chessml/book.bin(typically~/.local/share/chessml/book.bin) - User directory:
~/.chessml/book.bin - System-wide:
/usr/local/share/chessml/book.bin - System-wide:
/usr/share/chessml/book.bin
Cutechess will not find the book, so please install it. To install the opening book for your user:
# Create the directory
mkdir -p ~/.local/share/chessml
# Copy the book file
cp book.bin ~/.local/share/chessml/ChessML uses Just as a command runner see just list.
# Build the project
just
# Run all tests (includes quick search tests ~11s)
just test
# Run deep search validation (~2+ minutes)
just test-search
# Format code
just format
# Watch mode (rebuild on changes)
just watchYou can also use dune commands directly:
# Development build (debug mode)
dune build
# Release build (optimized - use for performance testing!)
dune build --profile=release
# Run all tests (quick by default ~11s)
dune runtest
# Run quick search tests only
dune exec test/engine/test_search.exe -- -q
# Run slow/deep search tests
dune exec test/engine/test_search.exe -- test slow_tests
# Test UCI engine manually
echo "uci
setoption name MaxDepth value 3
position startpos
go depth 2
quit" | ./_build/default/bin/chessml_uci.exe
# Test XBoard engine manually
printf "xboard\nprotover 2\nnew\nusermove e2e4\nquit\n" | ./_build/default/bin/chessml_xboard.exe
# Performance benchmarks (ALWAYS use --profile=release!)
dune exec --profile=release examples/bench_breakdown.exeThe engine supports runtime configuration via both UCI and XBoard protocols:
# Set maximum search depth (1-50)
setoption name MaxDepth value 8
# Set quiescence search depth (1-20)
setoption name QuiescenceDepth value 6
# Enable/disable quiescence search
setoption name UseQuiescence value true
# Enable/disable transposition table
setoption name UseTranspositionTable value true
# Set hash table size in MB
setoption name Hash value 64# Configure via option commands
option MaxDepth=8
option UseQuiescence=true
option DebugOutput=false- Depth 1-3: Beginner ๐ฃ (very fast)
- Depth 4-5: Intermediate ๐ฏ (default, ~1-10s per move)
- Depth 6-8: Advanced ๐ฅ (slower, stronger)
- Depth 9+: Expert ๐ (very slow, tournament strength)
ChessML uses a comprehensive test suite with both quick and thorough validation:
- Core Tests: Data structures, move generation, position handling
- Engine Tests: Evaluation, game logic, zobrist hashing
- Search Tests:
- Quick (~10s): Basic functionality, shallow search validation
- Slow (~2+ min): Deep search validation, performance analysis
- Integration Tests: End-to-end functionality
# Quick development feedback (~11s total)
dune runtest
# or
just test
# Deep search validation when needed
just test-search
# Run specific test categories
just test-core # Core data structures
just test-engine # Engine functionality
just test-integration # Integration testsThe test suite automatically runs quick tests by default to keep development cycles fast, with deep validation available when needed.
Cutechess install
sudo apt install git build-essential cmake qtbase5-dev qtbase5-dev-tools libqt5svg5-dev
git clone [email protected]:cutechess/cutechess.git
cd cutechess
mkdir build
cd build
cmake ..
make
cp cutechess cutechess-cli /usr/local/bin # copy binaries to some directory in your path or add $PWD to $PATHLaunch cutechess, add the engine in settings (I recomment to use xboard version) and create a new game.
- docs/README.md - Intro to ChessML
- Chess Programming Wiki
- Stockfish - Reference implementation
- Real World OCaml
- OCaml Manual
- PGNMentor - Opening PGN database
Contributions and suggestions are welcome! Please see CONTRIBUTING.md for guidelines.
ChessML is licensed under the MIT License - see the LICENSE file for details.
- Figure out if I can publish the book.bin generated from PGNMentor
- Tune evaluation parameters
- Add endgame tablebase support
- Improve parallel search performance
# Run a match against Stockfish (skill level 0)
cutechess-cli \
-engine name=ChessML cmd=./_build/default/bin/chessml_xboard.exe proto=xboard \
-engine name=Stockfish cmd=stockfish proto=uci option."Skill Level"=0 \
-each tc=40/300 \
-rounds 2 \
-repeat \
-pgnout "games.pgn" \
-recover