About Nim
Play Nim free online against the computer. Remove objects from rows and force your opponent to take the last one. A classic mathematical strategy game.
How to use
- Choose your win condition first: Last Takes = Loses (misère Nim, the classic variant most people learn) or Last Takes = Wins (normal Nim, the version mathematicians study). The optimal strategy diverges only in the very last few moves, so picking a mode changes endgame play dramatically.
- Pick a row layout: Classic (1-3-5-7) is the most famous starting position with 16 stones, Small (1-3-5) is faster and good for learning, Even (2-4-6) is a balanced alternative, or pick Custom to design your own configuration.
- On each turn, click stones in exactly one row to select how many to remove. You must take at least 1 stone, can take up to the entire row, but cannot split your turn across multiple rows. Tap Confirm Move to commit. This single-row constraint is the entire ruleset of Nim.
- Learn to compute the nim-sum — the binary XOR of all row sizes. For example, with rows of 1, 3, 5, 7: the nim-sum is 1⊕3⊕5⊕7 = 0, which is a losing position for whoever moves next under perfect play. The optimal move is always one that leaves the nim-sum at zero for your opponent.
- If the nim-sum is non-zero before your move, you can always find a winning play: pick the row whose binary representation has a 1 in the highest bit of the nim-sum, and reduce that row so the new nim-sum becomes zero. The Strategy Hint panel can show you the optimal move when you're stuck.
- Misère endgame rule: when every remaining row has only 1 stone (or you're about to make that true), reverse the strategy — leave an odd number of single-stone rows for your opponent so they're forced to take the final stone. Normal endgame rule: leave an even number of single-stone rows so you take the last one yourself.
- Press New Game to deal a fresh layout, or Reset Scores to clear the win/loss tally. Try matching the AI on Classic 1-3-5-7 — since it's a nim-sum-zero starting position, the AI playing second will beat you every time with perfect play. Switching to a position with non-zero nim-sum (like 1-2-3) gives the first player the win.
Frequently asked questions
What is the game of Nim?
Nim is one of the oldest and most famous mathematical strategy games. It features several rows of objects (often stones, matches, or coins), and two players take turns removing any number of objects from a single row. In the standard version, the player who takes the last object wins. Nim is a solved game, meaning there is a mathematically proven optimal strategy that guarantees a win from certain starting positions. The game has been studied extensively in combinatorial game theory and serves as the foundation for understanding a wide class of two-player perfect-information games.
Is there a winning strategy for Nim?
Yes — Nim has a complete mathematical solution based on the binary XOR operation (also called the nim-sum). Compute the XOR of all row sizes: if the result is non-zero, the player whose turn it is can always force a win by making a move that reduces the nim-sum to zero. If the nim-sum is already zero on your turn, you are in a losing position against a perfect opponent — any move you make will leave a non-zero nim-sum for your opponent to exploit. This was proven by Charles Bouton in 1901 in a landmark paper that founded combinatorial game theory.
How do you calculate the nim-sum?
The nim-sum is the bitwise XOR of all row sizes. Convert each row size to binary, then XOR them bit by bit (1 XOR 1 = 0, 1 XOR 0 = 1, 0 XOR 0 = 0). For example, with rows of 3, 4, and 5: 3 = 011, 4 = 100, 5 = 101 in binary. XOR them together: 011 XOR 100 = 111, then 111 XOR 101 = 010 = 2. Since the result is non-zero, the current player can force a win. To make a winning move, find a row where you can remove objects to make the overall nim-sum zero.
What is the difference between normal and misère Nim?
In normal Nim, the player who takes the last object wins. In misère Nim, the player who takes the last object loses. The optimal strategy is the same for most of the game — keep the nim-sum at zero after your turn. The only difference comes in the endgame: in misère play, when all rows have at most one object each, you want to leave an odd number of single-object rows (forcing your opponent to take the last one). In normal play, you want to leave an even number. This subtle endgame twist is the only distinction between the two variants.
What is the history of Nim?
Nim has ancient roots, with similar subtraction games played in China, Europe, and Africa for centuries. The modern mathematical analysis began in 1901 when Charles L. Bouton at Harvard published "Nim, a Game with a Complete Mathematical Theory," proving the XOR-based optimal strategy. The name "Nim" may come from the German word "nehmen" (to take) or the archaic English "nim" (to steal). In 1940, Westinghouse built the Nimatron, one of the first game-playing machines, which could play Nim perfectly using relay circuits — a remarkable early example of computational game theory.
Why is Nim important in mathematics?
Nim is the foundational game in combinatorial game theory. The Sprague-Grundy theorem, one of the most important results in the field, proves that every impartial game (a game where both players have the same available moves) is mathematically equivalent to a Nim position. This means that if you understand how to solve Nim, you have the theoretical tools to analyze an enormous class of combinatorial games. Nim is taught in university discrete mathematics and game theory courses worldwide as the canonical example of a perfectly solved strategy game.
Is this game free?
Completely free with no ads, no sign-up, and no downloads. It runs in your browser on any device — phone, tablet, or desktop. If you enjoy mathematical strategy games, try
Chess,
Gomoku, or
Mastermind for different strategic challenges.
Part of ToolFluency’s library of free online tools for Games & Fun. No account needed, no data leaves your device.