Nim Games
14 variants · perfect ai · sprague-grundy theory

Nim Games — Mathematical Strategy

Fourteen variants of Nim with perfect mathematical AI. Every move is explained using XOR, Grundy numbers and the Sprague-Grundy theorem.

How to play Nim

Take turns removing objects; play the right move and you can always win — the maths tells you which.

The rules

  1. Objects are arranged in piles (rows).
  2. On your turn, remove any number of objects from one pile.
  3. In normal play, whoever takes the last object wins. In misère play, whoever takes the last object loses.
  4. This site has 14 variants — including Wythoff, Kayles, Turning Turtles, Treblecross, Lasker and Ruler — each with its own move rule.

Playing here

  1. Pick one of the 14 variants and set the piles.
  2. On your turn, choose a pile and how many to take (or split, in the variants that allow it).
  3. The AI plays perfectly and explains every move with nim-sums and Grundy numbers.
  4. Replay any game move by move.

Tips

The full guide, with a worked example and common mistakes: How to play Nim.

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.

Related