Nim
Take sticks from one row. Plan ahead to get the last one.
How to play Nim
Nim is a very old game of taking things away. People play it with matches, coins or stones. You only need a few sticks, but you need a sharp mind. Mathematicians love Nim because it has a perfect strategy you can learn.
On this site the sticks are set out in four rows: 1, 3, 5 and 7 sticks. This is the classic layout.
Goal
There are two ways to play. Pick one with the Rule menu before you start.
- Last one wins (normal play): the player who takes the very last stick wins.
- Last one loses (also called misère): the player who has to take the last stick loses.
Turn by turn
- On your turn, choose one row. Take as many sticks as you like from that row. You must take at least one. You may take the whole row.
- You can never take sticks from two rows in the same turn.
- On this site, tap a stick. That stick and all the sticks to its right light up. Then press Take, or tap the same stick again. Tap a different stick to change your choice.
- With a keyboard, use Tab or the arrow keys to move between sticks, Enter to pick, Enter again to take, and Escape to clear.
- Then it is the other player's turn.
End of the game
The game ends when no sticks are left. With "Last one wins", the player who took the last stick is the winner. With "Last one loses", that player loses. There are no draws in Nim.
Playing the computer
Easy picks moves at random. Medium plays the perfect move a little more than half the time. Hard always plays the perfect move when it has one. Hard can still be beaten, but only if you play perfectly too. Choose who goes first, and press Undo if you want to try a turn again.
Strategy tips
- Learn the secret sum. Write each row size as a sum of 4, 2 and 1. For example, 7 = 4 + 2 + 1 and 5 = 4 + 1. Count how many times each of 4, 2 and 1 appears across all rows. If every count is even, the position is safe for the player who just moved.
- Always leave even counts. On your turn, try to take sticks so that every count of 4s, 2s and 1s is even again. Your opponent can never do the same back, and you will take the last stick.
- Pairs cancel out. Two rows with the same number of sticks cancel each other. If you can leave two equal rows and nothing else, just copy your opponent in the other row until you win.
- For "Last one loses", change your plan at the end. Play the same way until only one row has two or more sticks. Then take sticks so that an odd number of rows with one stick is left. Your opponent must take the last one.
- Make it last when you are behind. If you cannot reach a safe position, take just one stick. A longer game gives your opponent more chances to slip.
Nim FAQ
Who wins Nim with perfect play?
With rows of 1, 3, 5 and 7, the player who goes second can always win if they play perfectly. The secret sum of the start position is already even, so the first player has no safe move.
What is misère Nim?
Misère is the “Last one loses” rule. The player who takes the final stick loses. The strategy is almost the same as normal Nim. Only the last few moves change.
Can you beat the computer on Hard?
Yes, but only with perfect play. Go second, or wait for a slip, and always leave the secret sum even. One wrong move and Hard will win.
Is Nim the same as the 21 sticks game?
They are cousins. In the 21 sticks game there is one pile and you take 1 to 3 sticks. In Nim you have several rows and may take any number from one row.