How to play Nim
- The tray. The game starts with rows of bubbles. The usual layout is three rows of 3, 5 and 7 bubbles.
- Take turns. Players take turns, and the player who starts swaps after every game.
- Pop from one row. On your turn, choose one or more bubbles from a single row and pop them. You can pop one bubble, the whole row or anything in between, but never bubbles from two rows in the same turn.
- You must pop. You can't pass: every turn pops at least one bubble.
- The last bubble. With the usual rule, the player who pops the last bubble in the tray loses the game. Choose Last bubble: Wins before you start and the player who pops the last bubble wins instead.
- The match. Play one game, best of 3 (the default) or best of 5. Nim has no draws: every game has a winner.
Layouts and options
| Option | Choices |
|---|---|
| Rows | 3, 5, 7 (the default), 1, 3, 5, 7, 3, 4, 5 or Random |
| Last bubble | Loses (the usual rule) or Wins |
| Match | 1 game, best of 3 or best of 5 |
Random deals a new tray for every game: 3 to 5 rows of 1 to 9 bubbles, between 12 and 30 bubbles in all. Half of the random trays are a win for the player who starts and half for the player who goes second, if both play perfectly, so you can't tell in advance which side you're on. You have to work it out.
Controls
| Device | How to play |
|---|---|
| Phone or tablet | Tap the bubbles you want in one row (or slide your finger along the row to choose several), then tap Pop. Tap a chosen bubble again to put it back, or tap Clear |
| Mouse | The same with clicks, or press and drag along a row |
| Keyboard | The arrow keys move between bubbles, Space chooses a bubble or puts it back, Enter or P pops what you've chosen and Esc clears your choice |
Chosen bubbles lift up and wobble. Choosing a bubble in another row starts a new choice in that row. Popped bubbles stay in the tray as dents in the colour of the player who popped them, so you can see how the game went.
Computer levels
The computer sees the same tray you do. Nim is a solved game, so the levels differ in how often the computer uses the winning method.
| Level | How the computer plays |
|---|---|
| Easy | Pops any bubbles at all, except that with 3 or fewer bubbles left, or one row left, it sees the right move. A good first opponent for young players. |
| Medium | Plays the right move when 6 or fewer bubbles are left or only two rows still have bubbles, and about one move in three before that. Otherwise it pops anything. |
| Hard | Knows the winning method, but while the tray is still big (more than 6 bubbles in three or more rows) it slips about one move in five and pops something else. It never slips in the ending. |
| Expert | Never slips. Whenever a winning move exists, it finds it. When it has no winning move, it pops a single bubble from the biggest row, to make the game as long as it can and give you the most chances to go wrong. |
In our tests of single games on the 3, 5, 7 tray, Medium beat Easy 9 games in 10, Hard beat Medium about 4 games in 5 and Expert beat Hard about 3 games in 4.
Can you beat Expert? Yes, but only by playing perfectly. Every Nim position is a win for one player if they never make a mistake. On the 3, 5, 7 tray that's the player who starts: if you start and use the method below on every turn, Expert can't stop you. If Expert starts, it wins. In a best of 3, whoever starts the first game also starts the third, so against Expert you can win the match only in a match you start, by winning both of your games. The first start swaps from one match to the next. On 1, 3, 5, 7 it's the other way round: the player who goes second wins with perfect play.
The winning method
In 1901 the mathematician Charles L. Bouton worked out exactly how to win at Nim, using binary numbers. You don't need to write anything in binary, though. Here's the same idea in plain steps.
- Split each row into 4s, 2s and 1s. Write every row as a sum of the numbers 8, 4, 2 and 1, using each number at most once. A row of 7 is 4 + 2 + 1, a row of 5 is 4 + 1 and a row of 3 is 2 + 1.
- Look for pairs. Count how many rows use a 4, how many use a 2 and how many use a 1 (and an 8, in big trays). If every one of those counts is even, the position is "balanced".
- Leave it balanced. A balanced position is bad for the player who has to move: whatever they pop, they unbalance it. An unbalanced position is good: there's always a move that balances it. So on your turn, find a move that makes every count even again.
An example. The tray 3, 5, 7 is 2 + 1, 4 + 1 and 4 + 2 + 1. There are two 4s and two 2s, but three 1s: unbalanced, so the player to move can win. Pop one bubble from any row. Pop one from the 7, for example, and the rows are 3, 5 and 6, which are 2 + 1, 4 + 1 and 4 + 2: two 4s, two 2s and two 1s. Balanced. Whatever your rival does now, you can balance it again.
Mathematicians call this the nim-sum: add the rows in binary without carrying. A position with a nim-sum of zero is balanced.
The twist when the last bubble loses
The method above wins the game where the last bubble wins. With the usual rule, where the last bubble loses, you play exactly the same way except at one moment near the end. As soon as your move would leave no row with more than one bubble, leave an odd number of single bubbles instead of an even number. Your rival then pops one, you pop one, and so on, until your rival is stuck with the last bubble.
For example, with rows of 1, 1 and 4 to play, the balancing move is to pop the whole row of 4, leaving 1 and 1. That's right when the last bubble wins. When the last bubble loses, pop three bubbles from the 4 instead, leaving 1, 1 and 1: your rival pops one, you pop one, and your rival has to pop the last.
Tips to win
- Learn small positions first. Two equal rows are balanced: whatever your rival pops from one, pop the same from the other. That copycat trick wins a lot of endings on its own. With the usual rule, watch the very end: if your rival leaves one row with a single bubble, pop the whole other row, and if they pop a whole row, leave one bubble in the other.
- Count rows of one. Near the end, rows with a single bubble decide everything. With the usual rule, try to hand your rival an odd number of them.
- Don't rush. Popping a whole row looks bold, but it often hands your rival a balanced position. One bubble is often the right move.
- When you can't win, keep it complicated. In a balanced position every move loses against perfect play, so make a small move and hope your rival slips.
Good for math class
Nim is a favourite in math classrooms and math clubs because the winning method is binary numbers in disguise. Younger players can find the copycat trick with two rows and explain why it works. Older players can work out the 4s, 2s and 1s method, then check it against the computer: a balanced position you leave should make even Expert pop just one bubble from its biggest row, the sign it knows it's losing. Pairs of pupils can share one device. See also our page for parents and teachers, and try Dots and Boxes for another pencil-and-paper strategy game with a hidden trick.
About Nim
Games of taking objects from heaps are very old, and nobody knows who first played them. The name Nim comes from Charles L. Bouton, a mathematics professor at Harvard University, who in 1901 published the complete mathematical theory of the game in the journal Annals of Mathematics, and with it the winning method above. Nim became one of the first games played by machines: a Nim-playing machine called the Nimatron was shown at the New York World's Fair in 1940, and the Nimrod computer played Nim against visitors at the Festival of Britain in 1951.
Our version uses bubbles in a tray instead of matches, coins or counters, and lets you choose the layout, the last-bubble rule and the length of the match.
Questions
What is the game of Nim?
Nim is a two-player strategy game with rows (or heaps) of objects. On each turn a player takes any number of objects from one row. With the usual rule, the player who takes the last object loses; in the other version, the player who takes the last object wins.
Can you always win at Nim?
Not always, but you can always win from a winning position. Every position is either balanced, where the player to move loses against perfect play, or unbalanced, where the player to move can win by balancing it. If you start in an unbalanced position and never slip, you win.
Who goes first on the 3, 5, 7 tray?
The player who starts has the winning move: pop one bubble from any row. That's why the start swaps every game, and why a best of 3 is fairer than one game.
Is the Expert computer unbeatable?
Expert never makes a mistake, but it can still lose. When you start in a winning position and play perfectly every turn, Expert loses. When Expert starts in a winning position, you can't beat it.
What's the difference between the two last-bubble rules?
With Loses (often called misère play), the player who pops the last bubble loses. With Wins (often called normal play), the player who pops the last bubble wins. The winning method is the same until the very end; see the twist above.
Updated 22 November 2026