Nim is a combinatorial game, where two players alternately take turns in taking objects from several heaps. The only rule is that each player must take at least one object on their turn, but they may take more than one object in a single turn, as long as they all come from the same heap.
Nim is the most well-known example of an impartial game, a game where both players have the same moves all the time, and thus the only distinguishing feature is that one player goes first. It is also completely solved, in the sense that the exact strategy has been found for any starting configuration.
The basic Nim begins with two players and several heaps, each containing several objects. Occasionally, heaps are also called piles, and the objects are called stones.
Each player, in turn, must take at least one stone, but they may take more than one stone as long as they all come from the same pile. It's allowed to make a pile empty, effectively removing the pile out of the game. When a player is unable to move, the game ends. Naturally, as long as there is a stone, either player can take that stone, and thus can move. So the ending condition can be rephrased, where the game ends if there is no stone left.
In normal Nim, the loser is the player unable to move. It is called normal condition by convention from combinatorial game theory, where a normal game gives the win to the last player making a move. In misère Nim, the player unable to move wins instead; this is equivalent to the player taking the last stone losing.
Consider the following example of the game. There are three piles, initially having stones respectively. Alice and Bob are playing, with Alice starting.
|Pile 1||Pile 2||Pile 3||Move|
|1. Alice takes stones from Pile 1|
|2. Bob takes stones from Pile 3|
|3. Alice takes stones from Pile 2|
|4. Bob takes stone from Pile 1|
|5. Alice takes stone from Pile 3|
|6. Bob takes stone from Pile 2|
|7. Alice takes stone from Pile 3|
|8. Bob takes stone from Pile 3|
|9. Alice takes stone from Pile 2|
In normal play, Alice wins, as Alice has taken the last stone and thus leaving Bob with no move. In misère play, Alice would lose instead, but in this case Alice would have taken stones from Pile 3 on move 7, leaving Bob with pile sizes and thus forcing Bob to take the last object.
In the above game, Alice has played a perfect game, never letting Bob to be able to snatch the win. This can be generalized into a general strategy.
The nim-sum of two non-negative integers and is defined as follows. Represent and as sums of distinct powers of two. Cancel powers of two appearing twice, and add up the remaining numbers. Nim-sum is also known as XOR by computer scientists.
For example, can be computed as follows. We have and . We cancel for appearing twice, and add up the remaining , giving .
It can be shown that is associative, thus making the nim-sum of several numbers defined.
Now, given a normal Nim position (the sizes of the piles) , the player to move wins if ; a winning move can be found by determining a pile and a number such that , and taking some stones from pile such that stones are left. If , then the player to move loses.
For example, in the above game with , we see that , and , so the player to move (in this case Alice) wins by reducing the first pile from stones to stone. After that, , so theoretically the player to move (in this case Bob) loses, as long as the opponent plays perfectly.
In misère Nim, the strategy is almost identical. As long as after the suggested move, there will be at least one pile of size or larger, follow the strategy of the normal Nim. However, if after the suggested move, there will be no pile of size or larger, play a different move:
- If the suggested move makes the pile have stone left, make it have stones instead, or
- If the suggested move makes the pile have stones left, make it have stone instead.
In other words, the correct move is to leave an odd number of piles of size . (In normal play, there should be an even number of piles of size instead, to make the nim-sum zero.)
The following proof is for normal Nim's strategy, given by C. Bouton.
The moving player wins in normal Nim if and only if the nim-sum of the pile sizes is not zero.
We will begin with the easy base case: if the pile sizes are all zero, then the moving player loses and the nim-sum is zero. From now on, assume that not all pile sizes are zero.
First, we observe that the nim-sum obeys the following several properties for all non-negative integers
- Computing nim-sum of multiple numbers at once is possible: write all numbers as sums of distinct powers of 2, find all powers of 2 that appear an odd number of times, and sum up one occurrence of each power of 2. For example, .
Suppose the pile sizes are before a move, and after a move. Suppose that the move is on pile ; then for all , . Let and . We have
Now we will prove two results.
Result 1: If , then . If the nim-sum of the original sizes is zero, then the moving player is losing (they must make the nim-sum nonzero).
We claim that . Indeed, suppose it is, then
Thus . But this contradicts the fact that the moving player moved on pile , and thus must make the size different.
Thus, since , we have
Result 2: If , it's possible to make . If the nim-sum of the original sizes is not zero, the moving player is winning (they can make the nim-sum zero).
Consider the largest power of 2, , not greater than . There must be at least one such that it also contains , otherwise cannot appear in . Now, take . The value decreases by , and increases by at most (each remaining powers of 2 making up adds to the value; for example and gives ), so . Moreover,
This proves the theorem.
The strategy given above for misère Nim is correct: follow normal Nim strategy, except that when the moving player is going to make all pile sizes less than stones, the moving player makes the number of piles of stone odd instead of even.
The only change is when the moving player needs to reduce the single pile having size or more to less than other piles have sizes no more than
Suppose the piles are , where and . Then . If , this would imply , contradiction. So the moving player is winning. It's also clear that the moving player can make the first pile to have only zero or one stone, since it begins with at least stones.
Once the moving player decides the move, the rest of the game is forced; with an odd number of -sized piles and the opponent starting, the opponent will also take the last stone, thus losing.
This proves the correctness.
There are many variations of Nim. The most well-known ones are normal Nim and misère Nim; in several textbooks, normal Nim is considered to be the standard game and misère Nim is the variant, while in others, it's the other way around. Due to Nim being a very basic game, there are so many possible variants obtained by simply adding one extra rule; this list doesn't intend to cover all possibilities, only the well-known ones.
A variant is known as subtraction game. This is equivalent to Nim, but where the number of stones taken is limited to some set of positive integers: for example, the first numbers or the square numbers. Generally, this is played with one pile only.
Usually, Nim is played with the stones clumped together in heaps. A variant places the stones in rows so that taking stones in the middle of a row breaks the row into two. In other words, a player's move is to take at least one stone from a pile and optionally split the pile into two piles. Kayles is played with a single pile, allowing splitting, but a player may take at most stones at a time. Dawson's chess is another variant of Kayles, where a player may take at most stones at a time; however, taking one stone is only allowed if it's the only stone in a pile, and taking two stones doesn't allow splitting. Circular Nim is played with stones initially arranged in a circle, so the first time the pile is played on, it cannot be split; additionally, one can only take no more than stones.
A generalization of Nim is the octal game. Players take turns removing several stones from a single heap in each turn, just as in usual Nim; however, the rules that govern when and how a heap can be taken and/or split are compactly noted as an octal number. For the octal game , the digit specifies how many heaps are allowed to be left after removing stones from a heap. is the sum of
- if leaving heaps is permitted (the heap has exactly stones), otherwise;
- if leaving heap is permitted (the heap has more than stones), otherwise;
- if leaving heaps is permitted (take stones from the heap and split the rest into two non-empty heaps), otherwise.
As an example, Dawson's chess has the octal game notation :
- Removing stone is only possible when it's the only stone in the pile, so .
- Removing stones is possible at any time, but it doesn't allow splitting the pile, so .
- Removing stones is possible at any time, including splitting the pile, so .
The regular Nim has octal game notation . Kayles has octal game notation .
Octal games have further variants. The first digit, corresponding to removing stones, may be set into to indicate that splitting a heap into two (without taking any stones) is a permitted move. By using a higher base, it's possible to express games that allow splitting a heap into three or more heaps.
Index- Nim is a variant of Nim where a player is allowed to take stones from more than one pile in a single turn. This can be combined with subtraction game, limiting the total number of stones removed or the number of stones removed from a pile.
Wythoff's game is a variant of Index- Nim, where the number of stones removed from all piles affected must be the same. Usually, Wythoff's game is played with only two piles. The strategy is very different; the two-pile game is solved, involving Beatty's sequences using and .
Grundy's game is even more different in that stones cannot be taken. The only allowed move is to split a pile into two differently sized piles. This will still end the game since the number of piles will be limited by the number of stones and it keeps increasing.
Unbalanced Nim has the extra rule that any two heaps may not be equal in size. Heaps of zero objects may or may not be counted as heaps; in the former option, this makes the ending position different, as where is the number of heaps.
Even more quirky variants are greedy Nim, where a player may only take stones from the currently largest pile—any such largest pile if there are many—and building Nim, where the players begin with placing stones into piles before playing a game of Nim with the result, so that the starting position is not known until the end of the building phase.
The Sprague-Grundy theorem is more fully developed out on this page.
The Sprague-Grundy theorem
Any position of an impartial game is equivalent to a Nim pile of a certain size.
Two players play a game according to the following rules:
They place three heaps of coins on a table. The first heap has coins, the second heap has coins, and the third heap has coins.
The second player adds another pile of coins on the table, having at most coins.
The players take turns alternately, starting with the first player. At each move, the player has to remove a positive number of coins from one heap. The player who removes the last coin wins.
It turns out that regardless of the strategy of the first player, the second player always wins with optimal play. How many coins should the second player add in the fourth pile?
Dan and Sam play a game on a cube that consists of 125 objects; each one must take at least one object and at most a whole heap, in his turn.
As an explicit example, in the first turn, Dan can take just the bottom front left corner object, or the whole vertical central heap, or the whole bottom front horizontal heap. Thus, he can take any number of objects of one of the 125 initial heaps (heaps cannot be diagonals).
The winner is the one who takes the last object. If Dan begins, who will win? This means, who has a winning strategy?
This is the fifteenth problem of the set Winning Strategies.
Dan and Sam play a game on a grid, in which one takes squares (red) and the other takes circles (blue). Once the game starts, they take turns moving a single piece in their turn. Each piece can only be moved straight forward or backward, any number of grids (and cannot skip over the opponent's piece). This is the initial position:
A player loses when he is not able to move any of his pieces in his turn. If Dan goes first, who will win? In other words, who has a winning strategy?
This is the twelfth problem of the set Winning Strategies.
Alice and Bob are playing a game called Moving Chips. The game is played on an board where some cells have some chips on it. Two players move alternately. Each turn consists of one person moving a chip to any cell to its left or any cell to its top. For example, the possible movements of chip A on a board are as follows:
The last player who moves all the chips to the top left cell wins.
Consider the configuration below. Alice will move first. Assuming that both players move optimally, who will win the game?
The chips can be stacked on top of one another.
The chips can move past any chips.
Do you have a generalized strategy for this problem? Then you might enjoy the Large Data version!