# The perfect game and its deviations

Part of [`research/chess-proof`](../README.md). Scripts are in the parent folder, results in `../data/`, figures in `../figures/`.

## The perfect game, and an attempt to break it

To solve chess is to find the perfect game and show that neither side can do better by leaving it. Played moves are
cheap; certifying them means showing that every alternative is no better. `perfect_game.py` does the engine version of
that. Stockfish 16 plays both sides at depth 24, with 4 threads. Then, in every position of the game, a multi-PV search
at depth 20 scores every legal move, to see whether any deviation is better for the side that played it.

The game is a draw by threefold repetition after 132 plies:

```
1. e4 e5 2. Nf3 Nc6 3. Bb5 Nf6 4. O-O Nxe4 5. Re1 Nd6 6. Nxe5 Be7 7. Bf1 Nxe5 8. Rxe5 O-O 9. d4 Ne8 10. d5 Bc5 11. Re1 d6 12. Nc3 Bf5 13. Bd3 Qf6 14. Ne4 Bxe4 15. Rxe4 g6 16. Rf4 Qe5 17. g3 Nf6 18. c4 Nh5 19. Rf3 Nf6 20. Kg2 a5 21. Bd2 Nd7 22. Rb1 Bd4 23. Bc2 Qg7 24. Bf4 Be5 25. Be3 f5 26. Qd2 Qf7 27. Ba4 Nf6 28. Bh6 Ne4 29. Qc2 Bg7 30. Re1 Nc5 31. Bc1 Bd4 32. Bh6 Bg7 33. Bc1 Bd4 34. h4 Nxa4 35. Qxa4 Rfe8 36. Rxe8+ Qxe8 37. Qc2 Qe4 38. Qxe4 fxe4 39. Rf4 Re8 40. Kf1 c6 41. dxc6 bxc6 42. f3 Rb8 43. Rxe4 Bxb2 44. Bxb2 Rxb2 45. Re6 Rxa2 46. Rxd6 c5 47. Rc6 a4 48. Rxc5 Rc2 49. Rc8+ Kf7 50. Rc7+ Kg8 51. Rc8+ Kf7 52. Ra8 Rxc4 53. Ra7+ Kg8 54. Ra8+ Kg7 55. Ra7+ Kg8 56. Kf2 Rc2+ 57. Ke3 Rc4 58. Ra6 Kf7 59. Rb6 Rc7 60. Ra6 Rc4 61. Ra7+ Kg8 62. Ke2 h5 63. Kf1 Rd4 64. Kg2 Rc4 65. Kf1 Rd4 66. Kg2 Rc4 1/2-1/2
```

Both sides chose the Berlin Defence of the Ruy Lopez: 1.e4 e5 2.Nf3 Nc6 3.Bb5 Nf6 4.O-O Nxe4. It is the line elite
players adopted because it neutralizes the first move's edge. White's advantage never exceeds +0.42, and from
move 46 the score is exactly 0.00, in a rook endgame that repeats. Of 3 683 alternatives tested, none is a quarter
pawn better than the move played. The largest edge any alternative had was 0.09 pawns. So Stockfish's best guess at
the perfect game is a draw, and it holds against every single-move deviation at depth 20.

That is evidence, not proof. Every alternative was judged by a search, not followed to the end with perfect play. A
certificate would follow each of the defender's deviations with a perfect answer of its own, recursively, to exact
leaves. That is the proof graph of `SOLVING_CHESS.md`: the perfect game is its spine, and the defender's deviations
are its ribs.

### One level deeper: the ribs

`ribs.py` leaves the spine at each of its first 60 turns (moves 1–30, both sides). At each turn, the side to move plays
its strongest alternative instead of the spine move. The rib is then played out by Stockfish 16 at depth 18, and
attacked like the spine: every alternative in every position, at depth 14. `ribs_recheck.py` re-searches every flagged
break at depth 22. For confirmed breaks it plays on from the better move, and the two critical positions get a depth-30
check.

