Nim
A mathematically solvable impartial game with an optimal XOR-based computer opponent.
Nim Heaps
How to Play
- The game starts with several heaps containing different numbers of tokens.
- On your turn, choose exactly one heap.
- Remove one or more tokens from that heap; you may remove the entire heap if you wish.
- You may not remove tokens from more than one heap in the same turn.
- Under the normal-play rule used here, the player who removes the final token wins.
The Nim-Sum
Write each heap size in binary and XOR the values together. This result is the Nim-sum. A position with Nim-sum zero is losing if the opponent plays perfectly; a non-zero position has at least one move that makes the Nim-sum zero for the opponent.
Strategy Tips
- Do not choose a move only because it removes many tokens.
- Try to leave a zero Nim-sum after your turn.
- If the position already has Nim-sum zero, no move can preserve that property; your goal becomes creating the most difficult practical response.
- Compare your intuition with the Optimal AI to see how the mathematical strategy behaves.
Game-Theory Connection
Nim is one of the clearest examples of a game with a complete mathematical solution. It demonstrates winning and losing states, backward induction, invariant-based strategy and how a compact algebraic representation can replace brute-force search.