6 Impartial games

   6.1.  Notation
   6.2.  Nimbers
   6.3.  The Sprague-Grundy Theorem
   6.4.  Nim addition
   6.5.  Taking and Breaking

6.1 Notation

Recall that a game G is called impartial if, from any subposition of G, both Left and Right have the same options. In other words, the left options of G are the same as the right options of G, and all options of G are impartial. In particular, G≅ ⁡ − G, since switching the roles of Left and Right does not change the game.

Since the left and right options of G are the same, it is a bit wasteful to write out both left and write options. Hence we will typically just write the set of options once, so we will abbreviate the impartial game

G = {G1′,G 2′,G 3′,… ⁡∣G 1′,G 2′,G 3′,… ⁡}

as

G = {G1′,G 2′,G 3′,… ⁡},

and we will use G′ to refer to a typical option of G.

6.2 Nimbers

In Nim, each player picks a pile of stones and removes at least one stone from that pile. Hence from a pile of n stones, either player can move to a pile of 0, 1, 2, …, or n − 1 stones. We denote the Nim game consisting of a single pile of n stones by ∗ n.

Definition 6.1 A nimber is any game of the form

∗n = {∗0,∗1,∗2,… ⁡,∗(n − 1)∣ ∗ 0,∗1,∗2,… ⁡,∗(n − 1)},

where n is a nonnegative integer, or abbreviated:

∗n = {∗0,∗1,∗2,… ⁡,∗(n − 1)}.
Hence ∗ 0 = {} = {∣} = 0, ∗ 1 = {∗0} = {0∣0} = ∗, ∗ 2 = {∗0,∗1} = {0,∗∣0,∗}, ∗ 3 = {∗0,∗1,∗2} = {0,∗,∗2∣0,∗,∗2},

and so forth.

Any Nim position is therefore just a sum of nimbers (corresponding to the sizes of the piles).

6.3 The Sprague-Grundy Theorem

It might seem as if there should be many impartial games that are not nimbers: for instance, a game like {0,∗,∗2,∗5,∗7} is not isomorphic to any nimber. However, the following theorem shows that in fact any impartial game is equal to some nimber.

Theorem 6.2 (Sprague-Grundy) Let G be an impartial game. Then G = ∗m for some integer m ≥ 0.

If G = ∗m, then we call m the nim value of G and write 𝒢 (G) = m. Note that G is a second player win (that is, G lies in 𝒫 ) if and only if 𝒢 (G) = 0.

To prove the Sprague-Grundy Theorem, we will need to determine the nim value of games like {0,∗,∗2,∗5,∗7}. This is given by the following proposition, called the Mex Rule.

Definition 6.3 For any finite set of nonnegative integers S, the minimum excluded value or mex of S, denoted mex ⁡ (S), is the smallest nonnegative integer not lying in S.

Proposition 6.4 (Mex Rule) Let G = {∗a1,∗a2,… ⁡,∗ak} for some nonnegative integers a1,a2,… ⁡,ak. Then G = ∗m, where m = mex ⁡ {a1,a2,… ⁡ ,ak}.

Example 6.5 If G = {0,∗,∗2,∗5,∗7}, then mex ⁡ {0,1,2,5,7} = 3, so G = ∗3.

Similarly, if H = {∗,∗2,∗3,∗4}, then mex ⁡ {1,2,3,4} = 0, so H = 0.

Proof of Mex Rule. We need to show that G −∗m = G + ∗m is a win for the second player. For any nonnegative integer p < m, ∗ p is both an option of G and ∗ m.It is an option of ∗ m by definition of ∗ m, and it is an option of G because p is smaller than the minimum excluded value m.Hence if the first player moves to ∗ p in either component, the second player can move to ∗ p in the other component, leaving ∗ p + ∗p = 0, so the second player wins.

Otherwise, the first player must move to some option ∗ q of G with q > m.Recall that ∗ m is not an option of G.Then the second player can move in ∗ q to ∗ m, leaving ∗ m + ∗m = 0, so the second player wins.  

