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.