Lesson 2

In this lesson, we will focus on a special type of game called Nim. Although it’s only one out of infinitely many possible games, understanding it in depth will be very beneficial in analysing a much larger class of games, known as impartial games. (The exact definition of this term is deferred until a later lesson.)

The rules of this game are as follows:

Start with r heaps of stones, of sizes . Play alternates between two players: at each player’s turn, he must pick a heap and remove as many stones as he wants from it, possibly even emptying the heap. However, he can remove stones from one and only one heap.

Here is an example of a game between two players:

Thus the second player wins in this match.

Of course, we could do our analysis as in the previous lesson: to check whether (3, 5, 8) is winning or losing, we could iteratively compute the status of for . But this would require us to compute the status for 3 × 5 × 8 = 120 positions, even for a simple configuration like (3, 5, 8), which is way too much work. Thankfully, there is a beautiful solution for the problem.

Binary Representation

If you understand what binary representation is, you may skip this section entirely. The crux of the matter is the following result:

Every positive integer is uniquely representable as a sum of distinct powers of two (i.e. 1, 2, 4, 8, 16 … ).

The keyword here is “distinct”. To write a number n as a sum of distinct powers of 2, let be the largest power of 2 which does not exceed n. Now consider the difference . If d = 0, then and there is nothing left to do. Otherwise, d > 0. We claim that ; indeed, if then would be at least which contradicts the maximality of r. (q.e.d.)

Example : let’s take n=25. To take the largest power of two which is at most 25, we have to pick . The difference is:

.

Now do the same with 9: the largest power of two not exceeding it is and the difference is:

.

Finally so we can write . We will write this more compactly as follows:

.

This is known as the binary representation of 25. We can add as many leading zeros as we want:

which is akin to 37 = 037 = 0037 = … in decimal representation.

Solving Nim

Now we are ready to describe our strategy for playing Nim perfectly. For simplicity, we consider only three heaps of stones: (a, b, c), where a, b, c > 0, and take the example a = 17, b = 25, c = 13. First, write a, b, c in binary notation and align them on the right. E.g.

Now count the number of ones in each column and note their parities. In our example, we have (from left to right): 2, 2, 1, 0, 3. Their parities are even, even, odd, even, odd respectively. The big theorem is as follows:

The above Nim configuration is a losing position if and only if there is an even number of ones in each column.

Hence, the above configuration is winning (i.e. first player wins) since there are an odd number of ones in the rightmost column. One naturally asks for a proof to this theorem. Instead of a rigourous proof, we will instead explain a winning strategy, from which the proof should be apparent. For now, let’s look at another example.

Example

Consider the Nim game (30, 15, 27, 10). To compute its status, let us write:

Now there are an even number of ones in every column, so the second player wins.

Playing Nim

Knowing the status of the game position is just half the battle won – you need to know how to exploit a winning position to obtain an eventual victory. Below, we will explain how to do this, by looking at the case of (17, 25, 13) above. As before, write:

17 = (10001)
25 = (11001)
13 = (01101)

Starting from the leftmost position, find the first column c which has an odd number of ones:

17 = (10 [0] 01)
25 = (11 [0] 01)
13 = (01 [1] 01)

Next, pick a heap number m which has a one in that column. Such a heap must exist, since otherwise, there’d be no 1’s in that column (and thus, an even number of ones). In our example, we must take m = 13. Now remove the binary digits of m from column c to the right.

17 = (10 001)
25 = (11 001)
? = (01 ***)

Fill in our desired binary digits in order to have an even number of ones in each column:

17 = (10 001)
25 = (11 001)
? = (01 000)

Note that this is a losing position by our theorem, so it would do us good to leave this configuration for our opponent. Also, such a move is always possible since we’re flipping the ‘1’ in column c to ‘0’, and no bits to the left of c are changed. So the resulting number is always smaller than m. Since (1000) = 8 in binary, in our example, a good move would be:

(17, 25, 13) → (17, 25, 8).

Thus, we see that given a winning position, it is always possible to move to a losing position for the opponent. That’s only half the theory; we also need to explain why every move from a losing position results in a winning one.

The XOR Operation

Given a Nim game if are fixed, then there’s only one possible for which G is a losing position. Indeed, suppose r= 3 and . Then to compute , we write the binary representations:

n1 = 37 = (100101)
n2 = 52 = (110100)
n3 = ?? = (******)

For each column of , there’s a unique bit such that the total number of ones in that column is even:

n1 = 37 = (100101)
n2 = 52 = (110100)
n3 = ?? = (010001)

This shows that is unique and also gives us a recipe for calculating it: simply add the two numbers in binary and ignore any carry which might occur. Addition has never been easier! Just perform 0+0 = 0, 0+1 = 1+0 = 1, 1+1 = 0 and forget the carry. This operation is called XOR for exclusive-or, or sometimes just addition without carry and denoted by .

Incidentally, this also explains why every losing position must lead to a winning one in the next move. For if were a losing position, then:

.

Another way of writing this would be . Hence, if the first player makes a move from , then it would result in which is a winning position. Since there’s nothing special about the last heap , any move would also result in a winning position.

Exercises

Determine the status of the following Nim games:

(17, 18, 13),

(18, 14, 9, 21).

For the games in Q1 where the first player wins, find a good move for him.

Given that the Nim game (15, 20, 7, m) is a second-player win, what is m?

Mum has baked a large almond cake which is divided into unit squares as below:

Unfortunately, the black square is burnt and inedible. Now Alice and Bob play a game as follows: each child alternately cuts the cake into two (possibly unequal) pieces, by a horizontal / vertical cut which is along a gridline. He/she then eats the edible piece and leaves the other behind. Whoever is left with the burnt piece is deemed to have lost and must do the dishes. If Alice starts the game, can she avoid the dishes?

In Northcott’s Game, each player controls counters of a certain colour (either black or white). At his turn, he may arbitrarily shift his counters left or right any number of squares. However, no counter may jump over another counter. The player who’s unable to make any move loses. If black starts first, who wins, or will the game never end?

In Misere Nim, the rules are exactly the same as Nim with one caveat: the person who takes the last stick loses the game instead of winning. Based on your understanding of classical Nim, find a strategy for Misere Nim. Explain how the strategies for the two games differ.

(Hard) In Nimble, we have coins in the following strip: each square contains at most one coin. Play alternates between two players. Each player, at his turn, picks up any coin and shifts it to some position on its left – without sliding off the strip and without jumping over another coin. Does the first or second player win in the following configuration?

Analyse the following Chinese Chess position : who wins?

This interesting mathematical game is the subject of some research recently. The game of Chomp is played as follows: start with an m-by-n grid of squares. Two players take turns cutting out a piece of the board by (i) picking any unit square on the board, and (ii) cutting away all squares to its right and below. Whoever is forced to take the top-left square loses.

Find a winning strategy for the first or second player on an n-by-n square board.

Find a winning strategy for the first or second player on an 2-by-n square board.

Determine whether the first player wins in the following configuration. (P. S. The answer depends on a and b.)

(Hard) Prove that, in general, the first player wins on any m-by-n grid if mn > 1.