(In our earlier parlance, each option ∗ q of G with q > m is reversible through ∗ m.)

It is now straightforward to prove the Sprague-Grundy Theorem.

Proof of Sprague-Grundy Theorem. Let G = {G1′,G2′,… ⁡,Gk′} be an impartial game. By induction, each Gi′ equals some ∗ ai. Thus G = {∗a1,∗a2,… ⁡,∗ak} = ∗m, where m = mex ⁡ {a1,… ⁡ ,ak}.  

The Sprague-Grundy Theorem and Mex Rule can be summarized by the following equation:

𝒢 (G) = mex ⁡ G′∈G𝒢 (G′).

6.4 Nim addition

In theory, one can use the Mex Rule to find the nim value of any impartial game. For instance, let’s use it to compute the sum of two nimbers. So as to avoid having to write ∗’s everywhere, we will define a ⊕ b to be the nim value of ∗ a + ∗b, or in other words, ∗ (a ⊕ b) = ∗a + ∗b. The operation ⊕ is called nim addition. For instance, a ⊕ a = 0 for all a since ∗ a + ∗a = 0.

By definition,

∗ a + ∗b = {(∗a)′ + ∗b,∗a + (∗b)′} = {∗0 + ∗b,∗1 + ∗b,… ⁡,∗(a − 1) + ∗b, ∗ a + ∗0,∗a + ∗1,… ⁡,∗a + ∗(b − 1)}.

In other words,

a ⊕ b = mex ⁡ {a′⊕ b,a ⊕ b′∣a′ < a,b′ < b}.

We can then create a nim addition table by noting that each entry a ⊕ b must be the mex of the entries above it in the same column (a′⊕ b for a′ < a) and to the left of it in the same row (a ⊕ b′ for b′ < b). This allows us to fill in entries moving outward from the upper left corner.

For instance, suppose that we have filled the table up to this point:

⊕0123 ̲ ̲ ̲ ̲ ̲ 00123 110322230

To find 2 ⊕ 3, we must compute the mex of all numbers above it in its column and to its left in its row. Hence 2 ⊕ 3 = mex ⁡ {3,2,2,3,0} = 1. The addition table will end up looking something like the following.

⊕01234567 ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ 001234567 110325476223016745 332107654445670123 554761032667452301 776543210

You may be able to spot some patterns in this table, which suggests that one may be able to give an explicit formula for a ⊕ b without having to work through this whole process. Indeed, this is given by the following theorem.

Theorem 6.6 For any nonnegative integer a, let ak⋯a2a1a0 be the binary representation of a, so that a = ∑ ⁡ ai2i with ai ∈{0,1} for all i. Similarly let b have binary representation bk⋯b2b1b0.

Then a ⊕ b has binary representation ck⋯c2c1c0, where ci = 0 if ai and bi are the same, and ci = 1 if ai and bi are different.

The operation ⊕ is sometimes called bitwise XOR in computer science. (Here bit is short for binary digit and XOR is short for eXclusive OR.)

Example 6.7 Let us compute 19 ⊕ 10. Since 19 = 24 + 21 + 20, the binary representation of 19 is 10011. Likewise, 10 = 23 + 21, so the binary representation of 10 is 1010. We then “add without carrying”:

19 =1001110=1010 ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ 25 =11001

It follows that 19 ⊕ 10 = 25, or equivalently, ∗ 19 + ∗10 = ∗25. Note that in the resulting table, each column has an even number of 1’s.

Proof of theorem. Let c be the number with binary representation ck⋯c2c1c0. Note that by construction, ai + bi + ci is even for all i. We will show that this condition is equivalent to ∗ a + ∗b + ∗c = 0 (that is, that ∗ a + ∗b + ∗c is a second player win) by induction.

Suppose (without loss of generality) that Alice moves from ∗ a to ∗ a′ for some a′ < a. Let j be the leftmost position in which the jth bit of a and a′ differ. Since a′ < a, we must have aj = 1 and aj′ = 0. Since aj + bj + cj is even, either bj or cj must be 1; say it is bj. Then let b′ be the number such that ai′ + bi′ + ci is even for all i. Then bj′ = 0 and all bits to the left are unchanged from b, so b′ < b. Hence Bob can win by moving from ∗ a′ + ∗b + ∗c to ∗ a′ + ∗b′ + ∗c, which equals 0 by induction.  

