Play Nim Online

The classic mathematical strategy game. Take turns removing objects from rows and outthink your opponent. Challenge the AI or play with a friend.

Game Mode
AI Difficulty
Variant
Starting Heaps
You: 0
vs
AI: 0
Your turn — select objects from one row

What Is Nim?

Nim is a classic two-player combinatorial strategy game with roots stretching back centuries. It is one of the most important games in mathematical game theory and is often the first game discussed when introducing concepts like combinatorial game theory, impartial games, and Sprague–Grundy theory.

In Nim, objects (traditionally stones, matchsticks, or counters) are arranged in several heaps (also called rows or piles). On each turn, a player must remove at least one object from a single heap. They may remove as many objects as they wish from that heap — up to and including the entire heap — but they may only take from one heap per turn.

The game ends when all objects have been taken. In the normal play convention, the player who takes the last object wins. In the misère variant (sometimes called “last one loses”), the player forced to take the last object loses.

How to Play Nim

  1. Set up the heaps. The classic configuration is rows of 1, 3, 5, and 7 objects (16 objects total). Other popular starting positions include 1-3-5 and 3-4-5.
  2. Take turns. On your turn, choose any single row and tap one or more objects to select them. Then press “Take” to remove them. You must remove at least one object per turn.
  3. Win the game. In Normal mode, the player who takes the last object wins. In Misère mode, the player who takes the last object loses.

Nim Strategy Guide

1. The Nim-Sum (XOR Strategy)

The Nim-sum is the key to perfect play. It is computed by taking the binary exclusive OR (XOR) of all heap sizes. For example, with heaps of 1, 3, 5, and 7: 1 XOR 3 XOR 5 XOR 7 = 0. When the Nim-sum is zero before your turn, you are in a losing position if your opponent plays perfectly. When it is non-zero, there is always a winning move.

2. Making a Winning Move

When the Nim-sum is non-zero, your goal is to leave a position with a Nim-sum of zero for your opponent. To do this, find a heap where heap XOR Nim-sum < heap. Remove enough objects from that heap to reduce it to heap XOR Nim-sum. This guarantees every future position your opponent faces will have a non-zero Nim-sum — meaning you can always respond optimally.

3. The Misère Twist

In Misère Nim, the strategy is almost identical to normal Nim with one critical difference in the endgame. Play the standard XOR strategy until all remaining heaps have at most one object. At that point, leave an odd number of heaps with exactly one object — this forces your opponent to take the last one.

4. First-Mover Advantage

Whether the first player wins or loses depends entirely on the starting position. In the classic 1-3-5-7 setup, the Nim-sum is 0, which means the second player has the winning strategy. In a 1-3-5 setup (Nim-sum = 7 ≠ 0), the first player can always win with perfect play.

5. Single-Heap Simplification

When only one heap remains, the strategy is trivial. In Normal Nim, take everything to win. In Misère Nim, leave exactly one object for your opponent.

About the AI

Our Nim game features three distinct AI difficulty levels:

  • Easy: Makes random moves 80% of the time. Occasionally stumbles into a good move but mostly plays randomly — great for beginners and young learners.
  • Medium: Uses the correct XOR strategy about 60% of the time but deliberately makes sub-optimal moves the rest of the time. Provides a fair, beatable challenge for casual players.
  • Unbeatable: Uses the Nim-sum (XOR) algorithm to play mathematically perfect Nim. In a winning position, it will never make a mistake. The only way to beat it is to start from a position where Player 1 has the advantage — and go first.

The Mathematics of Nim

