New York, May 1997. The rematch between Garry Kasparov and IBM's Deep Blue stands at 2½–2½ with one game
In This Chapter
- 34.1 From Shannon's paper to Deep Blue's matches
- 34.2 Counting the tree: perft
- 34.3 Minimax: the idea that never went away
- 34.4 Alpha-beta, move ordering and the horizon
- 34.5 The evaluation function: chess knowledge in a few hundred lines
- 34.6 engine_lab vs Stockfish: a real (small) match
- 34.7 How Stockfish goes deep: pruning, not just speed
- 34.8 AlphaZero, Monte Carlo tree search and Leela
- 34.9 NNUE: the hybrid that won, and engines playing engines
- 34.10 What the machines taught humans
- Spaced Review
- Project Checkpoint: engine_lab.py
- Chapter Summary
- What's Next
Chapter 34 — Chess AI: From Deep Blue to AlphaZero to Modern NNUE — How Computers Learned to Play Chess Better Than Any Human
New York, May 1997. The rematch between Garry Kasparov and IBM's Deep Blue stands at 2½–2½ with one game to play. Kasparov has Black. The game record in our master-games collection runs:
1.e4 c6 2.d4 d5 3.Nc3 dxe4 4.Nxe4 Nd7 5.Ng5 Ngf6 6.Bd3 e6 7.N1f3 h6 8.Nxe6 Qe7 9.O-O fxe6 10.Bg6+ Kd8
11.Bf4 b5 12.a4 Bb7 13.Re1 Nd5 14.Bg3 Kc8 15.axb5 cxb5 16.Qd3 Bc6 17.Bf5 exf5 18.Rxe7 Bxe7 19.c4 1-0
Thirty-seven plies — the shortest decisive game of the twelve the two played. The whole match turned on the moment below. Black has just played 7...h6, asking the knight on g5 to make up its mind, and the machine replies 8.Nxe6, giving a knight for a pawn and two open lines towards Black's uncastled king.

