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.
Key takeaways
- 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.
- 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.
- The winning move cuts one heap so that the nim-sum becomes zero.
- The classic start of 1, 3, 5 and 7 has nim-sum zero, so the first player loses against perfect play.
- 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.
- Wikipedia: NimThe rules, the nim-sum and Bouton’s theory.
- University of Alberta: Nim notesThe nim-sum and the winning strategy.
- Brilliant: NimThe strategy and a worked example.
- Plus Maths: NimBinary and the game.
- CodeProject: the Nim gameFinding the move.
- Wikipedia: NimatronThe Nim machine of 1940.
- History of Information: NimatronAn early machine to play Nim.
- COMSOL: Edward CondonThe person behind the Nimatron.