Example 6.8 Suppose that in the game ∗ 19 + ∗10 + ∗25, which we claim is a second player win, Alice takes six stones from ∗ 10, leaving ∗ 4.

19 =1001110=1010 25 =11001 →A 19 =10011 4 = 1 0 0 25 =11001 →B 19 =10011 4 = 1 0 0 23 =10111

Since the leftmost bit in which 10 and 4 differ is in the 23 place (where the digit changed from a 1 to a 0), that column now has an odd number of 1’s. In particular, one of the other numbers must have a 1 in that column, in this case, 25. Hence we can replace 25 with a smaller number to again guarantee that each column has an even number of 1’s. Here, Bob’s winning move is to take two stones ∗ 25, leaving ∗ 23.

It is just as easy to find the winning move in any Nim position by adding together the values of all the piles using nim addition. When nim-adding several numbers, simply list all of them in binary. A bit of the nim sum will be 1 if there are an odd number of other 1’s in that column, and 0 otherwise. If the nim sum is already 0, then there is no winning move. Otherwise, find the leftmost bit in the nim sum that is 1. There must be some pile that also has that bit equal to 1, so take the appropriate number of stones from that pile to make the total nim sum 0. The size of the new pile can be found by nim-adding the old nim sum to the size of the pile to be changed.

Example 6.9 Consider the nim position with piles of sizes 4, 6, 8, and 9. We compute the nim sum 4 ⊕ 6 ⊕ 8 ⊕ 9 = 3:

4 = 1006=110 8 =10009=1001 ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ 3 =0011 →4 = 100 5 = 1 0 1 8 =1000 9 = 1 0 0 1 ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ 0 =0000

Since the leftmost bit of 3 equal to 1 is in the 21 place, the winning move must be in a pile that also has a 1 in that place, namely ∗ 6. Since 6 ⊕ 3 = 5, the winning move is to take 1 stone from that pile, turning ∗ 6 to ∗ 5.

6.5 Taking and Breaking

Many impartial games fall into the category of taking and breaking games, which are games that involve piles of stones of various sizes in which a move consists of taking some number of stones from a pile and then possibly breaking the remaining stones into smaller piles according to some rules. For such games, one can compute the nim values of a single pile recursively using the Mex Rule.

When our choice of game is implicit, we will write 𝒢 (n) for the nim value of a pile of size n in that game. We will call the sequence

𝒢 (0), 𝒢 (1), 𝒢 (2), 𝒢 (3),… ⁡

the nim sequence of the game.

The simplest such games are the subtraction games Subtraction(S) for any subset S of positive integers. These games are played exactly like Nim except that one is only allowed to remove a number of stones equal to a value in S on a single turn. Then the Mex Rule immediately gives that

𝒢 (n) = mex ⁡ s∈S𝒢 (n − s).

Example 6.10 In Subtraction(1,2,3), one is only allowed to remove 1, 2, or 3 stones from any pile. We can compute 𝒢 (n) by noting that each must be the mex of the three previous values, giving the following nim sequence.

n 01234567891011⋯ 𝒢 (n)0123012301 2 3 ⋯

A simple induction shows that the nim sequence is periodic with period 4.

To determine the winning move in a position consisting of several piles, simply imagine that each pile is instead a pile in a standard game of Nim according to its nim value. Then for any winning move in that Nim game, you can find a move in the original game that changes the nim values as desired.

Example 6.11 Suppose that in Subtraction(1,2,3), one receives a position with piles of sizes 13, 16, and 22. Using the periodicity found above, we can find that the nim value of this position is

𝒢 (13) ⊕ 𝒢 (16) ⊕ 𝒢 (22) = 1 ⊕ 0 ⊕ 2 = 3.

