a chess engine written from scratch in pure Python. no Stockfish, no python-chess, no pip install, just rules, search and a lot of tests
writeup · Lichess bot: @ChessForge_Bot · architecture docs · all my projects
ChessForge in the terminal
every rule, every search trick and every evaluation term is written by hand in Python: full FIDE legality, a real alpha-beta search, a handcrafted evaluator, a UCI interface so it plugs into chess GUIs, and a research side for running tournaments and experiments on the engine itself.
flowchart LR
F["FEN / position"] --> MG["legal move gen<br/>(pseudo-legal + king safety)"]
MG --> S["PVS alpha-beta<br/>iterative deepening"]
S --> Q["quiescence search"]
S <--> TT["transposition table<br/>(Zobrist keys)"]
Q --> E["evaluation"]
S --> U["UCI / CLI"]
| part | what it does |
|---|---|
| full FIDE rules | castling, en passant, promotion, checkmate, stalemate, threefold repetition, the 50-move rule |
| legal move gen | pseudo-legal moves + a king safety filter, so pins and discovered checks just work |
| Zobrist hashing | immutable positions with deterministic repetition keys |
| PVS search | aspiration windows, iterative deepening, and counters for everything |
| pruning | null-move pruning, late move reductions, check extensions, SEE-assisted move ordering, killer and history heuristics |
| evaluation | material + piece-square tables, mobility, bishop pair, pawn structure, passed pawns, rooks on open files, outposts, king tropism, king safety, space, tempo, endgame opposition and fortress scaling |
| transposition table | exact / lower / upper bound entries |
| research platform | tournaments, ablations, Elo estimates, SPRT, parameter sweeps |
| ML tooling | self-play data, sparse features, a quantized evaluator |
| visualization | attack maps, PV reports, evaluation heatmaps |
| CLI | human vs human, human vs engine, engine vs engine |
| UCI | drop it into Arena, Cute Chess and friends |
| perft checked | move gen matches the known node counts |
the evaluation heatmap from visualization.py after 1. e4 e5 2. Nf3 Nc6. the static eval loves Nxe5 (+164) cuz it grabs a pawn and doesnt look one move ahead to Nxe5 back. thats what the search is for
needs Python 3.10+. nothing to install.
git clone https://github.com/hexa3/chessforge.git
cd chessforge
# u vs the engine
python -m chessforge.cli --mode human-ai --human-color white --depth 3 --time 2
# two humans, one board
python -m chessforge.cli --mode human-human
# watch it play itself
python -m chessforge.cli --mode ai-ai --depth 2 --time 1 --max-plies 80
python examples/ai_vs_ai.py --depth 2 --time 0.5 --max-plies 40inside a game:
| command | what it does |
|---|---|
e2e4 |
play a move (promotions are e7e8q, e7e8n, e7e8r, e7e8b) |
legal |
list every legal move |
fen / board |
print the position |
eval |
the static evaluation |
perft N / divide N |
count positions at depth N |
undo |
take back one ply |
quit |
leave |
python -m chessforge.uciuci
isready
position startpos moves e2e4 e7e5
go depth 4
quit
the board is a 64-square array, a1 = 0 to h8 = 63. pieces are signed numbers: positive for white, negative for black, and the size is the piece type (P=1, N=2, B=3, R=4, Q=5, K=6).
move gen makes every pseudo-legal move, plays it on a throwaway copy, and throws it out if the king is attacked. that one check covers pins, en passant discovered checks, walking into check, and castling through attacked squares. attack tables for pawns, knights and kings are precomputed in attacks.py.
positions are immutable: every move returns a new Position. inside the legality filter it uses a cheaper transient copy, so the hot path doesnt make a ton of objects.
| file | what it is |
|---|---|
core.py |
FEN, rules, legal moves, state transitions, perft |
search.py |
PVS, quiescence, pruning, the TT, instrumentation |
evaluation.py |
the evaluator |
attacks.py, bitboards.py |
attack geometry and bitboard views |
benchmark.py |
reproducible perft / search / eval benchmarks |
experiments.py, research.py |
tournaments, ablations, Elo, SPRT, sweeps |
ml.py |
self-play data, sparse features, quantized evaluator |
visualization.py |
attack maps, PV reports, heatmaps |
cli.py, uci.py |
the two ways to talk to it |
more in ARCHITECTURE.md, SEARCH.md, EVALUATION.md and RESEARCH_PLATFORM.md.
python -m unittest discover -s tests -vRan 40 tests in 3.329s
OK
the perft counts it checks against:
| position | depth 1 | depth 2 | depth 3 |
|---|---|---|---|
| starting position | 20 | 400 | 8,902 |
| Kiwipete | 48 | 2,039 | 97,862 |
| en passant / endgame | 14 | 191 | 2,812 |
plus rule tests for castling edge cases, en passant discovered checks, all four promotions, mate and stalemate, threefold repetition, the 50-move rule, and the search only ever returning legal moves.
python -m chessforge.benchmark --repeats 1 --eval-iterations 20
python -m chessforge.research --depths 1,2 --max-plies 8from benchmarks/baseline.csv and benchmarks/post_evolution.csv. after the evaluation got smarter, the search needs way fewer nodes on the start position and the endgame, but each node is slower cuz theres more to evaluate. on Kiwipete and the tactical position it searches more. pure Python is not fast, its a few hundred nodes a second
- an NNUE evaluator instead of the handcrafted one
- an opening book (polyglot) and endgame tablebases (Syzygy)
- bitboard move generation, for speed
- proper time management for tournament play
- a web board
if u have any questions (or beat it) email me or open an issue :)




