GenerateRandomSearch

Nim Position Solver

Nim is one of the very few games that is completely solved, and the solution is a single line: take the exclusive or of the heap sizes. If it is anything but zero the player to move wins, and the winning move is the one that makes it zero. This works that out for your position, lists every move that wins rather than just one, and handles the misere version where taking the last object loses instead.

What this generator does

Computes the exclusive or of the heap sizes and, for each heap, the size it would have to drop to for that total to become zero. Where that size is smaller than the heap, the move is a winning one and it is listed.

How to use this tool

  1. Enter your heap sizes, separated by commas.
  2. Press Solve.
  3. Read whether the player to move is winning.
  4. Take any of the moves listed — each of them leaves the opponent lost.

Understanding the controls

Heap sizes
How many objects are in each heap, separated by commas. Up to eight heaps, and a heap may be empty — an empty heap changes nothing, which the nim-sum shows directly.

Common use cases

  • Checking whether a Nim position is already lost before you move
  • Finding every winning move rather than settling for the first one
  • Learning the nim-sum rule with positions you can verify
  • Settling an argument about the matchstick game at the end of a film
  • Seeing where misere play differs from normal play, and where it does not

How this generator works

Nim's solution is that a position is lost for the player to move exactly when the exclusive or of the heap sizes is zero. Any move from such a position must break that, and from any position where it is not zero there is always a move restoring it. To find those moves the tool takes each heap in turn and computes the heap exclusive-or the total: where the answer is smaller than the heap it is a legal move, and it leaves the total at zero. Before display every listed move is applied to the heaps and the resulting nim-sum recomputed, and every possible move is enumerated to confirm none was missed.

Randomness and fairness

Nothing here is random. A position has exactly one answer, and this computes it rather than searching or guessing.

For how randomness is produced across the whole site, see how Generate Random works.

Limitations and good to know

  • This is Nim itself, where a player may take any number from one heap. Variants that cap the take are subtraction games, which the Grundy value solver handles.
  • Misere play differs only when every heap holds at most one object; the tool says so rather than leaving it implicit.
  • Knowing the winning move does not help from a lost position — against an opponent who also knows the rule, a zero nim-sum is simply lost.
  • Heaps are capped at eight because that is well past the point where the arithmetic stops being the interesting part.

Privacy and your data

The calculation runs in your browser and the heap sizes you enter are never transmitted, stored or included in analytics.