Skip to the game
Tic Tac Toe

Nim

Take matches from one row at a time in this Nim game, against a three-level computer or a friend, and grab the last match to win.

Play Nim

You
0
Computer
0

Setting out the matches…

Take the last match to win

On your turn, take one or more matches from a single row. Tap a match to pick it and every match to its right, then tap Take (or the same match again). With a keyboard, move with the arrow keys and press Enter; Escape clears your pick, H gives a hint, U undoes, and N starts a new game.

Game settings
Changing the opponent, difficulty, first move, rows, or rule starts a new game.
Opponent
Difficulty
First move
Rows
Last match
Binary helper

How it works

How to play the Nim game

Each turn, take one or more matches from a single row, and plan ahead so that you take the last match, or leave it for your opponent.

  1. Step 01

    Pick a row

    Tap a match. It and every match to its right in that row are picked, so the match you tap sets how many you take.
  2. Step 02

    Take them

    Tap Take, or the same match again. You must take at least one match, and all of them from one row.
  3. Step 03

    Take the last match

    In normal play, whoever takes the last match wins. Set Last match to Loses to play it the other way round.

Features

Nim with the binary trick built in

  • Easy, Medium, and a Hard level that never slips

    Easy often misses the winning move, Medium finds it most of the time, and Hard never misses it.

  • Pass-and-play Nim for two

    Share one screen with a friend and take turns at the matches. Two-player games keep a score apart from the computer's.

  • Binary helper

    See every row written in binary with the nim-sum underneath, and which columns are odd.

  • A hint for the winning take, and undo

    A hint lights up a winning move or tells you there isn't one. Undo takes your move back.

  • Classic or random rows, either rule

    Play 1, 3, 5, 7 or 3 to 5 random rows, and choose whether the last match wins or loses.

  • Arrow keys and spoken match labels

    Move between matches with the arrow keys, press Enter to pick and take, and hear every move announced.

Strategy

Nim strategy: the binary trick

Nim is one of the few games with a complete solution, and you can do it in your head once you think in powers of two.

  • Split rows into powers of two

    Write each row as 8s, 4s, 2s, and 1s. A row of 7 is 4 + 2 + 1, and a row of 5 is 4 + 1.

  • Leave every power paired

    After your move, each power should appear an even number of times across all the rows. That's a nim-sum of 0.

  • Answer every move

    Whatever your opponent takes leaves some power unpaired. Pair it up again on each turn and the last match is yours.

  • Two rows left? Make them equal

    Even up the two rows, then copy every move your opponent makes on the other row.

  • Last one loses? Switch at the end

    Play the same way until your move would leave only single matches. Then leave an odd number of them, not an even one.

  • Go second on 1, 3, 5, 7

    The classic start already has a nim-sum of 0, so the player who moves first loses against perfect play.

About the game

The Nim game: a matchstick game with a binary secret

The rules, the rows and rule options, the computer levels, controls, the nim-sum strategy, misère play, and where Nim came from.

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.

FAQ

Nim questions

Quick answers about playing the Nim game: the rules, the nim-sum, last-one-loses play, the computer, and saving.

How do you play the Nim game?

Players take turns removing matches. On each turn you take at least one match, as many as you like, from a single row. In normal play whoever takes the last match wins; in last-one-loses play, whoever takes it loses.

Is there a way to always win at Nim?

Yes, whenever the position allows it. Write the row sizes in binary and move so every column has an even number of 1s. If that's already true on your turn, no move wins for sure, and you need your opponent to slip.

What is the nim-sum?

It's the row sizes added in binary without carrying, also called exclusive or (XOR). A nim-sum of 0 means the player to move loses against perfect play. The binary helper works it out as you play.

Who wins 1, 3, 5, 7 Nim?

The second player, with perfect play. The four rows have a nim-sum of 0, so whatever the first player takes, the second can answer. That holds in both normal and last-one-loses play.

How does last-one-loses Nim work?

Whoever takes the last match loses. Play the normal strategy until your move would leave only rows of one match, then leave an odd number of single matches instead of an even number.

Can I beat the Hard computer?

Yes, but only from a winning position and with perfect play, because Hard never misses a winning move. On 1, 3, 5, 7, set First move to Computer and leave a nim-sum of 0 after each of your moves.

How are the computer levels different?

Easy plays the winning move about 3 times in 10 and otherwise takes matches at random. Medium finds it about 8 times in 10, and always once 8 or fewer matches are left. Hard never misses it.

Can two people play Nim on one device?

Yes. Set Opponent to 2 players and pass the device back and forth. Player 1 always takes first, which on 1, 3, 5, 7 is the losing seat with perfect play, so swap seats between games. The two-player score is kept apart from the computer's.

Does Nim remember my scores and an unfinished game?

Yes. This browser keeps your settings, a score for each computer level and one for two players, and the game in progress in local storage. Reset scores clears every score at once.