Nim is a simple game, in which 2 players take turns removing objects from piles until there are no more objects left. This post will define basic terms for combinatorial game theory, detail the rules of nim, and explain how to win the game.

## Combinatorial games

Most people have been playing **combinatorial games** their whole lives and never knew it. These are games where two players alternate turns and know all relevant information to decide how to play next. Furthermore, let’s consider combinatorial games that are **impartial**, which means the only difference between the players is who makes the first move. These games will have an **ending condition**, and as the name suggests, this will ensure that the game ends. When a player wins if and only if they make the last move, the game is said to be **normal**.

Every position in an impartial game can be categorized as either an **N-position** or a **P-position**. In an N-position, the next player to move can exercise a winning strategy; similarly in a P-position, the previous player to move can exercise a winning strategy. A player has a **winning strategy** if no matter what the opponent does, the player has a set of rules to follow which guarantee they win the game.

You can probably think of tons of classic combinatorial games that you played since you were a kid, like tic-tac-toe, checkers, or maybe even nim.

## An important game

In the game nim, two players alternate taking items from different piles. There can be any finite number of piles, and the piles can have any (finite) number of items in them. At each turn, a player must remove at least one item, and all items removed must come from the same pile. The goal of nim is to be the last player to take an object (in other words, your opponent is unable to make a move because all piles are empty).

A game is **determined** if from any starting position for the game, one of the players has a winning strategy. The version of nim I’ve described is determined and normal.

### A subtraction game example

I’ll provide an example for how to calculate N and P positions for a combinatorial game because it helps to show how winning strategies are derived.

Consider the following game between two players. There is a set of hershey kisses, and each player can remove 1, 3, or 4 hershey kisses on their turn. The player to make the last move wins. I’ve labeled, with short explanations, whether a stack of hershey kisses is an N or P position in this game, when is between 0 and 11.

- 0: The
*previous*player to move took the last kiss and won. - 1, 3, 4: The
*next*player to move can take all the candy and wins. - 2: The
*previous*player takes the last kiss after the next player leaves exactly 1 kiss. - 5: After a move on 5 kisses, there are 1, 2 or 4 kisses left. The
*next*player can take 3 kisses, so they become the previous player on a game with 2 kisses. - 6: After a move on 6 kisses, there are 2, 3 or 5 kisses left. The
*next*player can take 4 kisses, so they become the previous player on a game with 2 kisses. - 7: After a move on 7 kisses, there are 3, 4, or 5 kisses left. On all the these positions the next player to move wins, so here the
*previous*player to move wins. - 8,10,11: From these positions, the
*next*player to move can leave 7 kisses. - 9: After a move on 9 kisses, there are 5, 6, or 8 kisses left. On all the these positions the next player to move wins, so here the
*previous*player to move wins.

The pattern established above for N and P positions can be proved inductively to extend for any set of natural numbers. So you could conclude that the second player has a winning strategy when the number of hershey kisses divided by 7 leaves a remainder of 0 or 2. Below is a diagram illustrating the pattern, where an arrow between two positions illustrates a possible move.

This example demonstrates how to characterize N-positions and P-positions in a game. They are defined recursively as such:

- An ending position is a P-position.
- From every P-position, all the moves are to N-positions.
- From every N-position, there exists a move to a P-position.

### Back to nim

So how do you win nim? If you don’t already know how this winning strategy goes, try a few rounds here, and see if you can figure it out.

First, let’s define **nim-sum**, but turn to your old friend if you forget, or never knew, what the **xor** operation is. When numbers, , are written in their binary representation, the nim-sum of them is just .

## Bouton’s theorem

Whether the first player to move has the winning strategy depends exactly on the size of the piles. The following theorem was due to Charles Bouton.

**Bouton’s Theorem** *The second player has a winning strategy to a game of nim if and only if the nim-sum of the piles is 0.*

When the nim-sum of the piles is 0, the winning strategy for the second player is to make moves which turn in nim-sum of the piles to 0 at each turn. When in the nim-sum of the piles is nonzero, the winning strategy for the first player is to make moves which turn in nim-sum of the piles to 0 at each turn.

### Proof

In a state of nim with piles, let be the binary representation for the size of the pile. After a move is made, denote the binary representation for the size of the pile by . Remember that during a move, items can only be taken from 1 pile, so only one pile’s size is different than it was before the move. Therefore for all except for exactly one pile, let’s call that pile . Also, note that because we removed items from pile , .

3 lemmas are needed for the theorem. They use the facts that on (the set of binary strings of length ) forms an Abelian group. If you don’t speak algebra, on is associative, commutative, has identity , and has a unique additive inverse for every element in . Regarding the additive inverse, for any number , . These facts are all easily verifiable if you’ve got any doubts.

For brevity, let and .

**Lemma 1** *The nim-sum after a move is .*

The proof follows as such, only relying on the properties defined above for $$\oplus$$ on .

**Lemma 2** *If , then no matter what move is made*.

This means that if the nim-sum in some stage of a game of nim is 0, it cannot be 0 after a move is made (except in the obvious case where there are no more moves left).

From Lemma 1, , but here . Thus . We required that , because it is the unique pile which had items removed in this move. Therefore, , because ‘s unique additive inverse was itself. So if the nim-sum before a move is 0, the nim-sum after that move is not 0.

**Lemma 3** *If , then there exists some move such that . This means that if the nim-sum in some stage of a game of nim is not 0, there is some move that can be made so that after the move, the nim-sum is 0.*

We know is not 0, so there is some bit of which is 1. Let be the leftmost bit of which is 1. Also, choose so that the $ bit of is also 1 (such an must exist in order for to have a 1 in its bit). Set .

has a 0 in its bit, and was the leftmost bit of which is 1. So in , the leftmost bit with a 1 in it is to the right of . This implies that decreased by . The bits to the right of may change, but this could add at most to . Therefore we see . So a player can make a move by taking objects from heap . We wish to find the nim-sum after this move, and will make use of Lemma 1 again:

So if the nim-sum in not 0, there is some move that can be made so that after the move, the nim-sum is 0.

Combining Lemmas 2 and 3, we conclude that when the nim-sum of a stage of the game is 0, the next move will always make it nonzero, and when the nim-sum of a stage of the game is nonzero, there exists some move to make to nim-sum 0.

## To be continued

What does this mean? If the nim-sum is nonzero at the start of a game, player 1 can move to make it zero. No matter what player 2 does, player 1 is always able to make the nim-sum 0 again on their turn. Player 1 will always be able to make the last move as long as in each turn, they remove items from a pile to make the nim-sum 0. The number of items in play is strictly decreasing. So eventually, the move from player 1 to make the nim-sum 0, is the move which makes all the piles empty.

If the nim-sum is 0, then no matter what player 1 does, player 2 is always able to make the nim-sum 0 again on their turn. From the same discussion as the one above, we see that player 2 will win.

Nim doesn’t seem very “fun” in the typical sense, or at least I wouldn’t expect 6 year olds to be playing it on the back of their kids menu at a restaurant. But enter the Sprague Grundy theorem and things get cool. This theorem states that any impartial game is equivalent to a game of nim with some associated “nimber”. Check out a post on the **Sprague-Grundy theorem** to see why this is true.

History of Common Games – with high probability