Nim

The classic take-away math game vs the computer — take the last token to win. Normal & misère modes, three levels, ranked.

How to play Nim

Nim is one of the oldest and most famous mathematical strategy games in the world. Two players face a table with several heaps of tokens, and they take turns removing tokens until none are left. The rules take ten seconds to learn, yet behind them hides one of the most beautiful ideas in game theory — a secret based on binary numbers that lets a player who knows it win every winnable game. In this version you play against the computer at three difficulty levels, in either the classic “normal play” mode or the trickier “misère” mode, and every win earns you ranking points on the leaderboard.

The goal

In the default NORMAL play mode, the player who takes the LAST token from the table WINS the game. If you switch to misère mode, the goal flips: the player forced to take the very last token LOSES. Make sure you check which mode you are in before planning your endgame — the message above the heaps always reminds you of the current rule.

The setup

Each round starts with 3 to 5 heaps, each holding a random number of tokens (1 to 9). The heaps are labelled A, B, C… and the number next to each label shows how many tokens it still holds. Every round is generated from a fresh random seed, so no two games are alike. You always move first.

How a turn works

  • On your turn you must remove AT LEAST ONE token — passing is never allowed.
  • All tokens you remove in one turn must come from ONE single heap. You may take one token, several, or the whole heap.
  • To move, tap any token in a heap: it selects that token and every token to its right. Fine-tune the amount with the + / − stepper, then press Confirm. You can also tap the same token again or press Cancel to change your mind.
  • Players alternate turns until the table is empty. In normal play the person who empties the table wins; in misère play they lose.

Normal vs misère mode

Normal play (take the last token to WIN) is the standard modern rule and the default here. Misère play (take the last token and you LOSE) is the older parlour version. Strategically the two games are almost identical for most of the game — the perfect strategy only changes at the very end, when the heaps are almost gone. Concretely: play misère exactly like normal Nim until your move would leave only heaps of a single token; at that point leave an ODD number of one-token heaps instead of an even number, so your opponent is forced to pick up the final token. You can switch modes with the selector above the board; changing mode starts a new round.

The secret: the binary xor trick (nim-sum)

Nim was completely solved in 1901, and the solution is wonderfully simple once you see it. Write each heap size in binary — for example 5 is 101, 3 is 011, 6 is 110. Now “add” the heap sizes column by column WITHOUT carrying: each binary column is 1 if it contains an odd number of 1s, and 0 if it contains an even number. This carry-less sum is called the nim-sum (in computing it is the XOR operation). Example: 5 ⊕ 3 ⊕ 6 = 101 ⊕ 011 ⊕ 110 = 000 — a nim-sum of zero.

Here is the whole strategy: a position is LOSING for the player about to move exactly when the nim-sum of all heaps is ZERO. If the nim-sum is not zero, there is always at least one move that makes it zero — make that move, hand your opponent a zero position, and whatever they do the nim-sum becomes non-zero again, letting you repeat the trick until you take the last token. An easy special case: with exactly two heaps, simply keep making them equal — two equal heaps have nim-sum zero, and you can mirror every move your opponent makes.

Playing the computer (ranked)

Pick a difficulty before you start. Easy plays mostly random moves and is great for learning the rules. Normal plays the perfect mathematical move about 60% of the time, so it punishes big mistakes but leaves you openings. Expert plays the flawless xor strategy: whenever the position is mathematically winnable for it, it WILL win — so to beat Expert you must play perfectly from a starting position that favours you, and never hand back a zero nim-sum. The computer runs entirely on your device and works offline. Wins are worth 10 (Easy), 30 (Normal) or 100 (Expert) ranking points; sign in and your best score appears on the leaderboard.

Strategy tips

  • Learn to spot small zero positions by heart: two equal heaps (like 4 and 4), or the classic 1-2-3, are lost for whoever must move. Try to hand your opponent exactly these.
  • With two heaps left, mirror your opponent: make the heaps equal on every one of your turns, and (in normal play) you will inevitably take the last token.
  • Practise the binary check: heaps 5, 3, 6 have nim-sum 0 — a trap for the mover. Heaps 5, 3, 4 have nim-sum 2, and there is always a heap you can shrink to restore zero — find which one!
  • In misère mode, keep playing “normal” strategy while any heap has 2+ tokens — only switch your thinking at the very end, leaving your opponent an odd number of single-token heaps.

Frequently asked questions

Do I win or lose by taking the last token?

It depends on the mode. In the default NORMAL play, taking the last token WINS — this is stated on screen during the game. In misère mode, taking the last token LOSES. The status line above the heaps always shows the active rule, and you can switch modes with the Mode selector (which starts a fresh round).

Can the Expert computer always be beaten?

Not always — and that is the honest math of Nim. Expert plays perfectly, so if the random starting heaps have a nim-sum of zero when your first move comes up, every move you make gives Expert a winning position and it will convert it. But when the starting nim-sum is NOT zero (most rounds), you have a guaranteed winning line: find the move that makes the nim-sum zero and keep doing so every turn. If a round feels hopeless, start a new game for a fresh set of heaps.

How is scoring and ranking calculated?

Nim is a vs-Computer ranked game: you earn points only when you WIN a round — 10 points on Easy, 30 on Normal and 100 on Expert. There is no penalty for losing. Your best result per difficulty is kept on the leaderboard once you are signed in. Both normal and misère wins score the same.

Does Nim work offline?

Yes. Once the page has loaded, the whole game — including the computer opponent — runs in your browser with no internet connection. Ranking points earned offline are stored on your device and uploaded automatically the next time you are online and signed in.