Play Nim — Free
The classic take-away strategy game. Remove any number of tokens from a single row on your turn — take the last token and you win. Can you crack the secret of the Nim-sum and beat a perfect computer?
You win! 🎉
Click a token to take it and every token to its right in that row. Take the last token on the board to win.
What is Nim?
Nim is one of the oldest and most famous mathematical strategy games in the world — a two-player battle of pure logic with rules simple enough to explain in a single breath. You start with several rows, or heaps, of tokens. The two players alternate turns, and on your turn you remove as many tokens as you like — one, a handful, or a whole row at once — but they must all come from a single row. In this "normal play" version, the goal is beautifully clear: take the very last token on the board and you win. There is no board to navigate, no dice, and no luck at all; every position is decided by the choices the two players make.
Behind that simplicity sits a piece of mathematical history. In 1901 the mathematician Charles Bouton published a complete theory of Nim, making it one of the first games ever to be "solved" — proven to have a perfect strategy that decides the winner from any starting position. That strategy rests on a single clever idea called the Nim-sum, and once you understand it you can win every game you are supposed to win and never lose one you should not. The computer on this page plays that perfect strategy, so beating it is a genuine test: your only path to victory is to start from a winning position and then never make a mistake. Learn the trick below and the game transforms from a coin-flip into a puzzle you can master.
How to Play
The rules are short, and only a few moves are against the rules. On any turn a move is illegal — and will not be allowed — when:
- you try to take tokens from more than one row in the same turn — every move stays inside a single row;
- you try to take zero tokens or skip your turn — you must remove at least one token;
- you try to remove more tokens than a row holds — you can empty a row but never overdraw it.
Nim Tips & Strategy
Nim looks like guesswork the first time you play, but it is completely solved — there is an exact rule that tells you the winning move whenever one exists. These techniques take you from tapping randomly to playing like the computer.
Master the Nim-sum (binary XOR)
Write the number of tokens in each row in binary and add them column by column without carrying — a "sum" where two of the same digit cancel out. That result is the Nim-sum. The whole game turns on one fact: if the Nim-sum is not zero, the player to move can win; if it is zero, the player to move will lose to perfect play. Your job every turn is to leave your opponent a position whose Nim-sum is zero, because from there they cannot help but hand it back to you.
Turn a winning position into a zero
When the Nim-sum is not zero you always have a move that drives it to zero, and here is how to find it. Take the Nim-sum, then look for a row whose count becomes smaller when you XOR it with that Nim-sum — reduce that row to exactly that smaller number. For example, from rows of 3, 4 and 5 the Nim-sum is 2, and removing 2 tokens from the first row leaves 1, 4, 5, whose Nim-sum is zero. Make that move and you have taken control of the game.
Read balanced positions and decide who starts
A position whose Nim-sum is already zero is "balanced," and it is a trap for whoever has to move — every option unbalances it and gives the opponent the win. The famous 1-3-5-7 board is balanced, which means the second player wins it with perfect play. So before the game even begins, use the First-move buttons wisely: on a balanced board, let the computer start; on an unbalanced one, take the first move yourself and seize the advantage immediately.
Count carefully in the endgame
When only small rows remain, watch out for heaps of exactly one token. If every row has a single token, the winner is decided by whether the number of rows is odd or even, not by the Nim-sum trick alone. And with just one row left, simply take all of it and claim the last token. The endgame is where sloppy counting throws away a won game, so slow down and confirm your Nim-sum on every move once the board thins out.
The Math Behind Nim
What makes Nim special is that it is not just playable but fully understood. Because there is no hidden information and no chance, every position is either a first-player win or a second-player win, and the Nim-sum tells you which. The reason the trick works is elegant: from any position with a non-zero Nim-sum there is always at least one move to a zero Nim-sum, and from a zero position every possible move produces a non-zero one. A player who always moves to zero therefore keeps handing their opponent losing positions until the tokens run out — and the person forced to move from the empty board is the one who just lost, because their opponent took the last token.
This idea reaches far beyond the game itself. The Sprague–Grundy theorem, a cornerstone of combinatorial game theory, shows that a huge family of turn-based "take-away" games behaves exactly like a single heap of Nim, so learning Nim gives you a master key to countless related puzzles. That is why Nim appears in maths classrooms and computer-science courses around the world: it is small enough to solve by hand, yet it opens the door to a deep and beautiful theory. Play a few rounds against the perfect computer here and you are practising the very reasoning those theorems describe.
Nim vs Tic-Tac-Toe and Other Strategy Games
If you enjoy sharp two-player thinkers like Tic-Tac-Toe, Dots and Boxes, Nine Men's Morris or Reversi, Nim will feel like a purer cousin. Those games ask you to build shapes, capture territory or block lines on a board; Nim strips all of that away and leaves only the cleanest possible decision — how many, and from which row. There is no drawing a game and no defensive shuffling: someone takes the last token, and that someone wins. The tension comes entirely from foresight, which makes each round quick to play yet genuinely satisfying to win.
That focus is exactly why Nim has survived for centuries and still shows up on phones and in classrooms today. It is the perfect palate-cleanser between longer games, easy to teach to a friend in thirty seconds, and secretly deep once you start hunting for the Nim-sum. Play a match or two, switch who goes first, and try different rows — then move on to a bigger board game with your strategic muscles warmed up. However you like your puzzles, learning to think a move ahead in Nim will sharpen the way you play everything else.
FAQ
Is Nim free to play?
Yes — Nim on vygam is completely free. There is no download and no sign-up; it plays instantly in your browser on phone, tablet or desktop.
How do you play Nim?
Several rows hold a number of tokens. Players take turns, and on your turn you remove as many tokens as you like — one, several or a whole row — but always from a single row. In this normal-play version, the player who takes the very last token wins.
What is the winning strategy in Nim?
The key is the Nim-sum: write each row's count in binary and XOR them together. If that total is not zero you can always win by moving to make it zero; if it is already zero, every move you make hands the advantage back to your opponent.
Can you always win at Nim?
Only from a winning position. If the Nim-sum of the starting rows is not zero, the player to move can force a win with perfect play. If it is zero, the player to move loses against an opponent who never slips, so choosing whether to go first or second matters.
Does the first player always win in Nim?
No. It depends entirely on the starting rows. The first player wins with perfect play only when the Nim-sum is not zero. On a balanced board such as 1-3-5-7, whose Nim-sum is zero, the second player holds the winning strategy.