- **Results:** 55 ribs are draws, 3 White wins and 2 Black wins.
  139 930 alternatives were tested inside the ribs.
- **Flagged breaks:** 155, of which 38 hold at depth 22; the rest were the
  shallower search's noise.
- **Four decisive ribs punish the deviation itself.** Each left the spine at a forced moment: White's 8.Nc3 and
  15.Bxe4 instead of recapturing, Black's 14...Qg6 instead of 14...Bxe4, and Black's 21...Ng4. The deviating side
  loses. Correcting the deviation, which means going back to the spine, gives a draw.
- **Two apparent cracks close under deeper search.**
  - In the 23.Bh6 rib White won, but only after Black's mistakes inside the rib (...f5 instead of ...Qh5, worse by
    0.85). Corrected, it is a draw. At depth 30 the position before move 23 is level: 23.Bc2 +0.17, 23.Bh6 +0.14.
  - In the 28.Qe2 rib, three White improvements late in the endgame, one of them g7 worth 31 pawns, would turn its
    draw into a White win. That advantage grew about 50 moves after the deviation, through the weaker search's play,
    and the deviation itself is level at depth 30: 28.Bh6 +0.17, 28.Qe2 +0.08.

**No deviation by either side improves on the spine.** Every decisive or changed result traces to one of two causes:
the deviation itself was a mistake, or the rib was played weaker than the spine. That is the lesson for a real
certificate: a rib counts only when it is played as perfectly as the spine, and a weaker search finds cracks that are
its own errors.

### Level three: the ribs of the ribs

`ribs3.py` takes each of the 55 drawn ribs and finds its most tempting deviation: in the rib's first 20 positions, one
search at depth 16 scores every move, and the deviation is the alternative that comes closest to the move the rib
played. Following the level-two lesson, the new rib is played out at depth 20 instead of 18, and only its first 40
positions are attacked (every alternative, depth 16). Each flagged break is re-searched at depth 24. `ribs3_check.py`
plays on from every confirmed break in a drawn rib, and checks every decisive rib's deviation at depth 30.

- **The deviations are real temptations.** The median one scores 0.16 pawns below the move played, and 23 of 55 are
  within 0.15. White deviates in 27 ribs, Black in 28.
- **Results:** 52 draws, 3 White wins, no Black wins. **The side that deviated never wins.** 67 922 alternatives
  were tested; 39 breaks were flagged and 8 hold at depth 24.
- **The three decisive ribs punish the deviation.** All three follow a Black deviation, and at depth 30 each is worse
  than the move it replaced: 17...Ne4 by 0.82 pawns (−1.17 against −0.35), 29...Be5 by 0.28 and 26...Qb2 by 0.27.
- **Six confirmed breaks sit inside drawn ribs; played on, five stay draws.** That includes both breaks that favour
  the deviating side (Qe5 and Qg4+, each about 0.7 pawns): the extra edge does not convert. The sixth, Black's
  14...Ne7 in the 3.d4 rib, turns the draw into a Black win. That is a win against the deviator, and the deviation is
  not to blame: at depth 30, 3.d4 scores +0.27 against +0.33 for 3.Nc3. The loss comes from White's piece sacrifices
  later in the rib, played at depth 20. It is the level-two lesson again.

**Three levels down, nothing breaks the spine.** 115 deviations from the spine or its ribs have been played out
(60 + 55), and 211 535 alternatives tested across the spine and both levels. No deviating side has won a game whose
result survives a deeper check. As the playouts get stronger, real breaks get rarer: confirmed breaks per alternative
tested fell from 0.027% at level two to 0.012% at level three. This is the shape a draw certificate would take. It is
still engine evidence at depth 16–30, not a proof.

### Anatomy of the perfect game: a tug of war in a flat valley

![anatomy](../figures/perfect_game_anatomy.png)

