Combinatorial Games & Sprague–Grundy
Winning positions, nim-sums and Grundy numbers.
Three heaps hold 7, 10 and 12 stones. A move takes any number of stones from one heap, and whoever takes the last stone wins. Should you move first, and how? Write the heap sizes in binary and XOR them: , which is not 0, so the first player wins, and the one winning move takes a single stone from the 7. This lesson proves why, and shows that every game of this kind is secretly a single Nim heap.
What you’ll learn
- Define impartial games under normal play and their P- and N-positions, and explain why one bit per position does not suffice to analyze sums of games.
- Prove Bouton's theorem for Nim, and compute a winning move from the nim-sum.
- Define Grundy numbers by the mex rule, prove the Sprague–Grundy theorem for sums of games, and compute Grundy values by DP.
- Prove that every finite subtraction game is eventually periodic, and state what is known and open about octal games, misère and partizan play, and the complexity of games.
Impartial games and P-positions
As the Optimized lesson proved by induction on the DAG, the positions split uniquely into the two kinds:
- a position is P iff every option (position reachable in one move) is N, which holds vacuously for a terminal position;
- a position is N iff some option is P.
Games often fall apart into independent pieces: several heaps, several rows of pins, several regions of a board. In the sum , a move is a move in exactly one component, and the player who cannot move in any component loses. The positions of a sum are tuples of component positions, so heaps of up to stones give positions: a DP over the sum itself explodes.
Think first Can you decide a sum from the P/N labels of its components alone? Try two Nim heaps: heaps of 1 and 1, and heaps of 1 and 2.
No. Every nonempty Nim heap is an N-position, yet is P (whatever you take, your opponent takes the other heap), while is N (reduce the 2 to a 1 and you have handed over ). Two N-components can make either kind of sum, so each component needs more than one bit of information. Equal heaps are always P: the second player copies every move on the other heap.
Nim and Bouton’s theorem
The terminal position has nim-sum 0. All heaps are empty.
From nim-sum 0, every move leads to a nonzero nim-sum. A move changes exactly one heap, from to some , and the new nim-sum is
From nim-sum , some move reaches nim-sum 0. Let be the highest set bit of . An odd number of heaps have bit set, so some does; then clears bit of and leaves the higher bits alone, so it is smaller than and is a legal move:
So the positions with nim-sum 0 satisfy the P-rule (every option is outside the set) and the others satisfy the N-rule (some option is inside it). The labeling is unique, by induction on the DAG, so these are exactly the P-positions.
Nim with heaps 3, 5 and 6, and it is your move. What happens with best play?
- You win by taking 1 stone from the heap of 6
- You win by emptying the heap of 3
- You lose: every move you make leaves a nonzero nim-sum for your opponent
- You win, because the heaps have different sizes
Show the answer
C. You lose: every move you make leaves a nonzero nim-sum for your opponent
and : a P-position, a loss for the player to move. Whatever you do, your opponent restores nim-sum 0, and eventually you face three empty heaps.
Misère Nim, where taking the last stone loses, needs only one change: play the normal strategy until your move would leave no heap larger than 1, and then leave an odd number of heaps of size 1 instead of an even one. Misère play of general games is far harder, as the last step shows.
Grundy numbers and the Sprague–Grundy theorem
The options of are the and the . By induction they satisfy the formula; let . It suffices to show that the options' values include every but not itself.
No option has value . By the mex rule no option has , so ; the same holds for the options .
Every is an option's value. Let and let be its highest set bit. Since , bit of is 1, so it is 1 in or in ; say in . Then , so by the mex rule some option has exactly that value:
Hence . Since a Nim heap of size has Grundy number too, and have the same Grundy number for every , so they are P-positions together, which is the sense in which "is" a Nim heap.
def grundy_subtraction(S, n): # Grundy values of one pile; a move removes s in S stones
g = [0] * (n + 1)
for k in range(1, n + 1):
options = {g[k - s] for s in S if s <= k}
m = 0
while m in options: # mex: the smallest value no option has
m += 1
g[k] = m
return gtime. For the values are , repeating with period 7, so the zeros are at : the losing positions of the Optimized lesson. A game of many piles is decided by XORing one table lookup per pile, instead of a DP over the product of the piles.
Three piles hold 5, 9 and 12 stones, and a move removes 1, 3 or 4 stones from one pile. Who wins with best play?
- The player to move, by taking 4 from the pile of 12
- The player to move, because is even
- The player to move, because every single pile is a winning position
- The other player: the position is a P-position
Show the answer
D. The other player: the position is a P-position
The Grundy values are , and , and . Every move changes one pile's value and makes the XOR nonzero, and the opponent can always restore 0.
Periodicity, octal games and the limits of the theory
Games in which a move may also split a heap are harder. In Kayles, a move knocks down one pin or two adjacent pins from a row, which may break the row in two; a row of pins is a heap, and a broken row is a sum. Its Grundy numbers come from the same DP, with an XOR inside the mex: . Guy and Smith (1956) introduced this family of octal games together with a periodicity test: if holds for every from up to about (plus the largest number of pins a move removes), then it holds for every . A finite computation can therefore prove periodicity forever.
| Game | A move | Grundy values from | Behavior |
|---|---|---|---|
| Nim heap | remove any number of stones | ||
| Subtraction | remove 1, 3 or 4 stones | period 7 from the start | |
| Kayles | remove 1 or 2 adjacent pins; the row may split | period 12 from on | |
| Dawson's Kayles | remove 2 adjacent pins; the row may split | period 34 from on | |
| Grundy's game | split a heap into two unequal heaps | periodicity unknown |
Complexity. A Grundy DP is polynomial in the number of positions, which is usually exponential in the size of the game's description. For succinctly described games the question is genuinely hard: deciding who wins generalized geography, or Kayles played on an arbitrary graph, is PSPACE-complete (Schaefer, 1978), and generalized chess is EXPTIME-complete (Fraenkel & Lichtenstein, 1981). Still, backward induction at enormous scale has solved real games: checkers is a draw with perfect play (Schaeffer et al., 2007).
Partizan games. When the players have different moves, as in chess or in Hackenbush with colored edges, one Nim heap per component is no longer enough. Conway's theory (On Numbers and Games, 1976; Winning Ways, with Berlekamp and Guy, 1982) gives every short game a value in an ordered group in which sums of games add; the values include the surreal numbers, named by Knuth (1974), and impartial games are the special case whose values are the nimbers .
Misère play. If the last player to move loses, sums no longer reduce to XOR. Plambeck and Siegel (2008) introduced misère quotients, finite algebraic structures that capture a misère game's behavior in sums, and used them to solve many octal games under misère play.
Key takeaways
- An impartial position is P iff every option is N; for sums of games one bit per component is not enough.
- Bouton: a Nim position is P iff the XOR of the heap sizes is 0, and a winning move replaces a heap holding the top bit of the nim-sum by its XOR with .
- The Grundy number is computed by a DP over positions, and by the Sprague–Grundy theorem : every impartial game acts like a Nim heap of size .
- Finite subtraction games are eventually periodic by a pigeonhole argument; games that split heaps, such as Kayles, are often periodic too, but whether every octal game is remains open.
- Succinct games can be PSPACE- or EXPTIME-complete, and misère and partizan play need richer theories than XOR.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Which Nim position is a loss for the player to move?
- Heaps 1, 2, 4
- Heaps 2, 2, 1
- Heaps 1, 2, 3
- Heaps 3, 3, 3
Show the answer
C. Heaps 1, 2, 3
, so it is a P-position. The others have nim-sums , and , all nonzero.
A position's options have Grundy numbers and . What is its Grundy number?
- 5
- 0
- 2
- 4
Show the answer
C. 2
The mex of is 2, the smallest value that no option has. So the position is an N-position (its value is nonzero), and in a sum it acts like a Nim heap of size 2.
A position is the sum of three independent games whose Grundy numbers are 4, 6 and 2. Who wins with best play?
- The player to move, because every component has a nonzero value
- The other player:
- The player to move, because
- It cannot be decided from the Grundy numbers alone
Show the answer
B. The other player:
By the Sprague–Grundy theorem the sum has Grundy number , so it is a P-position: the player to move loses, whatever the three games are.
Why must the Grundy sequence of every subtraction game with a finite move set be eventually periodic?
- Because Grundy numbers are always 0 or 1
- Because for every finite
- Because the values are bounded by and each depends only on the previous values, so a window must repeat
- It need not be: Grundy's game is a finite subtraction game with no known period
Show the answer
C. Because the values are bounded by and each depends only on the previous values, so a window must repeat
Finitely many possible windows of consecutive bounded values means some window repeats (pigeonhole), and since the next value is a function of the window, everything after the repetition repeats too.
Further reading
- C. L. Bouton, "Nim, a game with a complete mathematical theory," Annals of Mathematics 3 (1901–1902), 35–39.
- R. Sprague, "Über mathematische Kampfspiele," Tôhoku Mathematical Journal 41 (1935); P. M. Grundy, "Mathematics and games," Eureka 2 (1939).
- R. K. Guy & C. A. B. Smith, "The G-values of various games," Proceedings of the Cambridge Philosophical Society 52 (1956).
- E. R. Berlekamp, J. H. Conway & R. K. Guy, Winning Ways for your Mathematical Plays (Academic Press, 1982; 2nd ed., A K Peters, 2001–2004).
- A. N. Siegel, Combinatorial Game Theory (American Mathematical Society, 2013).