
Promote to
This is the original engine, ported line for line from Python to TypeScript and running in your browser.
Why I built it
I’ve been a long-time chess fan. Over one week of winter break in 2022, I took it upon myself to build a chess engine from scratch, aiming to make it as readable as possible. Ultimately it reached an Elo of about 2000.
The project involved a lot of research. I wanted to learn as much as I could about how chess engines actually think, and then build each piece myself.
How it works
A chess engine has to do three things: understand the board, judge how good a position is, and look ahead to find the move that leads to the best position it can force. Tilapia also leans on two databases at the very start and very end of a game, where looking things up beats thinking.
Seeing the board
The board is a list of 64 squares, from a1 up to h8, each holding a piece or nothing. A little padding past the edges means a knight or rook that steps off the board lands on an “off the board” marker instead of crashing the program.
For each position, Tilapia generates every move each piece could make, including castling, en passant and promotion. Then it filters out the illegal ones by trying each move and checking whether its own king would be left in check. It also tracks every position seen so far, so it can recognize threefold repetition and the 50-move rule as draws.
Judging a position
The evaluation function turns a position into a single score, in pawns, from the point of view of the side to move. It adds up five things:
| Term | What it rewards |
|---|---|
| Material | Pawn 1, knight 3.2, bishop 3.3, rook 5, queen 9, plus a small bonus for keeping both bishops |
| Piece-square tables | Pieces on good squares, like knights in the center or a rook on the seventh rank. Each piece has two tables, one for the middlegame and one for the endgame, so the king hides early and comes out to fight once the queens or minor pieces are gone. |
| Pawn structure | Passed pawns are good. Doubled pawns, backward pawns and extra pawn islands are bad. |
| Mobility | Having more legal moves, a rough measure of activity |
| Tempo | A small bonus for being the side to move |
Checkmate scores as infinitely bad for the side that’s mated, and any draw scores exactly zero.
Searching ahead
To pick a move, Tilapia builds a tree of possibilities: each of its moves, each reply to that move, each reply to that reply, and so on, down to a fixed depth. It scores the positions at the bottom and works back up, assuming both sides always choose their best option. This is minimax, written in its compact negamax form: a position that’s worth +2 for one side is worth −2 for the other.
The tree grows explosively, since there are about 30 legal moves in a typical position, so the key is to avoid searching most of it. Alpha-beta pruning skips any branch that provably can’t change the answer:
Pruning works best when the strongest moves are tried first, so Tilapia orders moves before searching them:
- Captures first, ordered by MVV-LVA (most valuable victim, least valuable attacker). Pawn takes queen gets looked at before queen takes pawn.
- The best move from last time. A transposition table remembers positions the search has already scored, along with their best move. The same position often comes up through different move orders, so this saves repeated work, and the remembered move is tried first.
Not stopping mid-fight
A fixed-depth search has a blind spot called the horizon effect. If the search stops right after a capture, it can’t see the recapture coming, and a terrible trade looks like free material. So at the bottom of the tree, Tilapia switches to a quiescence search that only looks at captures and promotions, and keeps going until the position is quiet.
To keep that from exploding too, quiescence uses delta pruning: if a side is so far behind that even winning a queen couldn’t bring it back, the search stops looking.
Openings and endgames
For the first 10 moves, Tilapia plays from an opening book instead of searching. It asks the Lichess masters database which moves strong players choose in the position, and picks one of the top four, weighted by how often each is played. That keeps it from playing the same game every time.
Once 7 or fewer pieces are left on the board, it asks the Lichess Syzygy tablebase instead. These are precomputed tables of every endgame position with 7 or fewer pieces, so the answer is provably perfect play. If either lookup fails, it simply goes back to searching.
The board at the top of this page has three difficulty levels. Easy searches 2 moves deep with no databases, Medium searches 3 deep with the opening book, and Hard searches 4 deep with both.
The hardest part
Honestly, it was that there were always more features to add. Every time one piece worked, I’d read about another technique: a better evaluation term, smarter move ordering, an opening book, endgame tablebases. It never ended!
What I would do differently
If I built it again, I’d use bitboards in a faster language like Rust or C. A bitboard stores the whole board as a handful of 64-bit numbers, one bit per square, so a question like “which squares does this rook attack?” becomes a few bitwise operations instead of a loop over squares. That’s how most strong engines represent the board.
Python made Tilapia readable, but slow. Even porting it line for line to TypeScript for this page made it 15 to 45 times faster, mostly because the browser compiles the code to machine code. Faster search means deeper search, and in chess, deeper search means a stronger engine.
Next projectStylistic Shaders in Unity →