Chapter 2 · Game

Nim

← Chapter 2
Nim-sum

Click a token: it and every token to its right are taken.

How to play

  1. The game starts with several heaps holding different numbers of tokens.
  2. On your turn you pick exactly one heap and take one or more tokens from it; you may take the whole heap.
  3. Click a token on the board: it and all tokens to its right are taken (hovering shows a preview).
  4. Under the normal play rule used here, the player who takes the last token wins.

The nim-sum

Write each heap size in binary and combine the values with XOR. The result is the nim-sum. A position with nim-sum zero is lost for the player to move if the opponent plays perfectly; from any position with nonzero nim-sum there is at least one move that leaves the opponent a nim-sum of zero. Tick Binary + nim-sum to show the binary columns during play.

Strategy tips

  • Do not pick a move just because it takes many tokens.
  • The goal is to leave a nim-sum of zero after your own move.
  • If the nim-sum is already zero, no move can keep it that way; the aim becomes leaving the opponent the hardest practical reply.
  • Comparing your intuition with the Optimal computer shows how the mathematical strategy works.

Link to game theory

Nim is one of the clearest examples of a game with a complete mathematical solution. It illustrates winning and losing positions, backward induction, invariant-based strategies, and how a short algebraic description can replace brute-force search.