Skip to main content

Nim

One heap per turn. Decide how many stones to take, and plan who will face the final stone.

Moves0

Left9

Taking the last stone wins.

You · A to move

 

Select one heap, choose how many, then Take. Tab + Enter or Space also works.

About Nim

Nim is an impartial strategy game: both players use the same heaps and can make the same kinds of moves. Each move removes at least one stone from a single heap. Nothing is added back, so every match ends. This original browser version is free and needs no account or download.

Normal play rewards taking the last stone. Misère play reverses that ending, so a plan that wins under one rule may lose under the other. The rule stays visible while you play.

How to play Nim

  1. 01Players alternate turns. Select one nonempty heap, choose an amount from one to the whole heap, then confirm Take. You cannot take from two heaps or pass.
  2. 02Win last means the person who takes the final stone wins. Lose last is misère Nim: taking the final stone loses. The selected rule is always shown above the heaps.
  3. 03Choose three, four or five heaps. A layout change uses a fixed starting pattern. Random creates a fresh random setup with one to seven stones per heap; Restart replays the same setup.
  4. 04Play against the computer as A, or take turns as A and B on the same device. Setup chooses who starts and the computer strength.
  5. 05Undo removes one local move, or your latest move and the computer reply. Legal history is saved in this browser when storage is available. Selecting a heap or amount does not make a move.

Nim controls

Mouse or touch
Choose a heap, adjust the quantity and confirm Take. A heap selection is only a preview; you can change it before committing.
Keyboard
Tab through heap buttons and the quantity control, then Enter or Space to confirm.
Undo and replay
Undo removes one local move, or your last move and every computer reply. Restart reuses the initial heaps; Random changes them.

Nim: modes and scoring

These are strategy matches, not numbered levels. Fixed starts have 3, 4 or 5 heaps: 1/3/5, 1/3/5/7 and 3/4/5/6/7 stones. Random starts use one to seven per heap. Both last-stone rules and either starting player can be used.

Easy chooses among legal moves. Optimal uses the exact Nim-sum strategy and adjusts for the all-singleton misère ending. It cannot force a win from every starting position. Settings and legal move history resume locally when storage is available.

Nim tips and strategies

Pair the heaps

In normal play, two equal heaps let the responding player copy each removal in the other heap. This keeps the totals paired.

Read the final singles

With only single stones left, every turn removes one. Under Win last, an odd count favors the player to move; under Lose last, an even count does.

Understand Nim-sum

For normal Nim, bitwise XOR of the heap sizes identifies the position: zero favors the responding player with correct play. For misère Nim this changes when only heaps of size one remain.

  • Look at the end position before you take a whole heap.
  • In normal Nim, paired equal heaps can be answered with matching moves.
  • When every heap has one stone, count parity and check which last-stone rule is active.

Nim FAQ

Is this the single-pile coding puzzle?

No. This is multi-heap Nim with unrestricted removal from one heap per turn, with both normal and misère endings.

Can two people play online?

Two people can share one device. There are no online rooms or remote matches.

Can I beat the optimal computer?

From a losing position, no strategy can force a win against perfect replies. From a winning position you can keep the advantage by making correct moves. Easy chooses random legal moves.

Does a random setup guarantee I can win?

No. Either player may start with the theoretical advantage. Change who starts or replay a position to explore another strategy.

Does it save online?

Only this browser stores the settings, initial heaps and legal history. No account or cross-device sync is used.

How is this different from Mancala?

Nim permanently removes stones from one shared heap. Mancala sows a whole pit around a ring and scores seeds in separate stores.