`anatomy.py` asks why leaving the spine costs, and why it is this line. The figure has five panels:

1. **The rope.** At every move of the spine, an arrow shows where the best deviation would put the score (depth 20).
   Every White deviation pulls the score down and every Black one pulls it up. No deviation helps the deviator by a
   quarter pawn, which is what an equilibrium means. The pulls are small, though: **the median cost is 0.03 pawns**.
   89 of 132 moves are nearly free (another move is within 0.1 pawns). Only **9 are forced**, where deviating costs
   half a pawn or more. Eight of those nine are captures, mostly recaptures: 8.Rxe5, 14...Bxe4, 15.Rxe4,
   35.Qxa4, 36...Qxe8, 38...fxe4, 41...bxc6 and 44...Rxb2 (plus 16...Qe5). The rope only snaps tight when material is
   in flux.
2. **The width of the path.** A median of 4 moves per position hold within a quarter pawn of the best (depth 14).
   That rises to 10–17 in the final rook endgame, where the score is exactly 0.00.
3. **The saddle.** Each row is one candidate move, and each column one of the opponent's replies, best first. A
   move is worth its worst case, the first cell, so the spine move is the row with the best first cell: the
   max of the mins. 1.e4 wins that comparison (+0.34 against +0.20 for 1.d4 and 1.Nf3), although other rows have
   bigger cells further right. At Black's third move the Berlin 3...Nf6 is 0.05 behind 3...a6 at this shallow depth,
   a near-tie that the spine's depth-24 search settled the other way.
4. **The draw basin.** 55 of 60 deviations played out are draws. Of the 5 decisive ones, 4 lose for the side that
   deviated, and the fifth (23.Bh6) is a draw once the mistakes inside it are corrected.
5. **Other perfect games.** The same engine with one thread plays a different line at each depth. Depths 14, 16 and
   18 answer 1.e4 with the Sicilian; depths 20 and 22 follow the spine's Berlin for 17 plies and then leave it. All
   five stay between −0.58 and +0.60 pawns.

**Why a deviation costs.** Part of it is true by construction: the spine move is the engine's maximum, so every
alternative scores no higher at that depth. The substantive part is the minimax theorem for finite games of perfect
information. If chess is a draw, a player who deviates against a perfect opponent can keep the draw or lose it, never
win. Levels 1–3 test that against deeper search and real play-outs, and it has held. The measured surprise is how
cheap most deviations are.

**Why it looks like a tug of war, and in what sense it is "entangled".** The score is the rope. White pulls it up,
Black pulls it down, and the perfect game is the line where neither can gain by changing only their own moves: a
saddle point, the equilibrium of a zero-sum game.

The analogy with entanglement holds in one respect. A move has no value by itself; its value is defined only together
with the opponent's best answer. Change the move, and the best answer changes with it: in panel 3a, 1.e4 is met by
...c5, 1.d4 by ...Nf6, 1.Nf3 by ...d5, and 1.c4 by ...e5. The two strategies are defined by each other, and the spine
is their fixed point.

The analogy breaks everywhere else. Nothing is in superposition, nothing is random, and the correlation is classical:
it comes from a shared rule, like two hands on one rope, not from a quantum state.

**Why exactly that line? It is not the only one.** The spine is unique only at the 9 forced moments. Everywhere else
several moves hold, and the engine's choice is a tie-break set by search depth, thread timing (the spine used 4
threads, which is not reproducible move for move) and move ordering. In the language of least action, the valley floor
is flat. The draw is a degenerate ground state, with many paths of the same value, and the engine picks one. Deeper
searches agree with the spine for longer (17 plies at depths 20 and 22, against 1 ply at depths 14–18), so in the
opening the spine behaves like an attractor. It is still one representative of a wide family.

Caveat: the width (depth 14) and the saddles (depths 14 and 12) are shallower than the spine (depth 24), so their small
differences are noise-level.