Since the total nim value is nonzero, this game is a first player win. In the nim position with piles of sizes 1, 0, and 2, the first player can win by turning the pile of size 2 into a pile of size 1. Hence in the original game, the first player can win by turning the pile with nim value 2 = 𝒢 (22) into a pile with nim value 1 = 𝒢 (21) (by taking one stone from that pile).

Example 6.12 Consider the game Subtraction(2,3), in which one is allowed to take 2 or 3 stones from one pile. (Note that the game will end when all piles have size 0 or 1.) Then we can find the nim sequence using 𝒢 (n) = mex ⁡ {𝒢 (n − 2), 𝒢 (n − 3)}. We find that it is periodic with period 5.

n 01234567891011121314⋯ 𝒢 (n)0011200112 0 0 1 1 2 ⋯

If you were to receive a position with piles of sizes 12, 23, and 34, its nim value would be

𝒢 (12) ⊕ 𝒢 (23) ⊕ 𝒢 (34) = 1 ⊕ 1 ⊕ 2 = 2.

Hence it is a first player win, and a winning move is to reduce the pile with nim value 2 to a pile of nim value 0. This can be done by removing 3 stones from the pile of size 34 to make it have size 31 since 𝒢 (31) = 0.

Slightly more complicated are games in which one is required to break piles into smaller piles.

Example 6.13 In Grundy’s game, a move consists of splitting a pile into two piles of different sizes. (Note that the game will end when all piles have size 1 or 2 since these cannot be split further.)

According to the Mex Rule, we have that 𝒢 (n) = mex ⁡ {𝒢 (a) ⊕ 𝒢 (n − a)∣0 < a < n,a≠n 2 }. For instance, 𝒢 (6) = mex ⁡ {𝒢 (1) ⊕ 𝒢 (5), 𝒢 (2) ⊕ 𝒢 (4)}. We can therefore recursively find the nim sequence of Grundy’s game.

n 01234567891011121314151617181920⋯ 𝒢 (n)0001021021 0 2 1 3 2 1 3 2 4 3 0 ⋯

Suppose one were to receive a position with piles of sizes 10, 15, and 19, which have nim values 0, 1, and 3. In such a Nim position, the winning move would be to turn the pile of size 3 into a pile of size 1. Hence the winning move in the Grundy’s game position is to turn the pile of size 19 into a position with nim value 1. One way to do this is to split this pile into piles of sizes 4 and 15 since 𝒢 (4) ⊕ 𝒢 (15) = 0 ⊕ 1 = 1. (One could also split it into 7 and 12, or 9 and 10.)

It is not known whether the nim sequence for Grundy’s game eventually becomes periodic.

Example 6.14 The game Kayles is played with a row of pins of length n. Players alternate throwing balls one at time at the pins, knocking down either a single pin or two adjacent pins.

From a row of length n, one can create two rows of length a and b if a + b = n − 1 or a + b = n − 2. Hence

𝒢 (n) = mex ⁡ {𝒢 (a) ⊕ 𝒢 (b)∣a + b = n − 1 or n − 2}.

Using this recurrence with the initial condition 𝒢 (0) = 0, we can calculate the nim sequence for Kayles to be

n 01234567891011 𝒢 (n)0123143214 2 6

Hence if you receive a position with three rows of sizes 4, 5, and 6:

you should calculate 𝒢 (4) ⊕ 𝒢 (5) ⊕ 𝒢 (6) = 1 ⊕ 4 ⊕ 3 = 6. This is nonzero, so the first player has a winning strategy. Here, it is to reduce the nim value of the second row from 4 to 2, which one can do by knocking out the second pin in that row.This reduces the pile of size 𝒢 (5) = 4 to 𝒢 (1) ⊕ 𝒢 (3) = 1 ⊕ 3 = 2, so that the total position has nim value 1 ⊕ 2 ⊕ 3 = 0.

It is straightforward for a computer to calculate even more values, yielding a nim sequence of

(The highlighted numbers indicate places where 𝒢 (n)≠𝒢 (n − 12).) One can show that the sequence then becomes periodic with period 12.