Chapter 34 Instructor Notes: Chess AI
Chapter text · Case study 1 · Case study 2 · Exercises · Quiz · Key takeaways
This is the Developer Track's home chapter and the one most students arrive excited about. It does two things at once: it builds a small engine (engine_lab.py) and measures every idea — perft, minimax, alpha-beta, ordering, quiescence, evaluation — and it tells the real history from Shannon (1950) through Deep Blue, AlphaZero, Leela and NNUE, checking the famous stories against the book's data. The hook is Deep Blue–Kasparov, New York 1997, game 6 (7...h6 8.Nxe6), which Stockfish 19 endorses. The closing dialogue asks whether engines are creative, and the chapter's answer is Lena's: the creativity happens when we work out the why.
History facts to keep exact in class: Kasparov won the 1996 match (Philadelphia) 4–2; Deep Blue won the 1997 rematch (New York) 3½–2½. The AlphaZero preprint appeared in December 2017 and the peer-reviewed paper in Science in December 2018; the "four hours" refers to the preprint's training time in its own setup, and the 2017 match conditions were criticised. NNUE came from computer shogi (Yu Nasu, 2018), entered Stockfish with Stockfish 12 (2020), and Stockfish 16 (2023) removed the hand-crafted evaluation. No persona games are used in this chapter.
Learning objectives
By the end, students should be able to:
- Describe every engine as search + evaluation, using Shannon's design (move generation, game tree, evaluation at the leaves, backed-up scores).
- Test a move generator with perft, and explain why rich positions (Kiwipete) catch bugs the start position hides.
- Explain alpha-beta as "one refutation is enough", measure what move ordering buys, and say why alpha-beta returns the minimax value exactly.
- Explain the horizon effect and how quiescence search fixes it — and map both onto human calculation ("finish the exchange").
- Measure an evaluation function against Stockfish and see that a little search beats a lot of static knowledge.
- Explain how Stockfish goes deep by pruning (effective branching factor ≈ 1.56), and how AlphaZero's policy/value network and MCTS differ from alpha-beta.
- Read engine history and engine-vs-engine data fairly: six-game matches, TCEC's assigned openings, and what the h-pawn trend can and cannot show.
Timing plan
75-minute session
| Minutes | Activity | Notes |
|---|---|---|
| 0–10 | Hook: 1997 game 6 | Board demo below. Ask the class to predict Stockfish 19's view of 8.Nxe6. |
| 10–20 | 34.1 the matches | 4–2 then 3½–2½; ACPL table (1997: Deep Blue 15.8, Kasparov 19.0). The "Deep Blue beat Kasparov" pitfall. |
| 20–30 | 34.2 perft | 20 / 400 / 8,902 / 197,281 / 4,865,609; the no-en-passant bug invisible from the start to depth 4. |
| 30–45 | 34.4 alpha-beta and the horizon | KT 1999: 81,018 vs 1,832 nodes; +9 / 0 / +6 / 0 without quiescence. |
| 45–55 | 34.5–34.7 | r 0.812 static vs 0.901 depth 3; EBF 1.56. |
| 55–68 | 34.8–34.10 | AlphaZero/Leela/NNUE in five minutes; TCEC draws 47.6% vs 66.8%; the "are engines creative?" debate. |
| 68–75 | Exit ticket |
3-hour block
| Minutes | Activity | Notes |
|---|---|---|
| 0–15 | Hook and the Deep Blue story | Read the Sam/Dev/Lena dialogue. |
| 15–30 | 34.1 | The ACPL table by game; Case Study 1's game 2 questions (37.Be4, 45...Qe3). |
| 30–80 | Lab: build and measure the toy engine | See the lab plan under "Running the code". |
| 80–90 | Break | |
| 90–110 | 34.5 evaluation | Level-material r 0.325 static vs 0.751 depth 3; the Opera Game static eval −0.86. |
| 110–125 | 34.6 the match | Intervals 32–81%; both sides blunder (17...Bc4?? and 12...Qxd4?). |
| 125–140 | 34.7–34.9 | EBF; MCTS on the Opera Game (6,400 simulations to find 16.Qb8+); NNUE. |
| 140–160 | Case Study 2: TCEC pairs | Stockfish vs LCZero 13 pairs; 22% of top pairs won by White twice. |
| 160–175 | 34.10 and the creativity debate | Caruana–Nakamura 30.h5; the h-pawn trend. |
| 175–180 | Exit ticket |
Key numbers and games
| Item | Value as quoted | Where |
|---|---|---|
| Matches | 1996 Kasparov 4–2 (wins K 3, DB 1, draws 2); 1997 Deep Blue 3½–2½ (wins DB 2, K 1, draws 3) — recomputed from the twelve games | §34.1 |
| 1997 game 6 | Before 7...h6 +0.40 (d24, best 7...Bd6); after +0.98 (d24, best 8.Nxe6); MultiPV d20 Bd6 +0.42, Be7 +0.57, Bb4+ +0.58; final +6.42 (d20); 37 plies | Hook; CS1 |
| ACPL, SF19 d16 moves 9+ | 1996 DB 17.4 / Kasparov 14.1; 1997 DB 15.8 / Kasparov 19.0; Kasparov 1997 g2 44.2, g6 44.9 | §34.1; CS1 |
| 1997 game 2 | 37.Be4 first at d20 and d24 (+1.29 / +1.03 MultiPV; +1.19 single d24); 37.Qb6 +0.44; after 45.Ra6, 45...Qe3 best: +0.55 (d20), +1.31 (d24), +1.11 (d30); MultiPV d24 Qe3 +1.10, Qxc6 +5.31, Qd8 +7.02; 45.Ra6 dropped +4.56 → +1.24 (d16) | CS1; B6, B8 |
| perft | Start 20 / 400 / 8,902 / 197,281 / 4,865,609; Kiwipete 48 / 2,039 / 97,862 / 4,085,603; pseudo-legal start d4 +461; no-e.p. bug invisible from the start to d4, Kiwipete d2 −1 | §34.2 |
| Kasparov–Topalov 1999 nodes | d3 minimax 81,018, alpha-beta 10,481, captures first 1,832 | §34.4 |
| Ordering (12 OTB middlegames) | Minimax/captures-first 34.5× at d3; ordering alone 3.9× (d3), 8.5× (d4); 69/70 root values identical | §34.4 |
| Horizon | Material-only, no quiescence: +9 / 0 / +6 / 0 / +1 at d1–5; with quiescence 0.00; SF19 d20 −0.33 with 24.Rxd4 (engine_lab plays 24.Qxd6) | §34.4 |
| Horizon blunder (OTB 2026, 1586 vs 1463) | 35...Rgd7 0.00 (d16); depth-2 no-quiescence 35...Rxd6? → 36.Qxg7 +4.91 | §34.4; B2 |
| Quiescence share | 93.5% of nodes at depth 5 | §34.4 |
| Evaluation fit (240 positions vs SF19 d12) | r material 0.796, static 0.812, quiescence 0.869, depth-3 0.901; level material static 0.325 vs depth-3 0.751; opening static 0.17 | §34.5 |
| Node budget (80 middlegames) | 2k / 8k / 32k nodes: ACPL 81.3 / 64.4 / 52.0; same as SF 30.0 / 33.8 / 36.2%; blunders 8.8 / 7.5 / 2.5% | §34.6 |
| Match (engine_lab 5,000 nodes vs SF19 N nodes) | N = 1: 58.3% (+3 =8 −1) [32–81]; N = 100: 33.3% (+1 =6 −5) [14–61]; N = 1,000: 12.5% (+0 =3 −9) [3–40] | §34.6 |
| Match blunders | engine_lab 17...Bc4?? (#3) vs 17...c2 (−6.19 d20); SF-100 12...Qxd4? 13.Bxb5+ (+7.08 d12) | §34.6; B3–B4 |
| Speed and EBF | SF19 448,904 nps vs engine_lab ≈12,200 (≈37×); SF19 EBF ≈ 1.56 (d10–20), median 791,264 nodes to d20; engine_lab ≈ 3.9–4.2 | §34.7 |
| MCTS (Opera Game) | Toy PUCT needs 6,400 simulations to find 16.Qb8+ (73% of visits); alpha-beta #2 at depth 3 in 4,142 nodes | §34.8 |
| TCEC in our data | 3,880 games; top events (647) White 72.6%, draws 47.6%, White wins 48.8%, Black 3.6%; humans OTB classical both 2700+ (374) draws 66.8%; 22% of top pairs won by White twice | §34.9; CS2 |
| Stockfish vs LCZero | 34 games, SF 57.4% (+9 =21 −4); 13 pairs: 5 won by SF, 8 level; HCE event won by "StockfishClassical" (74.1%, 29 games) | §34.9; CS2 |
| h-pawn storms (both 2600+, both O-O) | 2000–09 11.0%; 2010–17 12.8% [11.8–13.9]; 2019–26 15.9% [15.0–16.8]; stormer 54.6% (n = 1,915) | §34.10 |
| Caruana–Nakamura, Candidates 2026 r1 | 30.h5 +1.12 (d20) / +1.05 (d24); MultiPV d20 h5 +1.24, Rh1 +1.03, Qd7 +0.65 | §34.10; B5 |
Threshold concepts and likely misconceptions
Threshold concept: search for the concrete, evaluate the quiet. A static evaluation cannot see a forced line (the Opera Game reads −0.86 for White one move before mate in two); a search that stops mid-exchange lies (+9 / 0 / +6 / 0). Students who cross this threshold see that the engine's number is the output of a procedure — and that strong human thinking has the same division of labour.
Misconceptions to expect:
- "Deep Blue beat Kasparov." It lost the first match 4–2 and won the second 3½–2½; six games each, far too few to say who was stronger.
- "Alpha-beta is an approximation." It returns exactly the minimax value; it only skips work. (The one disagreement in 70 cells was a transposition-table artefact, not alpha-beta.)
- "Stockfish is strong because it is fast." It is about 37× faster per node, but its EBF of 1.56 — selective pruning — is what reaches depth 20 in under a million nodes.
- "Depth 20 means the same in every engine." Stockfish's depth is nominal and selective.
- "Material explains 80% of chess." The r = 0.80 comes from big imbalances; with level material the static evaluation's r is 0.325.
- "AlphaZero beat Stockfish in four hours, end of story." Hold both: the 2017 conditions were criticised and the approach was a genuine breakthrough.
- "Engines draw everything." Top TCEC events draw 47.6% — less than 2700+ humans (66.8%) — because TCEC assigns unbalanced openings.
- "AlphaZero caused the h-pawn trend." The trend (12.8% → 15.9%) is real, but the data cannot separate AlphaZero from engines in general or from a trend already under way.
- "engine_lab is a 1500 player." Node-limited engines make different errors from humans; a 12-game interval is 40–50 points wide.
How the four tracks use this chapter
- ♟️ Improver: skip code, keep diagrams. 34.1, the horizon section of 34.4 (finish every exchange), 34.8 and 34.10. The decision checklist in the key takeaways is the engine's method as a board habit. Exercises A4, A7, B1, B2, B5, D1, D2.
- 📊 Analyst: the measurements — node counts (34.4), evaluation correlations (34.5), the match with its intervals (34.6), TCEC (34.9, Case Study 2), the h-pawn trend (34.10). Exercises C2, C5, C7, M4, M5, E3.
- 🏆 Competitor: Case Study 1 (what lost Kasparov the match: two games of ACPL 44+), B5–B8 as calculation drills, and 34.6's lesson that extra plies mostly remove blunders rather than find best moves.
- 💻 Developer: this is your chapter — 34.2–34.7 are a lab manual.
engine_lab.py:perft/perft_divide/perft_breakdown,minimax,Searcher(alpha-beta, ordering, quiescence, transposition table, iterative deepening, node or time budgets),count_nodes,mcts,play_game. Exercises C1–C6 and E1 (add one feature and prove it helps).
Running the code
Scripts run from the chapter directory; outputs are saved in code/_out*.txt. Stockfish 19 is used both through the toolkit (cached) and directly through chess.engine (node-limited play and nodes-per-depth counts — use engine.ENGINE_PATH to open it). engine_lab.py depends only on python-chess, so it runs on any laptop.
| Script | What it shows | Runtime |
|---|---|---|
_ch34_common.py |
Helpers | — |
01_perft.py |
perft tables for the start, Kiwipete and position 3; buggy generators | ~35 s |
02_search_nodes.py |
Nodes by variant and depth, 12 OTB middlegames + KT 1999; SF19 nodes per depth; writes ch34_search_nodes.csv |
~4.7 min (3 processes) |
03_eval_vs_stockfish.py |
240-position correlations; move quality by node budget | ~3 min (3 processes) |
04_match.py |
36 games engine_lab vs SF19 at 1/100/1,000 nodes + d12 judging; writes ch34_match_games.pgn |
263 s for the games |
05_horizon_mcts.py |
Odd–even effect, horizon blunder, toy MCTS on the Opera Game | short |
06_deep_blue.py |
Twelve Kasparov–Deep Blue games at d16; critical positions at d20/d24 | parallel, 3 workers |
07_tcec.py, 08_h_pawn_years.py |
TCEC results and pairs; h-pawn storms by year | short |
case_study_01.py, case_study_02.py, project_checkpoint.py |
Case studies; module demo | short (cached) |
Expect 13 board images and 12 charts. The match is deterministic (one thread, cleared hash, go nodes N), so a rerun reproduces the same games. A harmless pandas FutureWarning appears in analyse_games_parallel.
Lab plan: build and measure the toy engine (50 minutes)
- perft first (10 min). Students run
perftfrom the start to depth 4 and check 197,281. Then switch tomode="no_ep": the count is still right from the start position. Run Kiwipete at depth 2: 2,039 vs one fewer. Useperft_divideto find the missing move. Lesson: test with rich positions. - Minimax vs alpha-beta (10 min).
count_nodeson the Kasparov–Topalov position at depth 3 forminimax,ab,ab_captures. Students predict the ratio before running (81,018 vs 1,832). Ask why the root value is identical. - The horizon (10 min).
Searcher(quiescence=False, eval_name="material")at depths 1–4 on the same position: +9 / 0 / +6 / 0. Turn quiescence on: 0.00. Then the OTB horizon position (k2rb3/pp4r1/3BpQ2/3pP3/3q4/P2B2P1/5P1P/1R4K1 b - - 2 35) at depth 2 both ways. - Evaluation (5 min).
eval_termson the Opera Game position before 16.Qb8+: material −100, total −86. Then search to depth 3: mate in two. - Mini-match (15 min). Pairs play two games of engine_lab (5,000 nodes) against Stockfish at
go nodes 1with reversed colours usingplay_game, then compute a Wilson interval over the whole class's games. Compare with the chapter's 58.3% [32–81]. A class of ten pairs gets twenty games — still a wide interval, which is the point.
Troubleshooting: if a budget-limited search leaves the board in a strange state, the student's copy predates the fix that unwinds pushed moves when a search is aborted — update engine_lab.py.
In-class demos
Board demo
Set up 1997 game 6 before White's 8th move: r1bqkb1r/pp1n1pp1/2p1pn1p/6N1/3P4/3B1N2/PPP2PPP/R1BQK2R w KQkq - 0 8. Ask: "Black has just played 7...h6. White to move — what does a 1997 machine play, and what would you play?" Most students retreat the knight. Reveal 8.Nxe6 and play 8...Qe7 9.O-O fxe6 10.Bg6+ Kd8. Then the numbers: before 7...h6 the position was +0.40 (depth 24) with 7...Bd6 best; after it +0.98, and Stockfish 19's first choice is the sacrifice Deep Blue played. Ask for the human explanation: ...h6 spends a tempo while the king is still in the centre, and the e6-pawn is its only cover.
Data demo
Project the match table from 34.6 without the intervals. Ask: "engine_lab scored 58.3% against one-node Stockfish. Is it stronger?" Take a vote, then reveal the Wilson interval, 32–81%. Follow with the 1,000-node row: 12.5% although engine_lab searches five times more positions per move. Close with the node-budget table: going from 2,000 to 32,000 nodes cut blunders from 8.8% to 2.5% of moves but raised agreement with Stockfish only from 30% to 36% — the first plies of calculation are about not losing material.
Exercise guidance
- Part A: everyone. A7 ("Deep Blue beat Kasparov in both matches" — false) and A8 (why a node-limited Stockfish is a good sparring partner: reproducible) are quick checks.
- Part B:
- B1 (Opera Game): 16.Qb8+! Nxb8 17.Rd8#. Ask why a static evaluation of the position is −0.86.
- B2 (horizon): students who grab 35...Rxd6 have reproduced the engine's error; 36.Qxg7 takes the other rook.
- B3–B4: both sides of the match blunder. B4's 13.Bxb5+ is a discovered attack on the d4 queen.
- B6: 45...Qe3 is the only try. The point is the depth spread (+0.55 / +1.31 / +1.11) — not a dead draw by Stockfish 19.
- B7 (1996 game 1): students should find that 32...Rg5 was better than the game's 32...Re8.
- B8 (37.Be4): ask for the human reason the quiet move beats the queen raid.
- Part C: C1–C2 are 30-minute labs; C3 (add mobility and re-measure r), C4 (rematch at 20,000 nodes) and C6 (MCTS seeds) are longer. Insist on intervals in C4.
- Part D: D2 (are engines creative?) and D3 (when the engine number misleads: engine_lab's 24.Qxd6) make strong essays. D1 asks whether humans should think like engines.
- Part E: E1 is the best Developer project in Part VI — add null-move pruning or late-move reductions and prove it helps with a match large enough to have a useful interval.
Assessment ideas
- Engine anatomy. Students label a diagram of an engine (generator, search, ordering, quiescence, evaluation) and give one measured number from the chapter for each part.
- Legend check. Give the claim "Kasparov resigned game 2 in a drawn position". Students write 200 words using only depth-stated Stockfish 19 numbers from Case Study 1 (45...Qe3 +1.10 to +1.31 at depths 24–30) and say what the numbers can and cannot settle.
- Interval exercise. Given a 7/12 match score, compute the Wilson interval and explain why engine developers test over thousands of games.
- Quiz. The chapter quiz (20 questions: 12 multiple choice, 5 true/false, 2 short answer, 1 position) fits a 25-minute slot; 18–20 is the "try E1" band.
- Developer milestone. perft matches published tables to depth 4 on Kiwipete;
count_nodesreproduces the KT 1999 depth-3 counts; one new feature with a before/after node count.