Nim

Play full screen
Rate this game

No ratings yet. Be the first.

How to play

Nim is played with four rows of matches: 1, 3, 5 and 7. Players take turns. On your turn, pick one row and take as many matches as you like from it, at least one. Tap a match: it and every match to its right are taken. The player who takes the last match wins.

That is the whole game. The first player moves first, and against the computer you are the first player. Hint shows a winning move when one exists, and Undo takes back one move against a friend, or your move together with the computer’s answer. Choose Friend at the top to play two people on one device.

Strategy

Nim is a solved game, and the solution is one of the prettiest tricks in mathematics.

  • Write the row sizes in binary and add the columns without carrying (this is called the nim sum, or XOR). For 1, 3, 5, 7: 001, 011, 101, 111. Every column has an even number of ones, so the nim sum is 0.
  • A position with nim sum 0 is lost for the player to move. Whatever they take, the sum becomes non-zero, and the opponent can always restore it to 0.
  • To win, always leave a nim sum of 0. Find a row whose size changes the sum back to zero and reduce it to the right number.
  • The opening position 1-3-5-7 has nim sum 0, so the second player wins with perfect play. The computer on the hard level never makes a mistake once it is ahead; on easy it sometimes does. Playing first, your best chance is to wait for a slip and then never let the sum leave zero again.
  • Endgame shortcut. When only rows of size 1 remain, leave an even number of them. With two rows of equal size, mirror your opponent.

If you enjoy Nim, try Nine Men’s Morris or Okey next.

Frequently asked questions

Can I take from two rows in one turn?

No. One row per turn, any number of matches from it.

Is taking the last match good or bad?

In this version it is good: the player who takes the last match wins. (The variant where it loses is called misère Nim.)

Why does the computer always win when it moves second?

Because 1-3-5-7 is a losing position for the first player. Try a few games on easy to see the trick in action, then look for the computer's mistakes.

Add Nim to your website

Free, no sign-up. Paste the code into any page. The credit line under the game is optional, but we appreciate it. On WordPress, use our plugin and the shortcode.

Embedding rules and options