← Nim against the computer

Nim Strategy: The Nim-Sum in Your Head and How to Beat the Computer

Nim is small enough to compute exactly, so the figures on this page are not samples: every starting layout the game can deal and every position after it were enumerated with the page's own code. As a check, 200,000 games per figure were also played against the real engine, and the results agree.
The short answer

Nim is won by leaving your opponent a position whose nim-sum is zero: split every heap into 4s, 2s and 1s, cross out equal pieces in pairs, and move so that nothing is left over. On this page you move first, and on the two lower levels every one of the 2,401 possible starting layouts is a win for you — the formula wins 100% of those games. On the “rigged start” level all 350 layouts are lost and the win rate is exactly 0%. The “makes mistakes” level misses its winning move on about 38% of its turns, so even after one slip you still win about three games in four.

The nim-sum without binary

The textbook says: write the heaps in binary and XOR them. You do not need binary for that. Every number is a sum of different powers of two in exactly one way, and with heaps of up to seven stones the only pieces are 4, 2 and 1: 7 = 4+2+1, 6 = 4+2, 5 = 4+1, 3 = 2+1.

  1. Split every heap into 4s, 2s and 1s.
  2. Cross out equal pieces in pairs: two 4s cancel, two 2s cancel, two 1s cancel, wherever they are.
  3. Whatever is left unpaired, added together, is the nim-sum. Nothing left means zero.
HeapsSplit4s2s1sLeft over (nim-sum)
3, 5, 72+1, 4+1, 4+2+1two, canceltwo, cancelthree, one left1
7, 6, 44+2+1, 4+2, 4three, one lefttwo, cancelone left4+1 = 5
3, 5, 62+1, 4+1, 4+2two, canceltwo, canceltwo, cancel0
4, 6, 7, 24, 4+2, 4+2+1, 2three, one leftthree, one leftone left4+2+1 = 7

Bouton's theorem is about that leftover. If it is zero, the player to move loses against correct play; if it is not, the player to move can make it zero and wins. Zero positions are the ones to hand to your opponent: any move from a zero position breaks a pair, and the other side can always restore it.

In practice you only count parity: is the number of heaps containing a 4 odd or even? The same for 2 and for 1. Three quick looks at the board, no arithmetic.

From the leftover to the move

Once you know the leftover pieces, the winning move takes three steps:

  1. Take the biggest leftover piece and find a heap that contains it. There always is one.
  2. In that heap, remove every leftover piece it contains and add every leftover piece it lacks.
  3. The result is the new size of the heap. It is always smaller than the old one, so the move is legal: take the difference.

Example: 7, 6, 4 leaves 4+1. The heap of 6 = 4+2 contains the 4 but not the 1: remove the 4, add the 1, and you get 2+1 = 3. So take three stones from the heap of six: 7, 3, 4 has nothing left over.

PositionLeft overWinning moves
3, 5, 71take one stone from any heap: 3→2, 5→4 or 7→6
2, 5, 615→4 only (the 5 is the only heap containing a 1)
7, 6, 44+17→2, 6→3 or 4→1
1, 3, 5, 611→0, 3→2 or 5→4
4, 6, 7, 24+2+14→3, 6→1 or 7→0
1, 1, 32+13→0 only

The number of winning moves is the number of heaps that contain the biggest leftover piece, so it is always odd. On this page a starting layout has exactly one winning first move in 49.4% of games and exactly three in 50.6%. A start offers 14 legal moves on average and only 14.5% of them win, which is why guessing does not work.

With three heaps it also helps to know the zero positions by sight. With heaps of one to seven stones and three different sizes there are only seven: 1-2-3, 1-4-5, 1-6-7, 2-4-6, 2-5-7, 3-4-7 and 3-5-6. Two equal heaps on their own are zero as well, and from there you simply copy every move of the opponent in the other heap.

A whole game, move by move

