The Game of Nim

What is Nim?

Nim is one of the oldest and most fascinating strategy games in history. Two players take turns removing objects from a set of piles. The rules are simple: on each turn, a player must remove at least one object, and can remove as many as they want — but only from a single pile. The player forced to make the last move loses (this is the misère variant, the one we play on NimPlayer).

That’s it. No dice, no luck, no hidden information. Just pure logic.


A game with ancient roots

The origins of Nim are debated. Some historians trace it back to ancient China, where a similar game called Jianshizi (“picking stones”) was played thousands of years ago. The name “Nim” itself appeared in the West in the early 20th century — mathematician Charles Bouton published the first complete mathematical analysis of the game in 1901, proving that a perfect strategy always exists.

The game became famous in Europe under the name Marienbad, after appearing in the 1961 French avant-garde film Last Year at Marienbad by Alain Resnais — where it is played with matches in a hypnotic, dreamlike atmosphere. That’s where NimPlayer takes its aesthetic inspiration from: a game that feels ancient, inevitable, almost cosmic.


The math behind the moves

Here’s where it gets interesting for the nerd in you.

Every position in Nim can be analyzed using a concept called nim-sum: the bitwise XOR of all pile sizes. If the nim-sum of the current position is 0, the player whose turn it is is in a losing position — assuming both players play perfectly. If it’s anything other than 0, the current player can always force a win.

Example:
Piles of 3, 5, and 7.
3 = 011 in binary
5 = 101 in binary
7 = 111 in binary
XOR = 001 ≠ 0 → the current player wins.

Try the Nim-sum yourself

Piles 3
Binary XOR breakdown

The winning move is to reduce one pile so that the nim-sum becomes 0, leaving the opponent in a losing position.

In the misère variant (where taking the last object means you lose), the strategy is identical to normal Nim — with one exception: when all piles have at most one object, you want to leave an odd number of piles of size 1 for your opponent, instead of an even number.


Can you beat the AI?

NimPlayer’s AI uses the exact optimal strategy described above. It never makes a mistake. The only way to beat it is to start from a position where the nim-sum is already 0 when it’s the AI’s turn — which is why NimPlayer lets you choose who goes first.

Every game on Nim Lab starts with a randomly generated board of 3 to 7 piles, each containing 1 to 15 objects. Some starting positions are winning for the first player. Some are winning for the second. Choose wisely.

Where do you stand?

Enter your current pile sizes and follow the path

Beyond the math: visual patterns

There’s another way to get good at Nim — one that doesn’t require calculating XOR in your head. Experienced players start to recognize winning positions visually, almost like patterns on a chessboard. Two equal piles, certain triplets, specific combinations — once you’ve played enough, some configurations just look like a win or a loss before you’ve done any math.

We won’t tell you which ones. That’s the fun part. Play enough rounds, lose a few times on purpose to study the board, and start building your own mental library. The patterns are there — waiting to be discovered.

Play now against Nimplayer →

Scroll to Top