🪨 Nim

Remove stones from one row — last to take loses (misère)

Your turn 🔥 0 🏆 0 / 0

How to Play Nim

Nim is one of the oldest and most mathematically elegant strategy games in existence. The game is played with several rows of objects—traditionally stones, sticks, or matches—and two players take turns removing any number of objects from a single row. In the misère variant played here, the player who is forced to take the last object loses. Despite its simple rules, Nim has profound connections to combinatorial game theory and was one of the first games to be fully solved mathematically using the concept of nim-sum (binary XOR).

Rules

On your turn, you choose one row and remove one or more stones from it. You must remove at least one stone, and all stones must come from the same row. The player who takes the last remaining stone loses. That's the misère rule: you want to force your opponent into taking the final stone. The game starts with a preset configuration—Small (rows of 1, 3, 5), Medium (1, 3, 5, 7), or Large (1, 3, 5, 7, 9) stones—and you play against the computer.

How to Play

Click a row to select it. A bar of buttons will appear showing how many stones you can remove (1 through the row's count). Click a number to make your move. The AI will then respond. On Easy, the AI makes random legal moves. On Normal, it sometimes plays optimally and sometimes makes mistakes. On Hard, it uses the nim-sum strategy for perfect play—you cannot beat it from a losing position.

History & Origin

Nim's origins are ancient. Similar games appear in Chinese literature and were played in Europe for centuries. The name "Nim" was coined by Charles L. Bouton, who published a complete mathematical analysis in 1901. Bouton proved that the game could be solved using binary numbers and the XOR operation. His work laid the foundation for the Sprague-Grundy theorem, which generalizes winning strategies to many impartial games. Nim has since become a staple of game theory courses and a classic example of a combinatorial game.

Strategy & the Nim-Sum

The key to perfect play is the nim-sum: the XOR of all heap sizes. If the nim-sum is zero, the position is losing for the player to move—assuming normal play (last to take wins). In misère Nim, when at least one heap has more than one stone, the optimal strategy is the same: play to leave a nim-sum of zero. When all heaps are of size 0 or 1, the strategy changes: you want to leave an odd number of heaps of size 1 for your opponent. Skilled players learn to compute nim-sums mentally to find winning moves.

Tips for Winning

Frequently Asked Questions

What is misère Nim?

In normal Nim, the player who takes the last stone wins. In misère Nim, the player who takes the last stone loses. The misère variant adds a twist: when all heaps are small (0 or 1), the endgame strategy differs from the standard nim-sum approach.

Can I beat the Hard AI?

Only if you start from a winning position. From a losing position (nim-sum zero), the Hard AI will never make a mistake. From a winning position, you can force a win by always leaving nim-sum zero after your moves.

Why use XOR for Nim?

Bouton proved that the winning strategy depends on the binary representation of heap sizes. XOR (exclusive-or) captures the "nim-sum": when the nim-sum is 0, the position is losing. This elegant connection makes Nim a beautiful example of mathematical game theory.

Play Nim free in your browser—no download needed. Remove stones, outthink the AI, and avoid taking the last one.