Here is one game from the start 3, 5, 7 against the level that plays the formula. Each row is your turn; the computer's replies are one possibility of many, because from a zero position it has no good move and takes stones at random.

Before your moveSplitLeft overYour moveThe computer then
3, 5, 72+1, 4+1, 4+2+11one from the first heap: 2, 5, 7takes four from the third heap: 2, 5, 3
2, 5, 32, 4+1, 2+145→1, four stones: 2, 1, 3takes the whole third heap: 2, 1
2, 12, 12+12→1, one stone: 1, 1takes one of the two stones: 1
111the last stone–

The third row shows the rule at its smallest: the leftover is 2+1, the heap of two contains the 2 but not the 1, so it loses the 2 and gains the 1 and becomes a heap of one. Two equal heaps are left, and whatever the computer does, you take the rest.

The trainer under the game drills exactly this step, one position at a time, with heaps of up to nine stones, so an 8 appears among the pieces there. It opens after your first finished game.

Which starts on this page are won

You always move first. The game deals three or four heaps, equally often, of one to seven stones each, and what happens next depends on the level.

LevelHow the start is madePossible layoutsWon for you, the first player
makes mistakesrandom, redealt while the nim-sum is zero301 with three heaps, 2,100 with fourall 2,401
plays the formulathe samethe same 2,401all 2,401
formula + rigged startthe last heap is replaced so that the nim-sum is zero350none

Layouts are counted with the order of the heaps. Without the redeal, a random layout would be lost for the first player in 12.2% of three-heap games and 12.5% of four-heap games; the page throws those away, so on the two lower levels you are winning before your first move in every single game, with 3 to 27 stones on the table, 14 on average.

On the rigged level the page drops the last heap and puts in exactly the heap that makes the nim-sum zero. Every one of the 350 layouts is lost for the player to move, and since this level never errs, your win rate there is exactly zero. It is a demonstration, not a contest.

A detail of the rigged start: in 2.2% of games it deals a heap of 8 and a heap of 15 (8, 7, 15, or 8 and 15 with two heaps between them that add up to 7), so the pieces there include an 8. The classic 1, 3, 5, 7 can turn up on this level too, and it is a zero position: 1, 2+1, 4+1, 4+2+1 cancel completely.

How often each level errs

The three levels differ in two things only: whether the computer ever misses its winning move, and whether the start is rigged. In a zero position all three do the same thing — pick a heap at random and take a random number of stones from it — so the computer's moves tell you nothing when you are winning.

  • Makes mistakes. When it has a winning move it plays it 55% of the time; otherwise it moves at random, and a random move is sometimes the right one by luck. The real miss rate depends on the position: 22.5% with a single heap of two, 30% with a single heap of three, at most 43.4%, and never with one stone left. Right after a wrong first move by you it averages 38%.
  • Plays the formula. Never misses. One wrong move by you at any point and the game is lost, exactly: 0%.
  • Formula + rigged start. Never misses, and you start from zero. 0% whatever you do.

Here is what simple ways of playing score, as exact probabilities of winning with you to move first:

How you playmakes mistakesplays the formularigged start
The formula on every move100%100%0%
A random legal move24.1%1.8%0%
Always take the whole largest heap34.6%7.1%0%
Always take one stone from the largest heap21.4%0.9%0%
Endgame only: one heap — take it all, two heaps — make them equal, otherwise at random52.7%6.3%0%
A wrong first move, then the formula whenever a winning move exists75.7%0%–

The last row is the answer to “how many chances do I get”. Against the level that makes mistakes, every turn it has while it is winning is a chance of roughly 38% that it hands the game back, and once it does, the formula finishes the job. So when you are lost, play for time: take a single stone from the largest heap and check the leftover again after every reply. That is the 75.7% in the table; the best possible play scores 77.5%, and moving at random while you wait only 66.4%.

The last row averages over every wrong first move, each equally likely, in every start that has one. The only start with no wrong move is 1, 1, 1.

Normal and misère play

