lans.cloud Tools

Nim Game Online — 2 Player

Play Nim online free — the classic 2-player matchstick game, or take on an unbeatable computer. Learn the nim-sum trick to always win the 21 game.

Set up your game

One pile of 21. Take 1, 2, or 3 stones each turn.

Normal play: take the last stone and you WIN.

Take turns on one screen, passing the phone or tablet back and forth.

Back up or move it to another device.

Was this tool helpful?
Support this site

The Oldest Strategy Game You Can Solve on Paper

Nim is the classic pencil-and-paper matchstick game: lay out a few rows of stones (or matchsticks, coins, or tally marks), and two players take turns removing as many as they like from a single row. Take the last stone and you win — or, in the misère version, you lose. That is the entire rulebook, and yet Nim is one of the very few games that is completely solved: from any position there is a known perfect move, and this page will teach it to you and then let you test it against a computer that never gets it wrong.

How to Always Win: the Nim-Sum

The secret is the nim-sum — the bitwise XOR of the row sizes. Write each row in binary, stack the numbers, and add each column with no carrying: a column is 1 if an odd number of rows have a stone in that place. If the nim-sum comes out to 0 the position is balanced, and whoever has to move is losing. If it is anything else, there is always exactly one row you can shrink to make the nim-sum 0 — that is your winning move. Take the classic 1-3-5-7 start: its nim-sum is already 0, so surprisingly the second player wins that one with perfect play. The worked-examples table below is generated by the same engine that runs the game, so every move it lists is genuinely optimal.

The 21 Game Shortcut: Leave Multiples of Four

The most famous Nim variant is the 21 game: one pile of 21, and each turn you take 1, 2, or 3. Here the XOR trick collapses into a much simpler rhythm — always leave a multiple of 4. Whatever your opponent removes, you take enough to make the round total 4, dragging them down 20 → 16 → 12 → 8 → 4 → 0. Moving first from 21 you win by taking 1; but sit down at a pile of 20 and you have already lost. Flip to misère (last stone loses) and the target shifts by one: leave 1, 5, 9, 13, 17, or 21. Both lists in the strategy table below are computed, not typed.

Play the Unbeatable Computer

Reading the strategy is one thing; surviving it is another. Switch the opponent to vs Computer and every move it makes is the perfect one — the proof of this article is that you can sit across from it. Or grab a friend for a two-player, same-screen match and try the trick on them. When you have had your fill of solved games, the same cluster has a genuinely unsolved one in Hex for 2 players, a team battle in the four in a row review game, and if you would rather leave it to chance, a dice roller and a random number generator.

21 game: positions to leave your opponent

RulePositions to leave your opponentWhy
Last take wins (normal)4, 8, 12, 16, 20Leave a multiple of 4; whatever they take (1–3), top it back up to 4 and walk them to zero.
Last take loses (misère)1, 5, 9, 13, 17, 21Leave one more than a multiple of 4, so the final lone stone is always theirs to take.

Computed by the same engine the computer opponent uses — the losing (P-)positions are derived by running the game backward from an empty pile, so this table can never drift from how the game actually plays.

Nim-sum worked examples (classic 1-3-5-7 and friends)

Position (rows)In binaryNim-sumWinning move
1 · 3 · 5 · 7001 · 011 · 101 · 1110Balanced — the player to move is losing
1 · 3 · 5 · 6001 · 011 · 101 · 1101Take 1 from the 1-stone row → 0·3·5·6
3 · 5 · 7011 · 101 · 1111Take 1 from the 3-stone row → 2·5·7
2 · 4 · 6010 · 100 · 1100Balanced — the player to move is losing

Each nim-sum is the XOR of the rows; a nim-sum of 0 means the player to move is losing. Winning moves are taken straight from the game's optimalMove function.

Frequently Asked Questions

How do you always win the 21 game?

Always leave your opponent a multiple of 4 stones. Whatever they take — 1, 2, or 3 — you take enough to bring the total removed that round to 4, so you march them down 20, 16, 12, 8, 4 and then take the last stone. The catch: if the game starts at 21 and you move first you can win (take 1 to leave 20), but if it starts at 20 the first player is already lost. In the misère version, where taking the last stone loses, leave one more than a multiple of 4 instead — 1, 5, 9, 13, 17, 21.

What is a nim-sum?

The nim-sum is the bitwise XOR of all the heap sizes — you write each heap in binary, line the columns up, and a column is 1 if an odd number of heaps have that bit set. If the nim-sum is 0 the position is balanced and the player to move is losing; if it is not 0 there is always a move that makes it 0, and that is the winning move. It is exact, computable math, which is why a computer can play Nim perfectly.

What is misère Nim?

Misère Nim flips the goal: whoever takes the LAST stone loses. It plays identically to normal Nim right up until the endgame — as soon as only single-stone heaps would remain, you switch to leaving your opponent an odd number of those lone stones instead of balancing the nim-sum to zero. That one twist is the whole difference, and the game and its strategy tables here handle it exactly.

Can I play Nim against a computer?

Yes. Choose "vs Computer" and you face an opponent that plays perfectly — it computes the nim-sum (or, in the 21 game, the multiple-of-4 target) every turn and never makes a mistake. If the starting position is a first-player win and you play flawlessly you can still beat it; the moment you slip, it punishes you. It is the article on this page made playable.

Is Nim solved?

Completely. Charles Bouton published the full solution in 1901: the nim-sum tells you who is winning from any position and exactly what to do. Every capped and misère variant here is solved too, and the computer opponent plays those solutions move for move — which is why the "positions to leave your opponent" table below is generated by the same engine the computer uses, not typed in by hand.