33Games

The game of Nim

Nim is a game of heaps of sticks where the player who takes the last stick wins. It looks like luck, but it is solved by adding the heaps in binary. Play a machine, add in binary, find the winning move and try the twist where the last stick loses.

Sticks on the table

Take the last stick

Nim is a game for two. There are heaps of sticks. On your turn you take as many sticks as you like from one heap, at least one. Whoever takes the last stick wins.

It looks like luck and guessing. It is not. Nim has a rule that tells the player whose turn it is whether they can win, and which move to make. The rule uses numbers written in binary. Four things to try: play a machine, add heaps in binary, find the winning move yourself, and play the twist where the last stick loses. Without a script each part shows one picture or table.

Try one

Play the machine

The classic game has four heaps of 1, 3, 5 and 7 sticks. Click a stick to take it and all the sticks to its right in the same heap. The machine answers at once.

Start with the machine playing at random, and win. Then switch it to perfect play and try again. The classic start is a position that the player who moves first cannot win against perfect play. Let the machine start and you have a chance.

Heap 1
Heap 2
Heap 3
Heap 4

Heaps of 1, 3, 5 and 7 sticks. Take the last stick to win. Click a stick to take it and the sticks to its right.

Try two

Add in binary

Write each heap size in binary. Then add each column without carrying: if the column has an odd number of ones, write 1, and if it has an even number, write 0. The result is the nim-sum. This is the operation called XOR.

The rule: if the nim-sum is zero when it is your turn, you lose against perfect play. If it is not zero, you can win. Move the sliders and watch the columns.

Heap sizes in binary, with the nim-sum in the last row
HeapSize421
11001
23011
35101
47111
Nim-sum000

The heaps 1, 3, 5, 7 have a nim-sum of 0. With a nim-sum of zero, the player to move loses against perfect play.

Try three

Find the move

Each position has a winning move, a move that leaves a nim-sum of zero. To find it, take the nim-sum. Then look for a heap that gets smaller when you combine it with the nim-sum using XOR, and cut that heap down to the new size.

Choose a heap and how many sticks to take, then check. The sum is shown for every try.

Heap 1
Heap 2
Heap 3
Heap 4

The heaps 6, 4, 5, 4 have a nim-sum of 3. In binary: 110, 100, 101, 100. Find a move that makes the nim-sum zero.

Try four

The last stick loses

Change one rule: the player who takes the last stick loses. This is called misere play. You might expect the whole strategy to flip. It does not. The winner plays exactly as before, until the moment when every heap has at most one stick. Then the winner makes sure an odd number of single sticks is left.

Play the machine in this version. When many heaps are big, the nim-sum still decides the game.

Heap 1
Heap 2
Heap 3
Heap 4

Heaps of 1, 3, 5 and 7 sticks. Now the player who takes the last stick loses.

Key takeaways

  1. Nim is solved. From any position, the player to move either has a winning move or none at all, and the nim-sum says which.
  2. The nim-sum is the XOR of the heap sizes: add in binary with no carrying. If it is zero on your turn, you lose against perfect play.
  3. The winning move cuts one heap so that the nim-sum becomes zero.
  4. The classic start of 1, 3, 5 and 7 has nim-sum zero, so the first player loses against perfect play.
  5. If the last stick loses, the strategy is the same until only heaps of one stick are left. Then the winner leaves an odd number of them.

Sources

Most of these sites could not be opened while this page was written, so the facts were checked through search results that quote them. The rule of the game, the nim-sum as XOR of the heap sizes, a zero nim-sum meaning a lost position for the player to move, the winning move of cutting a heap to heap XOR nim-sum, the nim-sum of zero for heaps of 1, 3, 5 and 7, the change of strategy at the end in misere play, and Charles Bouton’s complete theory of 1901 each appeared in at least two places. The Nimatron, a Nim machine shown by Westinghouse at the New York World’s Fair of 1939 to 1940, is described with different dates in different sources, so the page leaves the year out. The strategy on the page was also checked by computer against every position of up to four heaps of at most five sticks, in both versions of the game. The machine in the game uses that strategy, or a random move.