Nim holds a foundational place in combinatorial game theory. Here are some key mathematical facts:

  • Nim was the first game to be completely solved mathematically. Harvard mathematician Charles L. Bouton published the complete winning strategy in 1901 in a paper titled “Nim, a game with a complete mathematical theory.”
  • The Sprague–Grundy theorem (1935/1939) later proved that every impartial game is equivalent to a Nim position. This means Nim is, in a sense, the universal impartial game.
  • The winning strategy relies on the binary digital sum (XOR). A position is a losing position for the player about to move if and only if the XOR of all heap sizes is zero.
  • Nim is an impartial game — both players have the same moves available from any position — as opposed to partisan games like Chess or Go where each side has different pieces.
  • In 1940, Edward Condon and colleagues at Westinghouse built Nimatron, one of the first digital electronic game-playing machines. It played Nim using relay switches and was displayed at the New York World’s Fair.

History of Nim

The exact origin of Nim is uncertain, but games involving removing objects from piles have been played for centuries across many cultures. Similar games exist in China (Jiǎn shízi, “picking stones”), in Africa (various Tsoro and Mancala-related counting games), and in Europe (the “Marienbad Game”).

The name “Nim” was coined by Charles L. Bouton of Harvard University in 1901. The origin of the word is debated — it may come from the German word nimm (“take”) or the archaic English word nim (“to steal”).

Nim became widely popular after being featured in the 1961 French film Last Year at Marienbad (L’Année dernière à Marienbad) by Alain Resnais, in which a character repeatedly wins at a Nim-like game. The “Marienbad Game” (often played with four rows of 1-3-5-7) is now one of the most recognised Nim configurations.

Today Nim is widely used as a teaching tool in computer science courses for topics including recursion, game trees, dynamic programming, and the Sprague–Grundy theorem.

Variations of Nim

The simple Nim rules have inspired many creative variations:

  • Single-Pile Nim (Subtraction Game): A single heap where each player can remove up to k objects per turn. The strategy depends on the value of k and the heap size modulo (k+1).
  • Wythoff’s Game: Two heaps where a player may take any number from one heap or an equal number from both heaps. The winning positions involve the golden ratio.
  • Fibonacci Nim: A single heap where the first player takes any number (less than all) and thereafter each player takes at most double what the previous player took. The winning strategy connects to Fibonacci numbers.
  • Poker Nim: Players can add objects back to heaps as well as remove them — though this variation is equivalent to standard Nim with optimal play.
  • Turning Turtles: A row of coins where each player flips one coin from heads to tails and may optionally flip one earlier coin. Analysed via Nim values.
  • Northcott’s Game: A Checkers-like variant where pieces slide along rows; it reduces to Nim on column distances.

Frequently Asked Questions

Nim is a two-player mathematical strategy game. Objects are arranged in rows. On each turn, a player removes one or more objects from a single row. In Normal mode, the player who takes the last object wins; in Misère mode, the player who takes the last object loses.
The winning strategy uses the Nim-sum — the XOR (exclusive or) of all heap sizes. If the Nim-sum is non-zero before your move, there is always a move that leaves a Nim-sum of zero for your opponent, putting them in a losing position. If the Nim-sum is already zero before your move, every move you make will leave a non-zero Nim-sum, meaning your opponent can always respond optimally.
In Normal Nim, the player who takes the last object wins. In Misère Nim, the player who takes the last object loses. The Nim-sum strategy is the same for most of the game, but the endgame changes: in Misère Nim, when all heaps are 0 or 1, you aim to leave an odd number of single-object heaps.
The Nim-sum is the bitwise exclusive OR (XOR) of all the heap sizes. For example, with heaps of 3 (011), 5 (101), and 7 (111): 011 XOR 101 XOR 111 = 001. A Nim-sum of zero means the position is losing for the player about to move (assuming perfect play by the opponent).
Yes! The game is fully responsive and designed for touch screens. Tap objects to select them, then press the Take button. The board scales to fit any device.
Games resembling Nim have been played for centuries across many cultures. The name “Nim” and the complete mathematical solution were published by Charles L. Bouton of Harvard University in 1901. The game was famously featured in the 1961 film Last Year at Marienbad.

More Puzzle & Strategy Games

If you enjoy Nim, check out these other games on our site: