Nim with matchsticks, against the computer or a friend
This Nim game online is the classic take-away game: two players remove matches in turn, and everything comes down to the last one. There are two versions. One row lines up 5 to 40 matches (21 by default) and each player takes 1, 2 or 3. Several piles sets the matches out in piles of 1, 3, 5 and 7, the layout most people picture when they think of Nim, or in piles of 3, 4 and 5: take as many as you like, but from a single pile. The Last match setting decides whether whoever takes it loses or wins.
Play against Computer, easy, which misses chances, Computer, hard, which never misses a winning move, or choose Two players on this device for a quick duel at the kitchen table or in a maths lesson.
How to play
- Pick One row or Several piles, then the last-match rule, the opponent and Who starts (you, the computer or random).
- Tap a match: it is selected together with every match after it in the same pile. In a row, the selection stops at 3.
- Press Take. In a row, the Take straight away buttons 1, 2 and 3 are quicker.
- With a keyboard, focus the board: left and right arrows set how many, up and down switch pile, digits 1 to 9 work too, and Enter takes.
The matches your opponent just removed stay faded until the next move. An unfinished game is restored when you come back, and Statistics keeps your wins and losses against the computer for each variant and level. When a game ends, the tool shows the winning strategy and the move that decided the result.
A little history
Games where players take objects away in turn are old and turn up in many forms, and nobody can point to a single origin. The name Nim and the complete solution, based on binary numbers, were published at the very start of the twentieth century.
Winning with a single row
Everything hinges on the number 4: whatever your opponent takes (1, 2 or 3), you can make the total up to 4.
- If the last match wins, always leave a multiple of 4: 20, 16, 12, 8, 4. With 21 matches, the first player takes 1 and can no longer lose.
- If the last match loses, leave a multiple of 4 plus 1: 17, 13, 9, 5 and finally 1, which your opponent is forced to take. Since 21 = 4 × 5 + 1, whoever starts with 21 matches loses against someone who knows the trick.
To beat Computer, hard, let it start with 21 matches under the rule Whoever takes it loses, then never slip.
Several piles: the nim-sum
Write each pile in binary, one under the other, and count the 1s in each column: an even count gives 0, an odd count gives 1. The result is called the nim-sum.
| Pile | Binary |
|---|---|
| 1 | 001 |
| 3 | 011 |
| 5 | 101 |
| 7 | 111 |
| Nim-sum | 000 |
Every column holds two 1s, so the nim-sum is 0. A nim-sum of 0 is a losing position for the player to move: any move makes it non-zero, and the opponent can always bring it back to 0. With piles of 3, 4 and 5, the nim-sum is 010, which is 2: the first player wins by taking 2 matches from the pile of 3, leaving 1, 4 and 5, whose nim-sum is 0.
When the last match loses, only the ending changes: play the same way while at least one pile still has 2 or more matches. When your move would leave nothing but single-match piles, leave an odd number of them. Under both rules, 1-3-5-7 is a loss for the player who starts. For another thinking duel where every move counts, try mancala online, or tic tac toe online with younger players.
Frequently asked questions
Does the hard computer cheat?
No. It applies the nim-sum or the count by 4. From a losing position it plays at random, so you can beat it if you make no mistake.
Who wins with 21 matches?
If the last match loses, the second player wins by making each round add up to 4. If the last match wins, the first player wins by taking 1.
Can I take from two piles at once?
No, each move uses a single pile. The selection moves to another pile as soon as you tap one of its matches.
Is it suitable for children?
Yes, as soon as they can count to 20. The row against Computer, easy is a gentle way to discover the multiple of 4; on their own, they can then try peg solitaire.