This page plays normal Nim: whoever takes the last stone wins. In misère Nim the last stone loses. The page does not offer misère, but the rule is worth knowing, because the two games are nearly the same.

Play misère exactly like normal Nim — leave a zero nim-sum — until your move would leave nothing but heaps of one stone. At that moment do the opposite: leave an odd number of single stones instead of an even one. That is the whole difference. We checked this rule by brute force against every position of up to four heaps of up to 15 stones, 3,876 of them, with no exceptions.

PositionNormal play: move toMisère play: move to
1, 1, 31, 1 (take the three)1, 1, 1 (take two of the three)
2, 2, 31, 2, 3 or 2, 2the same — heaps bigger than one remain
1, 1, 1, 31, 1, 1, 11, 1, 1
1, 1, 1any stone: you winlost: you will take the last stone

How rarely it matters is easy to count. Of the 2,401 starting layouts of the two lower levels, only one would change hands under misère rules: 1, 1, 1, which is won in normal play and lost in misère. The set of winning first moves would differ in 3.7% of starts; in all the others you would make the same first move in either game.

The 21 game

The second game on the page is Nim with one heap and a limit: 21 stones, take one, two or three, the last stone wins. Here the zero positions are the multiples of four. If you leave 4, 8, 12, 16 or 20 stones, then whatever the opponent takes — call it k — you take 4−k and land on the next multiple of four.

From 21 you move first, so take one stone and then always answer with 4−k: 20, 16, 12, 8, 4, 0. Six moves, and it works against the first two levels every time. On the rigged level the game starts from 20 stones instead, and you are the one facing a multiple of four: 0%.

How you playmakes mistakesplays the formula
Take one, then answer with 4−k100%100%
A random legal move26.1%0.25%
Two or three on the first move, then correctly83.2%0%

In this game the level that makes mistakes misses the multiple of four on 30% of its turns, not 45%: when it moves at random, one take in three is the right one anyway. After a wrong first move it has five turns to slip, and it gets through all five only 0.75 = 17% of the time.

Questions and answers

How do you always win at Nim?

Leave your opponent a position with a nim-sum of zero after every move. Split each heap into 4s, 2s and 1s, cancel equal pieces in pairs, and change one heap so that nothing is left unpaired. If the nim-sum is already zero on your turn, no move wins against correct play.

How do you work out the nim-sum in your head?

Break every heap into powers of two — 7 is 4+2+1, 6 is 4+2, 5 is 4+1, 3 is 2+1 — and cross out equal pieces in pairs. The pieces left without a partner, added up, are the nim-sum. For 3, 5, 7 the 4s and 2s cancel and one 1 is left, so the nim-sum is 1.

Does the first player always win at Nim?

Only if the starting nim-sum is not zero. With three or four random heaps of one to seven stones that is about seven starts in eight. On this page zero starts are redealt on the two lower levels, so you, moving first, are winning in all 2,401 possible layouts; on the rigged level all 350 layouts are lost.

Can you beat the computer at Nim?

On the two lower levels here, always: every start is won for you, and the formula wins 100% of games. The level that makes mistakes also forgives a slip, missing its winning move on about 38% of turns. The rigged level cannot be beaten: the start is a zero position and the computer never errs.

What is the difference between normal and misère Nim?

In normal play the last stone wins, in misère it loses. The strategy is identical until a move would leave only heaps of one stone; then in misère you leave an odd number of them instead of an even number. This page plays the normal game.

How do you win the 21 game?

Leave a multiple of four. With 21 stones and takes of one to three, the first player takes one and then answers every take of k with 4−k, leaving 20, 16, 12, 8, 4 and finally nothing. Starting from 20 the first player loses against correct play.

Play Nim against the computer →
Where to go from here
To cite this page: “Nim Strategy: The Nim-Sum in Your Head and How to Beat the Computer”, You vs Computer, https://youvscomputer.com/games/nim-strategy.html