Nim is the classic game of taking matches. Rows of matches sit on the table, each player takes as many as they like from one row, and the last match decides the game. Here you play a three-level computer or a friend, with a binary helper that shows the math behind every move.
Nim game rules: take from one row
The rules of Nim fit in two sentences. On your turn, take at least one match, and as many as you like, but all from the same row. Players take turns until the table is empty.
What the last match means depends on the rule you choose under Last match. With Wins, called normal play, whoever takes the last match wins. With Loses, often called misère play, whoever takes it loses. There's no luck at all: no dice, no cards, and both players see every match.
Classic 1, 3, 5, 7 or random rows
Under Rows, pick one of two starts:
- 1, 3, 5, 7: the classic layout, 16 matches in four rows, as played in the 1961 film Last Year at Marienbad.
- Random: 3 to 5 rows of 1 to 9 matches each, with at least 12 matches in all. Each new game deals a fresh set.
Against perfect play, the classic layout is a loss for whoever moves first. With random rows, sometimes the first player can force a win and sometimes the second can, and the binary trick below is how you tell.
How Easy, Medium, and Hard take matches
Under Opponent, play the computer, or choose 2 players to share one phone, tablet, or computer with a friend. Against the computer, First move decides who starts, and there are three levels:
- Easy plays the winning move only about 3 times in 10 when it has one, and otherwise takes matches at random.
- Medium finds the winning move about 8 times in 10, and every time once 8 or fewer matches are left.
- Hard never misses a winning move. When it has none, it takes one match from the biggest row and waits for you to slip.
Each opponent keeps its own score: one for each computer level and one for two players. In 2-player mode Player 1 always moves first, so take turns starting.
Tap a match to choose how many to take
Tap or click a match to pick it and every match to its right, then tap Take or tap the same match again. With a mouse, hovering shows what a click would pick.
- Keyboard: Tab to the matches, move with the arrow keys, and press Enter to pick and Enter again to take. Escape clears your pick, H shows a hint, U undoes, and N starts a new game.
- Screen readers: every match has a label, such as row 4, match 5 of 7, take 3 matches, and each move is announced.
- Undo: takes back your last move, and against the computer its reply too, so it's your turn again.
How to win the Nim game: the nim-sum
Nim has a complete solution, and you can work it out in your head. Split every row into powers of two: 8s, 4s, 2s, and 1s. A row of 7 is 4 + 2 + 1, a row of 5 is 4 + 1, and a row of 3 is 2 + 1. Then count how many rows contain each power.
If every power turns up an even number of times, the player to move loses against perfect play. Mathematicians say the nim-sum is 0: it's the row sizes added in binary without carrying, also called exclusive or. If any power turns up an odd number of times, the player to move can win by taking matches so that every count is even again.
Take rows of 3, 4, and 5, which split into 2 + 1, 4, and 4 + 1. The 4s and the 1s come in pairs, but the 2 is alone. Taking 2 from the row of 3 leaves 1, 4, and 5, with a nim-sum of 0. Every reply your opponent makes breaks a pair, and you mend it on your next move, all the way to the last match.
The binary helper and hints
Turn on Binary helper to watch the trick at work. A table under the matches writes every row in binary, puts the nim-sum on the bottom line, and marks each column with an odd number of 1s. A note says whether the player to move wins or loses with perfect play.
Hint goes a step further. It lights up a winning move and says why, or tells you no move wins for sure because the nim-sum is already 0. On 1, 3, 5, 7, the 4s, 2s, and 1s all pair up, so the first player loses against perfect play. To beat Hard there, set First move to Computer.
Last one loses: misère Nim
Misère play sounds like it needs a whole new strategy, but it doesn't. Play exactly as in normal Nim, keeping the nim-sum at 0, until your move would leave only rows of one match. Then do the opposite: leave an odd number of single matches, not an even number, and your opponent has to take the last one.
Say the rows are 1, 1, and 3 and it's your move. In normal play you'd take all 3, leaving two single matches. In misère play, take 2 instead, leaving three single matches, and the last one falls to your opponent.
Habits that beat Easy and Medium
- Play with the binary helper on until you can spot an odd column yourself.
- With two rows left, make them equal, then copy every move on the other row.
- Learn a few positions to leave behind: 1, 2, 3 and 1, 4, 5 and 2, 4, 6 all have a nim-sum of 0.
- Two equal rows cancel out, so you can ignore them while you work out the rest.
- When you're losing, take one match and keep the game long. Easy and Medium pick a random move whenever they miss the winning one.
- Against Medium, reach a nim-sum of 0 before the table is down to 8 matches. From there it never misses.
From Bouton to the Nimatron
Games of taking stones or sticks from piles are very old. Nim is often said to come from China, but nobody knows for sure, and the earliest European references date from the early 1500s. The game got its name and its solution in 1901, when Charles L. Bouton of Harvard published Nim, a Game with a Complete Mathematical Theory. The Oxford English Dictionary traces the name to the German nimm, meaning take.
With such a simple winning rule, Nim suited early machines. The Nimatron, conceived by physicist Edward Condon, was shown by Westinghouse at the New York World's Fair in 1940, and few visitors beat it. In 1951 Ferranti showed a Nim-playing computer, Nimrod, at the Festival of Britain. In 1935 and 1939, Roland Sprague and Patrick Grundy each showed that every game where both players have the same moves behaves like a single row of Nim, which put Nim at the center of the math of games. Chomp is one of those games, yet it has no known formula.
Coming back to a half-played game
No account is needed. The page needs a connection to load, and from then on every hint and computer reply is worked out in your browser. Your moves and scores aren't sent to a server.
Your settings, a score against each computer level, the two-player score, and any unfinished game are kept in this browser's local storage, so a half-played 1, 3, 5, 7 is still on the table when you come back. Reset scores wipes every score at once, and the privacy policy lists the rest. For more pure strategy, try Dots and Boxes.