Ludozio
Games · Board · Nim

Nim

Time 0:00 Moves 0

How to play Nim

Nim pits you against the computer across several rows of tokens. Players take turns clearing part of a single row, and whoever removes the very last token on the board wins. It is a game of pure strategy, one that mathematics has completely solved.

  1. Look at the rows of tokens The board shows several rows of different sizes, for example 1, 3, 5 and 7 tokens. You play against the computer, and you always move first.
  2. Remove tokens from a single row On your turn, pick ONE row and remove as many tokens as you like, at least one. Click a token: it is swept away along with every token to its right in that same row.
  3. Let the computer answer After your move, the computer takes its turn under the same rule, removing tokens from a single row. The two players alternate until the board is empty.
  4. Take the last token to win Whoever removes the very last token on the board wins the game. This is normal play: taking the final piece makes you win, not lose.
Full rules

Nim is played by two, you against the computer, across several rows of tokens (for example 1, 3, 5 and 7). You always move first.

On your turn, pick a single row and remove as many tokens as you like from it, at least one. Click a token: it is swept away along with every token to its right in that same row. Hover to preview what will go. The computer then answers. Whoever removes the very last token on the board wins the game.

The computer uses Sprague-Grundy theory: it computes the "nim-sum" (the exclusive-OR of the row sizes) and, whenever it can, leaves a position whose nim-sum is zero, which is unbeatable. On easy it plays at random, on medium it plays perfectly about six times out of ten, on hard it never errs. The opening deal always leaves you a winning line, your job is to find it. Undo to take a move back, or ask for a hint to see the optimal move.

Playing tips
Aim for a balanced position

Nim is a solved game: at any moment one of the two positions is winning. Your goal is to hand the computer a layout it cannot escape from, then keep it trapped.

Work out the nim-sum of the rows

The winning idea is the binary exclusive-OR (nim-sum) of the row sizes. Write each size in binary and add column by column without carrying: if the total is zero, the position is losing for whoever must move.

Always bring the nim-sum back to zero

On your turn, adjust a single row so the nim-sum becomes zero. Such a move always exists when the position is not already zero; that is exactly what the computer does on hard.

Watch the endgame parity

When only single-token rows remain, count them: an odd number of rows wins for you under normal play. Keep this endgame in mind before rushing to grab tokens.

Frequently asked questions

Does taking the last token win or lose?

Here, removing the very last token on the board wins: this is normal Nim. It is not the misere variant where the final token would lose. So clear the board to your own advantage.

Is there a perfect strategy?

Yes, Nim is a completely solved game. The optimal strategy is to leave, after every move, a position whose nim-sum (the exclusive-OR of the row sizes) is zero. Whoever manages that wins for certain.

Can I take tokens from more than one row at once?

No. Each turn you touch a single row only, but you may take as many tokens from it as you wish, from one up to the whole row. You must remove at least one token.

How do the computer levels differ?

On easy the computer plays at random; on medium it plays perfectly about six times out of ten; on hard it never errs. The opening deal always leaves you a winning line, even against the hard level.

Do I need an account to play?

No account or sign-up is required. Just open the page and start playing. You can undo a move or ask for a hint to see the optimal play whenever you get stuck.

Can I play offline?

Yes. Once the page has loaded, everything runs in your browser with no connection needed. Since you play against the computer, there is no daily puzzle: start as many games as you like.

Come back tomorrow Five puzzles a day, the same for everyone. See today's five
This grid
LevelHard