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

  1. Click objects in any single row to select how many you want to remove. On each turn, you must choose exactly one row and remove at least one object from it — you can take as many objects as you want from that row, up to all of them. You cannot take objects from multiple rows in a single turn. This constraint is what creates the strategic depth of the game.
  2. Confirm your move to end your turn. The key strategic concept is the nim-sum: compute the binary XOR of all row sizes. If the nim-sum is zero after your move, you are in a winning position (assuming perfect play from that point forward). If it is non-zero, your opponent has the advantage. Learning to calculate the nim-sum quickly is the secret to mastering the game.
  3. In standard (normal) play, the player who takes the last object wins. In misère play (the classic variant many people learn first), the player forced to take the last object loses. The optimal strategy is nearly identical for both variants except in the endgame: in misère play, you want to leave an odd number of rows with exactly one object each, forcing your opponent to take the final one.
  4. Try different row configurations to explore how the starting position affects the game. Some configurations give the first player a guaranteed win with perfect play, while others favor the second player. Classic Nim starts with rows of 1, 3, 5, and 7 objects — this is a losing position for the first player (nim-sum = 0), so the second player wins with perfect play.

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.