How to play Nim
Take turns removing objects; play the right move and you can always win — the maths tells you which.
The rules
- Objects are arranged in piles (rows).
- On your turn, remove any number of objects from one pile.
- In normal play, whoever takes the last object wins. In misère play, whoever takes the last object loses.
- This site has 14 variants — including Wythoff, Kayles, Turning Turtles, Treblecross, Lasker and Ruler — each with its own move rule.
Playing Nim on Puzzle Solvers
- Pick one of the 14 variants and set the piles.
- On your turn, choose a pile and how many to take (or split, in the variants that allow it).
- The AI plays perfectly and explains every move with nim-sums and Grundy numbers.
- Replay any game move by move.
A worked example
Piles of 3, 4 and 5. In binary 011, 100 and 101; XOR gives 010 = 2, not zero, so the player to move can win.
Take 2 from the pile of 3, leaving 1, 4, 5: 001 XOR 100 XOR 101 = 000. Whatever your opponent does now, you can restore a zero nim-sum.
Strategies
- The XOR rule. Write each pile size in binary and combine them with XOR (the nim-sum). If the nim-sum is 0, the player to move will lose against perfect play.
- Make it zero. On your move, reduce one pile so the nim-sum becomes 0. Your opponent then faces a losing position.
- Misère endgame. Play as normal until every pile but one has size 1; then leave an odd number of single piles.
- Other variants. Kayles, Lasker and the others use Grundy numbers — the AI explains each move with the Sprague–Grundy theorem.
Common mistakes
- Playing for 'even piles' by feel — only the binary nim-sum tells you the right move.
- Using normal-play strategy all the way through a misère game.
Where it comes from
Nim is centuries old; its complete mathematical solution was published in 1901, and it became the founding example of combinatorial game theory.
Frequently asked questions
Can you always win at Nim?
If the starting position has a non-zero nim-sum and you move first, yes — by always leaving a nim-sum of zero.
What is the Sprague–Grundy theorem?
Every impartial game position is equivalent to a single Nim pile of some size (its Grundy number), so Nim's XOR rule extends to all of them.
Does the AI explain its moves?
Yes — every move is explained with XOR and Grundy numbers.
Keep going
- Play Nima fresh puzzle every time
- Peg SolitaireJump pegs over each other to remove them; finish with one peg — ideally in…
- CodeBreakerFind the hidden 4-digit code from Bulls and Cows feedback — deduce, don't…
- TaflThe Viking board game: the king must escape to the edge or a corner while…
All guides: How to play every puzzle and game.