♟️ Position: White Ke1, Qd1, Ra1, Rh1, Bc1, Bd3, Nf3, Ng5, a2, b2, c2, d4, f2, g2, h2; Black Ke8, Qd8, Ra8, Rh8, Bc8, Bf8, Nd7, Nf6, a7, b7, c6, e6, f7, g7, h6 — White to move. FEN:
r1bqkb1r/pp1n1pp1/2p1pn1p/6N1/3P4/3B1N2/PPP2PPP/R1BQK2R w KQkq - 0 8
What does a modern engine make of it? We asked Stockfish 19 (code/06_deep_blue.py):
moves 7-8: 7...h6 8.Nxe6
depth 16: before 7...h6 +0.36 (best Bd6); after it +1.35 (best Nxe6; PV Nxe6 fxe6 Bg6+ Ke7 Bf4 Qa5+)
depth 24: before 7...h6 +0.40 (best Bd6); after it +0.98 (best Nxe6; PV Nxe6 fxe6 Bg6+ Ke7 Bf4 Qa5+)
multipv 3 at depth 20 before Black's 7th move: Bd6 +0.42; Be7 +0.57; Bb4+ +0.58
final position (after 37 plies, Black resigned): +6.42 d20
🤖 Engine check: before Black's 7th move the position is a normal Caro-Kann, +0.40 (Stockfish 19, depth 24), with 7...Bd6 first choice. After 7...h6 it is +0.98 (depth 24), and Stockfish's best reply is exactly the knight sacrifice Deep Blue played. In human terms: ...h6 spends a tempo while the black king is still in the centre, and the e6-pawn is its only cover; once it falls, Bg6+ and Bf4 arrive with check and threats faster than Black can castle. The sacrifice is not a gamble — it is the best move, and twenty-nine years later a far stronger engine agrees.
Twenty-nine years on, the scene is a Thursday evening at the Hartwell library.
Sam: I watched that on the evening news. Everyone said chess was finished. "The computer is champion now, why would anyone play?"
Dev: And now there's an engine on my phone that would crush Deep Blue. I wrote my own this week — in Python. It loses to Stockfish when Stockfish is only allowed to look at a hundred positions per move.
Lena: A hundred? I look at about three.
Sam: And you still beat me on Tuesday.
They are all right, and that is this chapter. We will build a small engine — perft-tested move generation, minimax, alpha-beta, move ordering, quiescence search and a hand-written evaluation — and measure each idea: how many positions it saves, how much better its moves get, how it fares against a deliberately starved Stockfish 19. Then we follow the real history: Shannon's 1950 paper, Deep Blue, Stockfish's search, AlphaZero's neural network and tree search, Leela, and NNUE, the hybrid that now rules. Along the way we use the book's data — the twelve Kasparov–Deep Blue games, nearly four thousand TCEC engine games relayed on Lichess, and a quarter-century of master games — to ask what the machines did to human chess.
💻 Developer Track: This is your chapter. Build
engine_lab.pyalongside the text, run every script incode/, and treat Sections 34.2–34.7 as a lab manual. Exercise E1 asks you to add one feature and prove it helps — the hardest part of engine programming.♟️ Improver Track: Skip the code blocks, keep the diagrams. The engine ideas map onto your own thinking: move ordering is the checks–captures–threats scan, quiescence is "finish the exchange before you evaluate", and the evaluation function is Part I's checklist in numbers. Read 34.1, 34.4's horizon section, 34.8 and 34.10 closely.
📊 Analyst Track: Focus on the measurements — node counts (34.4), evaluation correlations (34.5), the match and its intervals (34.6), the TCEC data (34.9, Case Study 2) and the h-pawn trend (34.10). Each comes with its sample, filter and limits.
34.1 From Shannon's paper to Deep Blue's matches
In 1950 Claude Shannon published "Programming a Computer for Playing Chess". No computer of the day could play a game, but the paper laid out the design every engine since has followed: represent the position, generate legal moves, look ahead through the game tree, score the positions at the end of each line with an evaluation function, and back the scores up assuming both sides play their best. He also estimated the size of the game — roughly 10^120 possible games, a back-of-envelope figure meant to prove that no machine would ever search all of it.
For the next forty years programmers did what Shannon described, faster and faster. Our master-games data lets us watch the last steps of that climb through the games of the strongest humans who played them:
- 1989: Kasparov beat Deep Thought (Deep Blue's predecessor) in both games of a two-game exhibition.
- 1993: a prototype Deep Blue drew three games and lost one in a four-game match with Bent Larsen in Copenhagen; in New York it beat Judit Polgar in one game and drew the other.
- 1996, Philadelphia: Deep Blue won the first game of a six-game match with Kasparov — widely described as the first game a computer won against a reigning world champion at a classical time control — but Kasparov won three and drew two: 4–2.
- 1997, New York: the upgraded machine won the rematch 3½–2½: one win for Kasparov, two for Deep Blue, three draws.
1996: Kasparov 4 - Deep Blue 2 (wins K 3, DB 1, draws 2)
1997: Kasparov 2.5 - Deep Blue 3.5 (wins K 1, DB 2, draws 3)
(Both scores are recomputed from the results of the twelve games in our data.) The designers' own account (Campbell, Hoane and Hsu, Artificial Intelligence, 2002) describes a classic Shannon "Type A" machine built at enormous scale: a parallel alpha-beta search running on many special-purpose chess chips, and a large hand-designed evaluation function. Nothing in it learned by itself; everything it knew, people put there. After game 6 Kasparov voiced suspicions about some of the machine's play; IBM rejected them, and there was no third match.
How good was Deep Blue by today's standard? We can at least measure both players with the same ruler. Stockfish 19 at depth 16 analysed every move after move 8 of all twelve games:
=== Stockfish 19 depth 16, moves 9+ (book CPL, +-1000 clip; accuracy = book mean) ===
moves acpl median_cpl accuracy mistakes blunders
year player
1996 Deep Blue 238 17.4 3.0 95.3 3 2
Kasparov 237 14.1 1.0 96.2 3 1
1997 Deep Blue 212 15.8 2.0 94.9 4 1
Kasparov 212 19.0 4.0 94.5 1 1

📊 Figure (described): Average centipawn loss (moves 9 onward) for each player in each of the twelve games, Stockfish 19 at depth 16. Neither side dominates: Kasparov was the more accurate player in 1996 (14.1 vs 17.4) and the less accurate in 1997 (19.0 vs 15.8), and his two worst games of the rematch — game 2 (44.2) and game 6 (44.9) — are the two he lost.
Treat these numbers carefully. Six games per side per match is a tiny sample; ACPL depends on how sharp the positions were, and both players chose sharp positions on purpose (Chapter 32 explains why ACPL is not a rating). The fair summary is that in 1997 the human and the machine played at a broadly similar level of accuracy by a modern engine's lights, and the match was decided by a few critical moments — exactly the picture Chapter 13 paints for human games.
⚠️ Common pitfall: "Deep Blue beat Kasparov" as a one-line summary. Kasparov won the first match 4–2; Deep Blue won the second 3½–2½, with the decisive game lasting 19 moves. Both matches were six games — far too few to say which player was "stronger" in any statistical sense (Chapter 30 shows how wide the interval on a six-game score is). What 1997 proved was that a machine could win such a match.
🔄 Check your understanding 1. What were the scores of the 1996 and 1997 Kasparov–Deep Blue matches, and how do we know? 2. Which three ingredients of Shannon's 1950 design does every engine still use? 3. Stockfish 19 rates 7...h6 in game 6 as costing about 0.6 pawns at depth 24. Why is "Deep Blue found the best move" a stronger statement than "Deep Blue's move worked"?
Answer
- 1996: Kasparov 4–2 (three wins, one loss, two draws); 1997: Deep Blue 3½–2½ (two wins, one loss, three draws). Both are recomputed from the twelve game results in the master-games data.
- Legal move generation over a game tree, a look-ahead search that backs up scores assuming best play (minimax), and an evaluation function for the positions where the search stops.
- A move can "work" because the opponent errs later; Stockfish's depth-24 preference for 8.Nxe6 says the sacrifice is objectively the strongest continuation, independent of how the game went.
34.2 Counting the tree: perft
Every chess program, from Shannon's thought experiment to Stockfish 19, stands on one unglamorous layer: move generation — given a position, list every legal move, then make a move and unmake it again. Get it slightly wrong and nothing above it can be trusted. An engine that forgets en passant will one day "discover" a winning line that is illegal; an engine that lets its own king walk into check will report mates that don't exist. So before we search anything, we count.
📐 Definition — perft. perft(d) ("performance test") is the number of leaf positions reached by playing every legal move sequence of exactly d plies from a position, counted with repetition — two move orders that reach the same position count twice. It uses no evaluation and no pruning. Its only purpose is to check move generation and make/unmake against numbers that have been verified many times.
The code is six lines of recursion. Our version lives in project/outpost/engine_lab.py:
def perft(board: chess.Board, depth: int, mode: str = "legal") -> int:
if depth == 0:
return 1
moves = list(board.legal_moves) # 'pseudo' and 'no_ep' modes are deliberately broken
if depth == 1:
return len(moves) # "bulk counting": no need to make the last move
n = 0
for m in moves:
board.push(m) # make
n += perft(board, depth - 1, mode)
board.pop() # unmake
return n
code/01_perft.py runs it from the start position and from four standard test positions that
programmers use because they are crammed with the awkward rules — castling through attacked squares,
en passant captures that expose a king, promotions with capture. The best known is Kiwipete:

♟️ Position: White Ke1, Qf3, Ra1, Rh1, Bd2, Be2, Nc3, Ne5, a2, b2, c2, d5, e4, f2, g2, h2; Black Ke8, Qe7, Ra8, Rh8, Ba6, Bg7, Nb6, Nf6, a7, b4, c7, d7, e6, f7, g6, h3 — White to move. FEN:
r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1
Both sides can still castle both ways, pins abound, and the b4-pawn is one move away from en passant chances. Here is what our run printed:
=== perft (legal move generation, python-chess 1.11.2 ) ===
start d=1: 20 ( 0.00 s, 90,950 leaves/s) matches reference
start d=2: 400 ( 0.00 s, 166,674 leaves/s) matches reference
start d=3: 8,902 ( 0.03 s, 265,401 leaves/s) matches reference
start d=4: 197,281 ( 0.70 s, 283,641 leaves/s) matches reference
start d=5: 4,865,609 ( 16.38 s, 297,122 leaves/s) matches reference
kiwipete d=1: 48 ( 0.00 s, 282,021 leaves/s) matches reference
kiwipete d=2: 2,039 ( 0.01 s, 315,084 leaves/s) matches reference
kiwipete d=3: 97,862 ( 0.27 s, 358,668 leaves/s) matches reference
kiwipete d=4: 4,085,603 ( 13.03 s, 313,547 leaves/s) matches reference
position3 d=1: 14 ( 0.00 s, 244,755 leaves/s) matches reference
...
position5 d=3: 62,379 ( 0.17 s, 366,289 leaves/s) matches reference
=== Move-type breakdown of the leaves (perft_breakdown) ===
start d=4: nodes 197,281, captures 1,576, ep 0, castles 0, promotions 0, checks 469, checkmates 8
kiwipete d=3: nodes 97,862, captures 17,102, ep 45, castles 3,162, promotions 0, checks 993, checkmates 1
Every count matches the published perft tables. Four plies from the start — two moves each — there are 197,281 move sequences, and eight of them already end in checkmate (the Fool's Mate family). Five plies gives 4,865,609. Python needed 16 seconds just to count them, at roughly 300,000 leaves a second; keep that speed in mind, because it is the ceiling for any engine written in pure Python on top of python-chess.

📊 Figure (described): Leaf counts from four test positions, depths 1–5, on a logarithmic scale, with a dashed line for 30.8^d (30.8 is the mean number of legal moves in positions from real Lichess games, measured in our data snapshot). Each extra ply multiplies the tree by roughly 20–50. The quiet endgame "position 3" grows far more slowly than the busy middlegame Kiwipete — branching depends on the position, not on a universal constant.
Catching a bug
The real use of perft is diagnostic. engine_lab.perft has two deliberately broken modes: "pseudo"
generates pseudo-legal moves (it forgets to check whether the mover's own king is left in check), and
"no_ep" forgets en passant. Watch what they do to the counts:
=== Bug hunt: two broken generators ===
start d=4: legal 197,281 | pseudo-legal 197,742 (+461) | no en passant 197,281 (+0)
kiwipete d=2: legal 2,039 | pseudo-legal 2,044 (+5) | no en passant 2,038 (-1)
kiwipete d=3: legal 97,862 | pseudo-legal 98,515 (+653) | no en passant 97,766 (-96)
position3 d=3: legal 2,812 | pseudo-legal 4,867 (+2,055) | no en passant 2,810 (-2)
position3 perft_divide(3): root moves whose subtree count differs without en passant:
Ka6 (a5a6): correct 240 buggy 239
Ka4 (a5a4): correct 224 buggy 223
Three lessons. First, the start position is a poor test: the missing en passant rule changes nothing through four plies, because no pawn can capture en passant that early. Second, the king-safety bug shows up at depth 4 from the start (+461 sequences in which a king walked into, or stayed in, check) but already at depth 2 in Kiwipete — rich positions expose bugs sooner. Third, perft_divide — perft split by root move — tells you where to look: in position 3 only the two king moves Ka6 and Ka4 have the wrong subtree count, so the bug lives in a line that begins with a king move and later allows an en passant capture. Programmers then divide again inside that subtree until a single position is left. It is bisection, applied to a game tree.
🔍 Why this works: perft is an invariant. The number of legal move sequences is a property of the rules, not of any program, so independent generators written in different languages agree to the last digit. One wrong number proves a bug exists; a matching set of numbers across tricky positions is strong (not perfect) evidence that none does.
⚠️ Common pitfall: testing only from the start position. As the table shows, a generator that ignores en passant passes perft(1)–perft(4) from the initial position perfectly. Always include positions like Kiwipete that exercise castling, en passant and promotion.
📋 Try it — Engine analysis. Run
perft_breakdownon position 3 (8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1) at depth 4. How many en passant captures and checkmates are among the 43,238 leaves? Why can an endgame with so few pieces still contain en passant?
Answer
Our run prints
nodes 43,238, captures 3,348, ep 123, castles 0, promotions 0, checks 1,680, checkmates 17. En passant needs only a pawn on its fifth rank and an enemy pawn arriving beside it with a double step — here the white e2/g2 pawns and Black's f4-pawn create exactly that, and with the kings exposed on the edges, checks are common too.
34.3 Minimax: the idea that never went away
Shannon's 1950 paper described what we now call minimax: look ahead a fixed number of plies, score the positions at the end with an evaluation function, and back the scores up the tree assuming each side picks the move that is best for itself.
📐 Definition — minimax / negamax. The value of a position for the side to move is the maximum, over its legal moves, of the value of the resulting position for the opponent, negated:
V(p) = max over moves m of −V(p after m), withV = evaluationat the depth limit,−MATEif checkmated, 0 if stalemated. Writing it with the negation ("negamax") lets one function serve both colours.
def minimax(board, depth, eval_fn=material_eval):
nodes = 0
def rec(d, ply):
nonlocal nodes
nodes += 1
moves = list(board.legal_moves)
if not moves: # mate or stalemate
return -MATE + ply if board.is_check() else 0
if d == 0:
return eval_fn(board) # side to move's point of view
best = -INF
for m in moves:
board.push(m)
best = max(best, -rec(d - 1, ply + 1))
board.pop()
return best
return rec(depth, 0), nodes
The problem is arithmetic. Minimax visits every node of the tree, so its cost is perft plus the interior nodes. In our data snapshot the mean number of legal moves in positions from real games is 30.8 on Lichess and 31.0 in OTB broadcast games (35.8 in OTB middlegames); Chapter 10 measured the same thing from a human angle — ~35 legal moves in a middlegame, only ~6 of them forcing. A tree with 35 branches per node has 35⁴ ≈ 1.5 million leaves at four plies.
We ran plain minimax with a material-only evaluation on the position before Kasparov's famous 24.Rxd4 in Kasparov–Topalov, Wijk aan Zee 1999 (we return to this position in Section 34.4). White has 47 legal moves. Depth 1 visits 48 nodes (the root and its 47 children); depth 2, 1,765; depth 3, 81,018 — exactly the count of an earlier, independent measurement made for this book with the same kind of toy search, a useful check that two runs agree. That earlier measurement also pushed on to depth 4: 2,945,634 nodes and 100 seconds of Python. Depth 5 would take over an hour; depth 10 would outlast the universe. Shannon's 10^120 estimate (Section 34.1) was never a count anyone could verify, but the point stands: you cannot search the whole tree, so a chess program must decide what not to search.
Shannon sketched two answers. A Type A program searches every move to a fixed depth (full width) and relies on speed. A Type B program, like a human, looks only at plausible moves and follows them further. For forty years the Type A family won, thanks to an algorithm that makes full-width search far cheaper without changing its answer.
34.4 Alpha-beta, move ordering and the horizon
Suppose you are White and have found a move that keeps the balance. You now try a second move and see that Black has a reply that wins a piece. Do you need to look at Black's other replies to the second move? No: one refutation is enough to prove the second move is worse than the first. That is the whole of alpha-beta pruning.
📐 Definition — alpha-beta. Search with a window (α, β): α is the score the side to move is already guaranteed elsewhere, β the score the opponent is already guaranteed elsewhere. As soon as one move scores ≥ β, the opponent would never allow this position, so the remaining moves are skipped (a beta cut-off). Alpha-beta returns exactly the minimax value at the root; it only skips work.
The saving depends on order. If the refutation is the first reply you try, the cut-off comes at once; if it is the last, you have searched everything anyway. So engines sort moves before searching them: captures first (most valuable victim, least valuable attacker — "MVV-LVA"), then the move that was best in the previous, shallower iteration (the hash move, from the transposition table, which also remembers positions reached by different move orders), then killer moves — quiet moves that caused cut-offs at the same depth in sibling branches.
code/02_search_nodes.py measures what each step buys on 12 middlegame positions from real OTB games
(drawn at random from the book's position sample, engine and online events excluded; median 40 legal
moves) plus the Kasparov–Topalov position. Every variant uses the material-only evaluation and no
extensions, so the root value must be identical:
root value identical across minimax / alpha-beta variants in 69 of 70 (position, depth) cells
=== Nodes visited, Kasparov-Topalov before 24.Rxd4 (material eval, no quiescence) ===
variant minimax ab ab_captures ab_full
depth
1 48.0 48.0 48.0 48.0
2 1765.0 300.0 120.0 168.0
3 81018.0 10481.0 1832.0 2000.0
4 NaN 24517.0 4217.0 5834.0
5 NaN NaN 67616.0 55532.0
=== Median nodes over the 12 OTB middlegame positions ===
variant minimax ab ab_captures ab_full lab_full stockfish19
depth
1 41.0 41.0 41.0 41.0 202.0 58.0
2 1705.0 437.0 128.0 170.0 846.0 126.0
3 68052.0 7598.0 1928.0 2018.0 4468.0 264.0
4 NaN 58219.0 7172.0 8337.0 28614.0 584.0
5 NaN NaN 73174.0 55239.0 88482.0 1096.0
depth 3: median minimax / alpha-beta(captures first) = 34.5x
depth 3: median alpha-beta(no ordering) / alpha-beta(captures first) = 3.9x
depth 4: median alpha-beta(no ordering) / alpha-beta(captures first) = 8.5x

📊 Figure (described): Median positions visited (log scale) at depths 1–5 for plain minimax, alpha-beta without ordering, alpha-beta with captures first, and alpha-beta with hash move, killers and a transposition table. The dashed line is Knuth and Moore's count of leaves in the "minimal tree" that perfect ordering would search (b = 40). Alpha-beta with simple ordering stays close to that minimum; plain minimax is already 34.5 times more expensive at depth 3 and the gap widens every ply.
Read the Kasparov–Topalov rows from left to right: at depth 3, 81,018 nodes for minimax, 10,481 for unordered alpha-beta, 1,832 once captures go first — 44 times less work for the same answer. Ordering alone is worth a factor of 3.9 at depth 3 and 8.5 at depth 4 in the median OTB position. The fancier ordering (hash moves, killers, transposition table) helps most at depth 5, where it saved about a quarter of the nodes; at shallow depths its bookkeeping costs more than it saves.
And the one cell in 70 where the values disagree? Kasparov–Topalov at depth 5: the captures-first search says +2.00, the transposition-table search +1.00. That is not a bug in alpha-beta; it is the transposition table grafting a result computed along one move order onto another, where the repetition history differs. Real engines live with this "search instability" because the speed is worth it. It is a good reminder that an engine's number is the output of a procedure, not a fact about the position.
🔍 Why this works: with perfect ordering, alpha-beta must still examine every move of one side but only one move (the refutation) of the other, alternately. That is why the minimal tree grows like b^(d/2) instead of b^d: perfect ordering roughly doubles the depth you can afford. Donald Knuth and Ronald Moore proved this bound in their classic analysis of alpha-beta; our captures-first search sits within a factor of two or three of it.
🧩 Productive struggle. Before reading on: our engine searches the Kasparov–Topalov position (below) with a material-only evaluation and no quiescence. At depths 1, 2, 3 and 4 it reports +9, 0, +6 and 0 for White (the earlier measurement recorded the same values). Why would the score jump up and down by a queen's worth between odd and even depths? Write your guess down.
The horizon, and the odd–even effect

♟️ Position: White Kb1, Qf4, Rd1, Re1, Bh3, Na5, a3, b2, c2, d5, f3, g3, h2; Black Ka7, Qd6, Rd8, Rh8, Ba8, Nf6, a6, b5, c5, d4, f7, g6, h7 — White to move. FEN:
b2r3r/k4p1p/p2q1np1/NppP4/3p1Q2/P4PPB/1PP4P/1K1RR3 w - - 1 24
code/05_horizon_mcts.py searched this position at depths 1–5 three ways:
=== Part 1: engine_lab on Kasparov-Topalov before 24.Rxd4, depths 1-5 === (scores, White POV)
setup full eval + quiescence material + quiescence material, no quiescence
depth
1 -0.35 +0.00 +9.00
2 -0.35 +0.00 +0.00
3 -0.33 +0.00 +6.00
4 -0.33 +0.00 +0.00
5 -0.56 +0.00 +1.00
(moves chosen: Qxd6 at every depth in every setup, except Nc6+ at depth 3 without quiescence)
Stockfish 19 depth 20: -0.33, best Rxd4; PV Rxd4 Kb6 b4 Qxf4 Rxf4 Nxd5
Here is the answer to the productive-struggle question. Without quiescence, an odd-depth search ends on White's move: at depth 1 the search sees 24.Qxd6 and stops, believing White has simply won the queen (+9). At depth 2 it sees ...Rxd6 recapturing: level (0). At depth 3 it finds a sequence that ends, again, right after a White capture (+6), and so on. The number swings because the search keeps stopping in the middle of an exchange — the horizon effect in its purest form. Add quiescence search — at the depth limit, keep searching captures (and all replies to check) until the position is quiet — and the material-only score becomes a steady 0.00: the queens come off and nothing else happens.
📐 Definition — horizon effect / quiescence search. The horizon effect is the error a fixed-depth search makes when it evaluates an unstable position at its depth limit — a pending recapture, a hanging piece, a check. Quiescence search extends every leaf with capture moves only (plus check evasions), allowing the side to move to "stand pat" on the static evaluation if it does not want to capture, until no captures remain.
Notice two more things. First, every setup of our engine wants 24.Qxd6 — trade queens, keep the extra pawn — while Stockfish 19 at depth 20 picks Kasparov's 24.Rxd4 and calls the position −0.33, a small edge for Black. As Chapter 1 settled, Stockfish chooses the sacrifice at every depth it was tested but never rates it as winning; the brilliance lies in the human's practical judgment, not in a forced win. Second, look at the cost: quiescence is not cheap. At depth 5 our full engine spent 93.5% of its nodes in quiescence on the 12 OTB middlegames — most of an amateur engine's time goes into resolving captures.
Quiescence matters beyond this one position. To find a real horizon blunder we let the engine search positions from OTB middlegames at depth 2 with and without quiescence, and stopped at the first position where the quiescence-free search chose a capture that Stockfish rated at least 1.50 pawns worse. The very first position scanned qualified — an OTB game played in January 2026 between players rated 1586 and 1463:

♟️ Position: White Kg1, Qf6, Rb1, Bd3, Bd6, a3, e5, f2, g3, h2; Black Ka8, Qd4, Rd8, Rg7, Be8, a7, b7, d5, e6 — Black to move. FEN:
k2rb3/pp4r1/3BpQ2/3pP3/3q4/P2B2P1/5P1P/1R4K1 b - - 2 35
position SF19 d16: +0.00 best Rgd7
no quiescence, depth 2: Rxd6 believed +1.64 -> SF19 d16 after it +4.91
with quiescence, depth 2: Rgd7 believed -0.32 -> SF19 d16 after it +0.00
cost of the horizon move: 4.91 pawns (SF19 d16); SF reply after it: Qxg7 Rd7 Qf8 Rd8
White's queen on f6 attacks the rook on g7. The quiet answer, 35...Rgd7, keeps the balance (0.00 at depth 16). The quiescence-free engine sees every line end right after White's reply, half-way through the tactics, and judges that grabbing the bishop with 35...Rxd6 costs "only" the exchange (+1.64 for White). Stockfish shows what the horizon hid: after 35...Rxd6 White does not recapture at all — 36.Qxg7 takes the other rook, +4.91. The quiescence search, which keeps looking at captures, sees Qxg7 and rejects the line. This is exactly the mistake club players make when they "count to the end of the exchange" but stop one capture too early (Chapter 10's advice to finish every forcing sequence is quiescence search for humans).
🔄 Check your understanding 1. Alpha-beta and minimax return the same value. What, then, do the 81,018 and 1,832 node counts at depth 3 in Kasparov–Topalov measure? 2. Why does a material-only search without quiescence show +9 / 0 / +6 / 0 as the depth increases? 3. In the 12 OTB middlegames, captures-first ordering saved a factor of 3.9 at depth 3 and 8.5 at depth 4. Why does the benefit of ordering grow with depth?
Answer
- The work needed to prove the same value: alpha-beta with captures first proved it 44 times more cheaply by skipping branches that could not change the result.
- Odd depths end after White's capture and even depths after Black's recapture; without quiescence the evaluation is taken mid-exchange, so it oscillates.
- Each good cut-off removes a whole subtree; the deeper the search, the bigger the subtrees that are removed, so the multiplicative saving compounds ply by ply.
34.5 The evaluation function: chess knowledge in a few hundred lines
Search decides which positions to look at; the evaluation function decides what each one is worth when the search stops. For fifty years this was where chess knowledge lived — hand-written by programmers, often with grandmaster help, term by term: material, piece placement, pawn structure, king safety, mobility. Deep Blue's evaluation was a large hand-built function of this kind; Stockfish's "classical" evaluation, before 2020, was a highly tuned one.
engine_lab.evaluate is deliberately small, so you can read all of it:
📐 Definition — engine_lab's evaluation. Score (White's point of view, centipawns) = material (1/3/3/5/9 pawns, the book's piece values) + piece-square tables (a bonus or penalty for each piece on each square) blended between a middlegame and an endgame table by the amount of non-pawn material left (62 at the start; the book's endgame threshold is 26) + 30 for the bishop pair. The search uses the side-to-move's point of view.
The piece-square tables are where the chess goes. Rather than invent numbers, we derived the knight, bishop and queen tables from the geometry of Chapter 3: the bonus on each square is proportional to how many squares the piece attacks from there on an empty board. A knight attacks 8 squares from d4 and 2 from a1, so:
knight table (rank 8 at top, White's point of view; 5 cp per attacked square above 5)
-15 -10 -5 -5 -5 -5 -10 -15
-10 -5 5 5 5 5 -5 -10
-5 5 15 15 15 15 5 -5
-5 5 15 15 15 15 5 -5
-5 5 15 15 15 15 5 -5
-5 5 15 15 15 15 5 -5
-10 -5 5 5 5 5 -5 -10
-15 -10 -5 -5 -5 -5 -10 -15
"A knight on the rim is dim" becomes thirty centipawns between corner and centre. The pawn table rewards advancing (+70 on the seventh rank) and central pawns on the fourth and fifth ranks, and gently punishes leaving d2/e2 at home; the king has two tables — tucked in the corner (+20 on g1/b1) while queens and rooks are on, marching to the centre (+22 on d4/e4/d5/e5, −30 on the edge) once they are gone. That last pair is Chapter 14's "the king is a fighting piece in the endgame", reduced to arithmetic.
How good is this? code/03_eval_vs_stockfish.py drew 240 positions from the book's position sample — 120
from Lichess games (bot games excluded), 120 from OTB broadcast games (engine and online events excluded),
40 of each phase from each source — and compared four numbers with Stockfish 19 at depth 12:
=== Part 1: static and searched evaluations vs Stockfish 19 depth 12 (White POV, cp, clipped +-1000) ===
material only Pearson r 0.796 Spearman 0.649 mean |diff| 163 cp
engine_lab static eval Pearson r 0.812 Spearman 0.686 mean |diff| 155 cp
engine_lab quiescence value Pearson r 0.869 Spearman 0.782 mean |diff| 134 cp
engine_lab depth-3 search Pearson r 0.901 Spearman 0.815 mean |diff| 119 cp
opening (n=80): r material 0.116 | r static 0.171 | r depth-3 0.756
middlegame (n=80): r material 0.752 | r static 0.774 | r depth-3 0.894
endgame (n=80): r material 0.852 | r static 0.864 | r depth-3 0.921
positions with level material (n=128): r(static, SF) = 0.325; r(depth-3, SF) = 0.751

📊 Figure (described): Each dot is a real position: Stockfish 19's depth-12 evaluation (vertical) against material only (left, r = 0.80) and against engine_lab's depth-3 search (right, r = 0.90). The material-only panel is a set of vertical stripes — every position with level material scores 0.00 no matter what — while three plies of search spread the dots along the diagonal.
Three findings, each worth a sentence at the board:
- Material explains most of the evaluation — when material is unequal. Counting pieces alone correlates 0.80 with Stockfish over all 240 positions. That is not a compliment to material counting; it reflects how often real positions already contain a material imbalance (in endgames, 0.85).
- Static knowledge barely helps; a little search helps a lot. Our tables lift the correlation from 0.796 to 0.812. Resolving pending captures (quiescence) lifts it to 0.869, and three plies of real search to 0.901. In the 128 positions with level material — where only "positional" knowledge can speak — the static evaluation correlates just 0.325 with Stockfish, the depth-3 search 0.751. Much of what looks like positional understanding in Stockfish's number is concrete tactics our static function cannot see.
- Openings are where crude evaluation is blind (r = 0.17 static). In the opening the material is level and the eval differences are small and positional — exactly what a few hand-written tables cannot rank. (Compare Chapter 23, where even Stockfish's opening numbers had little to say about club results.)
🤖 Engine check: the Opera Game (Morphy vs the Duke of Brunswick and Count Isouard, Paris 1858), before White's 16th move. engine_lab's static evaluation, broken into terms:
{'material': -100, 'pst': 14, 'bishop_pair': 0, 'phase': 0.306, 'total': -86}— Black is "better by 0.86" because White has given material. Search tells the truth: White mates in two (16.Qb8+! Nxb8 17.Rd8#). A static evaluation, however clever, cannot see a forced line; that is search's job. The division of labour — search for the concrete, evaluate the quiet — is the deepest idea in this chapter, and it is also a fair description of how strong humans think (Chapter 1).⚠️ Common pitfall: reading a correlation of 0.80 as "material is 80% of chess". The correlation is high because big material imbalances produce big evaluations at both ends of the scale; it says nothing about the many level positions in the middle, where material is silent (r = 0.325 even with our tables). Always look at the subgroup that matters for your question.
34.6 engine_lab vs Stockfish: a real (small) match
Node counts and correlations are proxies. The honest test of an engine is to make it play. But against
full-strength Stockfish 19 a pure-Python engine would lose every game in a few dozen moves and teach us
nothing, so we starved Stockfish instead: python-chess lets us send go nodes N, which makes Stockfish
stop after searching N positions per move. With one thread and a cleared hash table at the start of each
game, the result is deterministic — run the script again and you get the same games.
📐 Definition — the match design (
code/04_match.py). engine_lab plays with 5,000 nodes per move (iterative deepening; the move from the last fully completed depth is played). Stockfish 19 plays with 1, 100 or 1,000 nodes per move. Six openings (Italian, Queen's Gambit Declined, Najdorf, French Advance, King's Indian, English) are played with each colour at each level: 36 games. Threefold repetition and the fifty-move rule end games automatically; games still running at 160 plies are adjudicated by Stockfish 19 at depth 14 (±3.00 or more = win, otherwise draw).
The whole match took 263 seconds on three processes. The results:
=== engine_lab (5,000 nodes/move) vs Stockfish 19 at fixed nodes per move ===
SF nodes= 1: engine_lab +3 =8 -1 -> 7.0/12 = 58.3% (Wilson 95% 32-81%); median plies 102; adjudicated 1
SF nodes= 100: engine_lab +1 =6 -5 -> 4.0/12 = 33.3% (Wilson 95% 14-61%); median plies 92; adjudicated 1
SF nodes= 1000: engine_lab +0 =3 -9 -> 1.5/12 = 12.5% (Wilson 95% 3-40%); median plies 85; adjudicated 1
terminations: {'checkmate': 18, 'threefold_repetition': 15, 'adjudicated': 3}
engine_lab mean completed depth per move: 3.2; mean seconds per move 0.46

📊 Figure (described): engine_lab's score (with Wilson 95% intervals) against Stockfish 19 limited to 1, 100 and 1,000 nodes per move, 12 games each. Our engine holds its own against a one-node Stockfish (58.3%), loses clearly at 100 nodes (33.3%) and is crushed at 1,000 nodes (12.5%) — although it searches five times more positions per move than Stockfish does at that level.
Two things stand out. First, the intervals are enormous: 58.3% over 12 games could be anything from 32% to 81%. Twelve games cannot separate "about equal" from "clearly better", which is why real engine developers test changes over tens of thousands of games. Second, a thousand Stockfish nodes beat five thousand of ours decisively. Stockfish's one-node "search" is little more than its neural evaluation plus a quick look at captures — and that alone is roughly a match for our three-ply search. The evaluation function is doing the work of several plies of search.
Stockfish 19 at depth 12 then judged every move after the forced opening:
=== Move quality by Stockfish 19 depth 12 (moves after the forced opening) ===
moves acpl accuracy blunders_per_100
level who
1 Stockfish 560 50.6 92.4 2.9
engine_lab 563 54.2 91.4 3.6
100 Stockfish 538 42.7 93.8 1.1
engine_lab 539 57.0 91.4 3.0
1000 Stockfish 506 26.5 95.8 0.0
engine_lab 502 44.6 92.7 1.8
engine_lab's average centipawn loss sits around 45–57 throughout; Stockfish's falls from 50.6 at one node to 26.5 at a thousand, with no blunders at all in the 506 moves at that level. These numbers come from engine-vs-engine games and a depth-12 judge, so do not line them up against human ACPL tables — the positions, the judge and the opponents all differ (Chapter 32).
Both sides blunder — and only one notices
The single worst move engine_lab made in the match came in the French Advance against the one-node Stockfish, with engine_lab playing Black:

♟️ Position: White Kg1, Qb3, Re1, Ba3, Nb1, Ne5, a2, f3, g2, h2; Black Ke8, Qa1, Ra8, Rh8, Ba6, Ne4, a7, c3, c6, d5, e6, g7, h7 — Black to move. FEN:
r3k2r/p5pp/b1p1p3/3pN3/4n3/BQp2P2/P5PP/qN2R1K1 b kq - 2 17
engine_lab's largest win-% drop: game 7 (French Advance, SF nodes=1, lab Black, result 0-1):
ply 34 Bc4 eval -3.37 -> +10.00 (SF19 d12), best was c2 (c2 Nxc6 cxb1=Q Rxb1 Qc3 fxe4)
check at depth 20: before -6.19 (best c2), after Bc4 #3
engine_lab re-search with 5,000 nodes: Bc4 -4.74 depth 2; PV Bc4 Qb7 c5
Black is up the exchange and three pawns, with a queen on a1 deep in White's camp; 17...c2 (−6.19, depth 20) simply wins. But 17...Bc4?? attacks the white queen and walks into mate in three — Stockfish at depth 20 sees it at once. Why did our engine not? Its re-search shows the reason: with 5,000 nodes in a position full of checks and captures, it completed only depth 2, and quiescence search follows captures, not quiet mating moves. The mate lay beyond its horizon. The postscript is instructive: the one-node Stockfish did not find the mate either, and engine_lab won the game. Shallow engines, like tired humans, blunder in pairs.
At 100 nodes Stockfish produced the most spectacular error of the match against us, in the Queen's Gambit Declined with engine_lab as White:

♟️ Position: White Kg1, Qd1, Ra1, Rf1, Bd3, Bf4, Nc3, Nd4, a2, b2, e4, f2, g2, h2; Black Ke8, Qd8, Ra8, Rh8, Bb7, Bf8, Ne5, Nf6, a6, b5, e6, f7, g7, h7 — Black to move. FEN:
r2qkb1r/1b3ppp/p3pn2/1p2n3/3NPB2/2NB4/PP3PPP/R2Q1RK1 b kq - 2 12
engine_lab win vs SF nodes=100 (QGD, lab White, 53 plies, checkmate): Stockfish's worst move ply 24
Qxd4 -1.21 -> +7.08 (d12); best Nxd3
game: ... 12.Bf4 Qxd4 13.Bxb5+ axb5 14.Qxd4 Rd8 15.Qxe5 Nd7 16.Qxb5 Ba6 17.Qxa6 e5 18.Be3 g5
19.Bxg5 Rb8 20.Bf6 Rg8 21.Qc6 Rxg2+ 22.Kxg2 h6 23.Rfd1 Bd6 24.Rxd6 Kf8 25.Qxd7 Re8 26.Qxe8+ Kxe8 27.Rd8#
12...Qxd4 grabs a knight but overlooks 13.Bxb5+, a check that uncovers the white queen on d1 against the black queen on d4 — a discovered attack of exactly the kind Chapter 9 catalogues. At 100 nodes Stockfish never searched deep enough to see the check-then-capture; engine_lab's captures-first search, which looks at every capture and check reply, played it at once and mated on move 27.
⚠️ Common pitfall: turning a node-limited engine into a "rating". "engine_lab scores 58% against one-node Stockfish, so it is a 1500" is meaningless: node-limited engines make a different kind of error from humans (they never miss a one-move capture and never understand a fortress), and our 12-game intervals are 40–50 points wide. If you want a rating, you need a pool of calibrated opponents and many games (Chapter 30).
How much does extra search buy? code/03_eval_vs_stockfish.py gave engine_lab 2,000, 8,000 and 32,000
nodes in each of 80 real middlegame positions (from the same 240 as Section 34.5) and let Stockfish 19 at
depth 12 judge the move:
=== Part 2: move quality vs node budget (80 middlegame positions, judged by SF19 d12) ===
n median_depth acpl median_cpl same_as_sf blunder_share
budget
2000 80 2.0 81.3 41.0 30.0 8.8
8000 80 3.0 64.4 27.5 33.8 7.5
32000 80 4.0 52.0 24.0 36.2 2.5

📊 Figure (described): For 80 real middlegames, engine_lab's average centipawn loss (left axis) falls from 81 to 64 to 52 as its budget grows from 2,000 to 32,000 nodes (median depth 2 → 3 → 4), while the share of moves identical to Stockfish's choice rises only from 30% to 36%. Each extra ply mostly removes blunders (8.8% → 2.5% of moves) rather than finding the best move.
That last point is worth carrying to your own games: the first plies of calculation are about not losing material; finding the best move needs judgment the search cannot supply at this depth.
🔄 Check your understanding 1. Why did we limit Stockfish by nodes rather than by time? 2. engine_lab scored 58.3% against one-node Stockfish. Give the 95% interval and say what it means. 3. In the lab blunder position, why did quiescence search not save engine_lab?
Answer
- A node limit is reproducible on any machine and independent of other programs running at the same time; a time limit depends on hardware and load, so the same match would not replay identically.
- 32–81%: twelve games are consistent with engine_lab being clearly weaker, equal, or clearly stronger.
- Quiescence follows captures (and check evasions); the losing line began with quiet checking and mating moves beyond depth 2, so it was never examined.
34.7 How Stockfish goes deep: pruning, not just speed
Stockfish 19 is faster than engine_lab — the book's reference measurement on this machine records 448,904 nodes per second on one thread from the start position, against engine_lab's median of about 12,200 in our runs, a factor of roughly 37. But speed is not the main story. Look at how many nodes each needs to finish a given depth:
=== Effective branching factor (median of nodes(d)/nodes(d-1)) ===
minimax depth 2->3: 40.13
ab depth 3->4: 8.92
ab_captures depth 4->5: 12.58
ab_full depth 4->5: 7.54
lab_full depth 4->5: 3.86
stockfish19 depth 9->10: 1.59
stockfish19 depth 19->20: 1.43
stockfish19 geometric EBF depth 10->20: 1.56 per ply; median nodes to depth 20: 791,264

📊 Figure (described): Median nodes needed to complete each search depth in the 12 OTB middlegames: engine_lab's full search (tables, quiescence, alpha-beta with ordering) climbs steeply and needs 88,482 nodes for depth 5; Stockfish 19 needs 1,096 nodes for depth 5 and 791,264 for depth 20. The slope of each line is the effective branching factor.
📐 Definition — effective branching factor (EBF). The factor by which the work grows for each extra ply of nominal depth: nodes(d) / nodes(d − 1), or (nodes(d₂)/nodes(d₁))^(1/(d₂−d₁)) over a range. Plain minimax has an EBF equal to the number of legal moves (about 40 here); perfect alpha-beta about its square root (about 6).
Stockfish's EBF of about 1.56 is far below even the perfect-ordering alpha-beta figure. That is only possible because Stockfish does not search full width: it reduces and prunes moves that look unpromising and spends the saved effort on the few that matter — Shannon's "Type B" idea, smuggled into a Type A engine. The main techniques, described in its source code and documentation, are:
- Iterative deepening with a transposition table: search depth 1, 2, 3…, reusing each iteration's best
moves to order the next — the same idea as our
ab_full, done very well. - Null-move pruning: "if I pass and my position is still too good for the opponent to allow, I needn't search this deeply" — the same null-move trick Chapter 7 used to find threats, turned into a pruning rule.
- Late-move reductions: moves sorted late (quiet, unpromising) are searched to a reduced depth first and only re-searched fully if they surprise.
- Futility and other forward pruning: near the leaves, moves that cannot plausibly raise the score enough are skipped.
- Extensions for checks and singular moves, so forcing lines are followed deeper — Chapter 10's "follow the forcing moves" in machine form.
- NNUE evaluation (Section 34.9), which makes each node's judgment far better than ours.
⚠️ Common pitfall: comparing "depth" across engines. Stockfish's depth 20 is a nominal depth: many lines are searched much shallower, some much deeper (its "selective depth" is higher). Our engine's depth 5 is full width plus quiescence. When Chapter 23 shows that Stockfish's verdict can change between depth 6 and depth 16, it is because pruned moves at low depth get their full search only at higher depth.
📋 Try it — Engine analysis. Use
chess.engineto run Stockfish 19 to depth 20 on the start position with one thread and a fresh hash, recording the nodes reported at depths 10 and 20. Compute the geometric EBF over that range.
Answer
Our run (
02_search_nodes.py) recorded 5,728 nodes at depth 10 and 381,392 at depth 20 from the start position: (381,392 / 5,728)^(1/10) ≈ 1.52 — almost the same as the 1.56 median over the 12 OTB middlegames. Your counts may differ slightly with another hash size or thread count; multi-threaded searches are not deterministic.
34.8 AlphaZero, Monte Carlo tree search and Leela
For twenty years after Deep Blue, progress meant better alpha-beta engines with better hand-tuned evaluations. Then, in December 2017, DeepMind posted a preprint describing AlphaZero, peer-reviewed in Science in December 2018 (Silver et al.). It broke almost every convention at once:
- No human knowledge beyond the rules. AlphaZero began with random play and learned only from games against itself (self-play reinforcement learning). No opening book, no endgame tables, no hand-written evaluation terms.
- A neural network instead of an evaluation function. One deep network looked at the position and produced two outputs: a policy (how promising each legal move looks) and a value (the expected result from here).
- Monte Carlo tree search (MCTS) instead of alpha-beta. Rather than search every move to a fixed depth, AlphaZero grew a tree selectively, one simulation at a time, spending its visits on the moves the policy and the accumulating values said were best. It evaluated far fewer positions per second than Stockfish and relied on each evaluation being much better.
The preprint's headline was that after about four hours of training (in its own setup) AlphaZero surpassed Stockfish 8, and it reported a decisive match win. The match conditions were criticised — a fixed time per move, and Stockfish 8 settings (hash size, no opening book) that some developers argued handicapped it — and the 2018 Science paper reported a larger, revised set of matches. Both points deserve to be held together: the 2017 comparison was not a fair championship, and the approach was a genuine breakthrough that changed engine development for good.
📐 Definition — MCTS with PUCT. Each simulation walks down the tree from the root, at each node choosing the child that maximises Q + c · P · √N / (1 + n): Q is the child's average result so far, P the policy prior, N the parent's visit count, n the child's. At a new leaf the network's value is backed up the path, flipping sign at each ply. After the simulations, the most-visited root move is played.
We cannot train a network in a chapter, but we can see the shape of the search. engine_lab.mcts is a
toy PUCT search with no network: its "policy" is a crude prior that favours captures and checks, and its
"value" at a leaf is our quiescence evaluation passed through the book's win-% curve. We gave it the
Opera Game position from Section 34.5:

♟️ Position: White Kc1, Qb3, Rd1, Bg5, a2, b2, c2, e4, f2, g2, h2; Black Ke8, Qe6, Rh8, Bf8, Nd7, a7, e5, f7, g7, h7 — White to move. FEN:
4kb1r/p2n1ppp/4q3/4p1B1/4P3/1Q6/PPP2PPP/2KR4 w k - 0 16
100 simulations: Qxe6+ 96 (96%, q=0.322), Rxd7 3 (3%, q=0.248), Bd8 0 (0%, q=0.0), Be7 0 (0%, q=0.0)
400 simulations: Qxe6+ 393 (98%, q=0.409), Rxd7 5 (1%, q=0.249), Qb8+ 1 (0%, q=0.027), Bd8 0 (0%, q=0.0)
1600 simulations: Qxe6+ 487 (30%, q=0.387), Qa4 197 (12%, q=0.46), Kd2 151 (9%, q=0.459), Qb5 115 (7%, q=0.456)
6400 simulations: Qb8+ 4673 (73%, q=0.98), Qxe6+ 487 (8%, q=0.387), Qa4 225 (4%, q=0.449), Kd2 193 (3%, q=0.448)
engine_lab alpha-beta: Qb8+ #2 depth 3, 4,142 nodes; PV Qb8+ Nxb8 Rd8#
Stockfish 19 depth 20: #2 best Qb8+; PV Qb8+ Nxb8 Rd8#

📊 Figure (described): Share of root visits for the four most-visited moves in four independent MCTS runs (100, 400, 1,600 and 6,400 simulations). With few simulations the search pours its visits into the queen trade 16.Qxe6+, which its capture-loving prior likes; only at 6,400 simulations has it explored the queen sacrifice 16.Qb8+ enough to discover the mate, after which 73% of all visits go there (average value 0.98).
This little experiment shows both the strength and the weakness of MCTS. The strength: once the search tries 16.Qb8+, the values back up decisively and the visits pile on. The weakness: whether it tries a move at all depends on the policy prior, and our prior thought a queen sacrifice was a poor candidate. Alpha-beta, which looks at every move, found the mate with 4,142 nodes at depth 3. AlphaZero's answer to this weakness was its learned policy — a network that, after millions of self-play games, had learned which "unlikely" moves are worth a look. A good policy is the machine version of a strong player's intuition about candidate moves (Chapter 10).
Leela Chess Zero (Lc0) brought the idea to everyone. It is an open-source engine, started soon after the AlphaZero preprint, that
follows AlphaZero's recipe — a policy-and-value network trained by distributed self-play and searched with
PUCT-style MCTS — with networks trained by volunteers' computers. It needs a GPU to be at its best, and it
has been one of the strongest engines in the world ever since. In the TCEC games in our broadcast data it
appears as LCZero, and it plays Stockfish on level terms (Section 34.9).
🔄 Check your understanding 1. Name the two outputs of AlphaZero's network and the job each does in the search. 2. Why did our toy MCTS need 6,400 simulations to find a mate in two that alpha-beta found at depth 3? 3. State one fair criticism of the 2017 AlphaZero–Stockfish match and one reason the result still mattered.
Answer
- Policy (move probabilities: which branches to explore) and value (expected result: what a leaf is worth, replacing hand-written evaluation and random playouts).
- Its prior steered almost all early visits to the queen trade; the sacrifice was barely tried until enough simulations had accumulated for the exploration term to force it. A full-width search tries every move at the root and cannot miss it.
- Criticism: fixed time per move and Stockfish 8's settings (hash, no book) were not championship conditions. Why it mattered: a program with no human chess knowledge, trained only by self-play, reached the very top level — and its approach (and later NNUE) reshaped engine design.
34.9 NNUE: the hybrid that won, and engines playing engines
The neural revolution did not end alpha-beta; it merged with it. NNUE (Efficiently Updatable Neural Network) was invented for computer shogi by Yu Nasu in 2018. Its trick is to use a network shaped so that the first, largest layer can be updated incrementally: the inputs describe which piece stands on which square (relative to the king), a move changes only a few of them, and the engine adds or subtracts just those columns from a stored "accumulator" instead of recomputing everything. The result is a neural evaluation cheap enough to run millions of times per second on an ordinary CPU, inside a classical alpha-beta search.
NNUE entered Stockfish with Stockfish 12 in 2020, alongside the old hand-crafted evaluation, and was an immediate large improvement. With Stockfish 16 in 2023 the hand-crafted evaluation was removed entirely. The Stockfish 19 used throughout this book is therefore a Shannon Type A search skeleton with a learned evaluation: alpha-beta, aggressive pruning, and a network trained on enormous numbers of positions. It is fair to call it a hybrid of Deep Blue's search tradition and AlphaZero's learning.
What does top engine chess look like now? Our broadcast data contains 3,880 games from the Top Chess
Engine Championship (TCEC) relayed on Lichess between November 2025 and November 2026, flagged as engine
games and excluded from every human statistic in this book. code/07_tcec.py compares them with the best
human classical games in the same data:
=== Results: engine games vs elite human games ===
group games white_score draw_rate white_wins black_wins median_plies
TCEC: all games 3880 70.0 50.0 45.0 5.0 155.0
TCEC: top events 647 72.6 47.6 48.8 3.6 155.0
TCEC: hand-crafted eval 300 69.7 43.3 48.0 8.7 147.0
TCEC: 4K engines 318 57.7 38.1 38.7 23.3 126.0
Humans OTB classical, both 2700+ 374 57.2 66.8 23.8 9.4 90.0
Humans OTB classical, avg 2400+ 23354 55.0 48.3 30.8 20.9 82.0

📊 Figure (described): Results split into White wins, draws and Black wins for TCEC engine groups and for human OTB classical games. The 2700+ humans draw 67% of their games; the top engines draw fewer (48%) but White wins almost half the time and Black only 3.6% — because TCEC assigns deliberately unbalanced opening positions and has each engine play both colours.
The surprising row is the first: engines at the top draw less than 2700-rated humans, not more. The reason is the format. TCEC assigns opening positions, often chosen to give one side an edge, and in our data 40% of the games come in identifiable pairs with the same opening and colours reversed. In 22% of the top-event pairs both games were won by White — the opening, not the engine, decided the score (Case Study 2 looks at the pairs in detail). Head to head, the two engine families met 34 times in our data:
=== Stockfish (dev builds) vs LCZero, all TCEC events in the data: 34 games ===
Stockfish score 57.4% (+9 =21 -4)
Stockfish vs everyone in the data: 204 games, score 65.2%, losses 22
HCE event: 'StockfishClassical 202007311012' scored 74.1% in 29 games
Stockfish edged LCZero, +9 =21 −4 over 34 games — a small sample with most games drawn. The last line is a piece of history. TCEC ran a competition restricted to hand-crafted evaluation (HCE), and the winner in our data, at 74.1%, was an entry named "StockfishClassical" with a build stamp from July 2020 — by its name, the last generation of Stockfish's classical evaluation. Six years after NNUE, the best hand-written chess knowledge still belongs to the engine that abandoned it.
🤖 Engine check: the hand-crafted-evaluation division, in which 21 engines played each other, scored 69.7% for White and drew 43.3% of its games — a pattern close to the top division's (72.6%, 47.6%). The openings shape the results more than the evaluation method. The 4K division — engines whose program must fit in 4 kilobytes — is the only group where Black wins often (23.3%): weaker engines make more errors, and errors cancel the opening's push.
🔄 Check your understanding 1. What does "efficiently updatable" buy NNUE that a large neural network in AlphaZero's style lacks? 2. Why do top TCEC engines draw fewer games (47.6%) than 2700+ humans (66.8%) in our data? 3. Which engine won TCEC's hand-crafted-evaluation event in our data, and why is that ironic?
Answer
- Speed on a CPU: only the few inputs changed by a move are updated, so the evaluation can run inside a fast alpha-beta search at hundreds of thousands of nodes per second.
- TCEC assigns unbalanced openings and plays them with both colours; White wins 48.8% of top-event games, so many games are decided by the opening's edge rather than drawn.
- An entry named "StockfishClassical" (build stamp July 2020), at 74.1% — the strongest hand-crafted evaluation still comes from the engine that replaced it with NNUE.
34.10 What the machines taught humans
🧩 Productive struggle. Before reading: if a self-taught engine changed how grandmasters play, what would you expect to find in the data? Write down one measurable prediction — something you could count in master games before and after 2017.
The most careful attempt to translate AlphaZero into human lessons is Matthew Sadler and Natasha Regan's Game Changer (2019). Sadler, a grandmaster, and Regan, a women's international master, studied a large set of AlphaZero's games and organised what they saw into themes: AlphaZero valued piece activity and mobility over material, played long-term sacrifices whose compensation was positional rather than tactical, restricted the opponent's pieces relentlessly, and — the theme readers remember — marched its rook's pawn up the board (h4–h5, sometimes on to h6) to gain space and pry open the enemy king, even with its own king castled on the same side.
Read Game Changer fairly: it is an interpretation of one engine's games by two strong players, not a controlled study, and several of its themes were known to strong humans long before 2017 (Chapter 4's data shows the h-pawn storm rising decade by decade since the 1970s). But it is also the most concrete account we have of what a learned evaluation "likes", and it gives us a measurable prediction to test: after AlphaZero, did elite players push the h-pawn more?
code/08_h_pawn_years.py counts, in master games from slow events between players both rated 2600+ who
had both castled kingside, the share of games in which White played h5 (or Black ...h4) before ply 80:
master games 2000-2026, slow events, both 2600+, both castled O-O: 13,649
2000-2009: 11.0% [9.9-12.3] of 2,566 games
2010-2017: 12.8% [11.8-13.9] of 4,018 games
2019-2026: 15.9% [15.0-16.8] of 6,611 games
storming side's score (all years): 54.6% over 1,915 games (White-storms 1087, Black-storms 828)

📊 Figure (described): Share of O-O vs O-O master games (both players 2600+, slow events) in which a side pushes its h-pawn to the fifth rank, by year with 95% intervals; the dashed line marks the AlphaZero preprint (December 2017). The share drifts up from about 11% in the 2000s to 12.8% in 2010–2017 and 15.9% in 2019–2026; the intervals of the last two periods do not overlap.
The rise is real in this data — 12.8% to 15.9%, with non-overlapping intervals — and it continues a trend that was already under way. What it does not show is why. Players in 2019–2026 had AlphaZero's published games and Game Changer, but they also had NNUE engines, which share the taste for such pawn advances, and a decade more of the trend Chapter 4 found. The sources in our master-games collection also change over time (more modern games, more of some players). The honest conclusion: elite players push the h-pawn more than before, in line with what Game Changer described; the data cannot separate AlphaZero's influence from engines in general, or from fashion. The storming side scored 54.6% over all years — the idea works on average when strong players choose it, which is not the same as "it works".
Here is the idea at the very top of the game, in the highest-rated decisive classical game in our broadcast data with such a storm: Fabiano Caruana vs Hikaru Nakamura, Candidates Tournament 2026, round 1.

♟️ Position: White Kg2, Qg4, Rf1, Bd5, a4, c4, d3, f2, g3, h4; Black Kg8, Qf6, Re8, Bc5, a5, b6, d4, f7, g7, h7 — White to move. FEN:
4r1k1/5ppp/1p3q2/p1bB4/P1Pp2QP/3P2P1/5PK1/5R2 w - - 7 30
depth 20: before h5 +1.12 (best h5), after +1.04; PV h6 Rh1 Qg5 Qd1 Qf6 Rh4
depth 24: before h5 +1.05 (best h5), after +0.77; PV h6 Rh1 Qg5 Qd7 Qe7 Qb5
multipv 3 depth 20: h5 +1.24; Rh1 +1.03; Qd7 +0.65
🤖 Engine check: 30.h5 is Stockfish 19's first choice (single-line search: +1.12 at depth 20, +1.05 at depth 24; in a multi-PV 3 search it also ranks first, +1.24 ahead of 30.Rh1 +1.03). The human explanation is pure Game Changer: White's bishop on d5 already dominates the light squares around f7; the h-pawn, supported by the queen on g4, takes the g6-square away from Black's pieces and prepares Rh1 and h6, so Black's king can never feel safe even though no direct attack is coming. Caruana kept the pressure for another fifty moves, broke through on f7 (49.Rxf7) and won the ending that followed.
📋 Try it — Analyze this position. In the diagram above, cover the text and list Black's weaknesses before White's 30th move. Then explain why 30.h5 does not weaken White's own king on g2.
Answer
Black's weak points are f7 (pinned against the king by the d5-bishop's pressure), the light squares around the black king, the b6-pawn and the d4-pawn that needs the c5-bishop's protection. White's king is safe because Black has no light-squared bishop, no knights and only one rook: nothing can exploit the g3/h4 squares behind the advancing pawn, and the queen on g4 covers both. A wing pawn advance is safe exactly when the opponent lacks the pieces to hit the squares it leaves behind — the engine's "aggressive" pawn move rests on a very classical calculation.
So, are engines creative? The Thursday club argued about it.
Sam: It's arithmetic, Dev. Very fast arithmetic. Creativity means doing something nobody taught you.
Dev: Nobody taught AlphaZero to push the h-pawn. It played itself and found it. That's more than I can say for half my openings.
Sam: It found what wins. A river finds the sea, too. I wouldn't call it a poet.
Lena: But when Deep Blue sacrificed that knight in 1997, Kasparov was the one who was surprised.
Sam: Surprise isn't creativity. My grandson surprises me every week.
Dev: Then what is it? If a person had played 30.h5 against Nakamura, you'd call it a fine idea.
Sam: Because a person would have to understand why it works to play it. The engine never explains.
Lena: So the creativity happens when we work out the why?
Lena's question is the best answer this chapter can give. The engine supplies a move and a number; the understanding — the light squares, the missing defenders, the king that cannot get comfortable — is something you build. That is the theme of this whole book: the engine is a tool, not a teacher.
⚠️ Common pitfall: copying engine-style moves without the engine's reasons. A club player who reads Game Changer and pushes h4–h5 in every game will weaken his own king in the many positions where the opponent does have the pieces to punish it. Test the preconditions — the opponent's available defenders and attackers — before you play the "AlphaZero move".
Spaced Review
Answer each from memory before checking.
- From Chapter 10: a middlegame position has about 35 legal moves but only about 6 forcing ones. Which part of an engine plays the role of the forcing-move scan, and which part plays the role of "finish every forcing line"? (Move ordering with captures and checks first; quiescence search and check extensions.)
- From Chapter 23: Stockfish's evaluation can change by a pawn or more between depth 6 and depth 16 in a real fraction of positions, and single-PV and multi-PV searches can rank near-equal moves differently. Where did you see both effects in this chapter? (The Deep Blue game 6 numbers at depths 16 and 24; the Caruana–Nakamura 30.h5 evaluated single-line and multi-PV; the TCEC exchange sacrifice in Case Study 2.)
- From Chapter 3: "a knight on d5 touches eight squares, on a1 two". How did engine_lab turn that sentence into a number, and why did such static knowledge add so little (r rose from 0.796 to 0.812) compared with three plies of search?
- From Chapter 32: ACPL depends on the judge's depth, the positions and the opponents. Why should engine_lab's ACPL of about 45–57 in its match not be compared with human ACPL tables?
Project Checkpoint: engine_lab.py
Dev's Outpost toolkit has so far used Stockfish — for reviews, accuracy, prep. This chapter he builds an
engine of his own, not to compete with Stockfish but to understand what the number on the screen is made
of. The module is project/outpost/engine_lab.py; it depends only on python-chess, so it runs anywhere.
Public functions and classes:
perft(board, depth, mode="legal"),perft_divide(board, depth, mode),perft_breakdown(board, depth)— move-generator tests, with two deliberately buggy modes for teaching.material_eval(board),evaluate(board),eval_terms(board),game_phase(board),table_as_grid(piece_type, phase)— the evaluation and its parts.minimax(board, depth, eval_fn)— plain search with node counting.Searcher(alphabeta, ordering, quiescence, tt, eval_name)with.search(board, depth, nodes, seconds)→SearchResult(move, score, pv, depth, nodes, qnodes, seconds, score_white, by_depth); every feature is a switch, so its value can be measured.count_nodes(board, depth, variant)wraps the comparisons of 34.4.mcts(board, simulations, c_puct, seed)— the toy PUCT search of 34.8.lab_player(...),stockfish_player(sf, limit),play_game(white, black, start_moves, max_plies),score_for(result, lab_is_white)— match play;summary(board, depth, nodes)— a one-line report.
The heart of it is the alpha-beta loop with ordering, a transposition table and killer moves:
for m in self._order(board, moves, ply, hash_move):
board.push(m)
v = -self._negamax(board, depth - 1, -beta, -alpha, ply + 1)
board.pop()
if v > best:
best, best_move = v, m
if v > alpha:
alpha = v
if alpha >= beta: # beta cut-off: the opponent will avoid this line
if not board.is_capture(m):
remember_killer(ply, m) # quiet moves that cut off are tried early in sibling nodes
break
self.tt[key] = (depth, best, bound_flag, best_move)
code/project_checkpoint.py runs the self-tests and a short game:
1) perft self-test
start: [20, 400, 8902, 197281]
Kiwipete: [48, 2039, 97862]
2) static evaluation terms, Opera Game before White's 16th move (White POV, cp)
{'material': -100, 'pst': 14, 'bishop_pair': 0, 'phase': 0.306, 'total': -86}
3) search reports
engine_lab: best Qb8+ #2 (depth 3, 172 nodes + 3,970 q-nodes, 17,490 nodes/s); PV Qb8+ Nxb8 Rd8#; ...
engine_lab: best e4 +0.00 (depth 4, 620 nodes + 2,075 q-nodes, 13,856 nodes/s); PV e4 d5 Nc3 Nf6; ...
4) engine_lab (3,000 nodes/move) vs Stockfish 19 (1 node/move), Italian Game, engine_lab White
result 0-1 (checkmate), 32 plies; engine_lab score 0.0
first 24 plies: e4 e5 Nf3 Nc6 Bc4 Bc5 Nc3 d6 O-O Bg4 h3 Bh5 g4 Bg6 g5 Bh5 d3 Nd4 Bb5+ c6 Bxc6+ Nxc6 a4 Nge7
The last line is a useful humbling: with 3,000 nodes instead of the match's 5,000, engine_lab lost the Italian to one-node Stockfish in 16 moves after weakening its kingside with g4–g5. Budget matters, and one game proves nothing — which is why the match used 36.
🎭 Composite data: this chapter uses no persona games — Dev's "engine" is code, and every game in it is engine_lab's own. When later chapters run engine_lab on Dev's games, those are real Lichess games played by anonymous players at his rating, assembled into one "player" to demonstrate the method.
How it plugs in later: Chapter 35 does not need a
search engine, but its dashboards reuse SearchResult.score_str()'s convention (White's point of view, two
decimals, #N for mates). Chapter 40's command line exposes python -m outpost perft and
python -m outpost lab <fen> as self-tests and teaching demos, and Exercise E1 turns Searcher into a
testbed for your own engine ideas.
Chapter Summary
| Idea | What it is | What we measured (this chapter's runs) |
|---|---|---|
| perft | count legal move sequences to test move generation | start 20 / 400 / 8,902 / 197,281 / 4,865,609; a missing en passant rule is invisible from the start position to depth 4 |
| Minimax | back up evaluations assuming best play | Kasparov–Topalov depth 3: 81,018 nodes |
| Alpha-beta | skip moves that cannot change the result | same value, 1,832 nodes with captures first (44× fewer); ordering worth 3.9× (d3) and 8.5× (d4) |
| Quiescence | search captures past the depth limit | removes the +9 / 0 / +6 / 0 oscillation; 93.5% of our nodes at depth 5 |
| Evaluation | material + tables + bishop pair | r = 0.812 with Stockfish (static) vs 0.901 after 3 plies; level material 0.325 vs 0.751 |
| Pruning | search unpromising moves shallowly | Stockfish 19 EBF ≈ 1.56; depth 20 in a median 791,264 nodes |
| Our match | engine_lab 5,000 nodes vs Stockfish 1 / 100 / 1,000 nodes | 58.3% / 33.3% / 12.5%, 12 games each, intervals ~40–50 points wide |
| MCTS | selective tree growth guided by policy and value | toy search found 16.Qb8+ only at 6,400 simulations (73% of visits) |
| NNUE | incrementally updated neural evaluation inside alpha-beta | Stockfish 12 (2020) → hand-crafted eval removed in Stockfish 16 (2023) |
| Engines vs humans | TCEC in the broadcast data | top-event draw rate 47.6% vs 66.8% for 2700+ humans; Stockfish–LCZero +9 =21 −4 |
| AlphaZero's influence | h-pawn storms in 2600+ master games | 12.8% (2010–17) → 15.9% (2019–26), cause not separable |
Decision checklist — using the ideas at the board:
- Order your candidates like an engine: checks, captures, threats first (Chapter 10).
- Finish every exchange before you judge the position — never evaluate in the middle of a capture sequence (quiescence).
- Prune with a reason: dismiss a move only when you have found one clear refutation (alpha-beta).
- Evaluate the quiet position with structure, activity, king safety and material — the engine's evaluation terms are Part I's checklist.
- Read the engine's number with its depth and mode (single-PV or multi-PV), and ask why before you copy a move.
What's Next
You now know what is inside the number your engine shows: a search that skips most of the tree, an evaluation learned from millions of positions, and the depth at which it stopped. Chapter 35 turns from the engine back to you — tracking your own progress over time, finding plateaus, and asking what the data can and cannot say about how fast you will improve.