Nim, explained.
Nim is a two-player game of removing objects from piles, with a complete strategy for the normal rule in which taking the last object wins.
Why it happens
A move changes only one pile. The bitwise XOR of pile sizes identifies balanced positions: from a nonzero nim-sum, a legal move can make it zero, while any legal move from zero makes it nonzero.
Normal-play Nim has positions from which a perfect opponent can force a win. The XOR, or nim-sum, identifies the balanced positions.
Read the result
Watch the remaining pile sizes after the bot responds. It makes the nim-sum zero whenever possible. Try a different opening after Reset and compare the positions you leave, rather than only how many stones you take.
A worked example
A winning opening
Piles 3, 4 and 5 have nim-sum 3 XOR 4 XOR 5 = 2.
Removing two from the first pile leaves 1, 4 and 5, whose XOR is zero.
Against perfect replies, preserving that balanced position after your moves gives a route to taking the last stone.
OPTIONAL DEEPER DETAILGo deeper: inside the model
Inside this model
The bot removes stones to make the nim-sum zero when possible, otherwise takes one stone. Both players use the same legal moves and the normal last-stone-wins rule.
Where this idea is useful
A practical use
A visible move can be evaluated by the future options it leaves, rather than only by its immediate size.
A common misconception
“Taking the largest number of stones is always best.”
The structure of the remaining piles matters. A smaller removal can leave the opponent a losing position.
What this explanation leaves out
- This is normal-play Nim. Changing the last-stone rule changes the strategy; the bot plays optimally.
Does this strategy also apply when taking the last stone loses?
Not unchanged. That misère variant needs different treatment near the end of the game.
Can you identify a move that improves your future position without maximizing the immediate gain?
Associated thinkers
Further reading
Explore the original research or the teaching reference behind this experiment.