Skip to the workspace
Pebellslearn by building
Hard Problems & Adversarial Search  ›  Lesson

Nim, Grundy Numbers & Winning Positions

Impartial Games 32 minXOR every heap together: zero means you're already losing, anything else names your winning move

dsa / Nim, Grundy Numbers & Winning Positions

Explore each scene at your own pace.

01 · Why XOR, and why zero loses

Why XOR, and why zero loses

Nim, Grundy Numbers & Winning Positions

Definition
An impartial game is one where both players have the same moves from every position; in Nim the player to move loses exactly when the XOR of the heap sizes is zero, and a Grundy number, the mex of the reachable positions' numbers, extends this to other such games.
In plain words
In a take-away stone game you can tell who is winning without searching. Combine the heap sizes with XOR: if the result is zero, the player about to move is beaten by perfect play; otherwise a move exists that makes it zero.

In short: in Nim, two players alternate taking any positive number of stones from one heap, and whoever takes the last stone wins. There's no searching needed to know who's winning: XOR every heap size together. If the result is 0, the player about to move is already losing against perfect play. If it's anything else, there's always a move — take from the heap whose own bit pattern can absorb the difference — that brings the XOR back to 0 and hands the loss to the opponent instead. Grundy numbers extend this idea past Nim to any "take-away" game: the Grundy number of a position is the mex (minimum excludant — the smallest non-negative integer not reachable in one move) of the Grundy numbers of everywhere one move can reach, and a position is a loss for the mover exactly when its Grundy number is 0.

Three heap rows (3, 4, 5 tally marks) and their binary columns, with a green XOR row reading 010 = 2 — not zero, so it's a win, and fixing heap 0 from 3 to 1 clears the row

Why XOR, and why zero loses

watch it move · grid

Each bit column is its own tiny vote. XOR of a column is 1 exactly when an odd number of heaps have a 1 there.

Column b2: 0, 1, 1 — two 1s, an even number, so XOR is 0. 4 by 3 table. 10 of 12 cells filled.

1 / 4

A losing position (XOR already 0) stays losing no matter what the mover does, because changing one heap always flips at least one bit somewhere, which can never bring an all-even column count back to all-even everywhere at once:

python
Scene 1 of 6
Project context and further resources
You're building a piece ofMeridian — the bid desk
This piece — nim_winning_move(): When two bidders are deadlocked on identical terms, settles it with a Nim-style game whose winner is known before a single move is played.
Scenario Two bidders land on identical terms and the tender rules say the tie gets settled by a quick game: each takes stones from shared piles, and whoever can't move loses. The bid desk wants to know who wins before a single stone is taken -- the XOR of the piles is the entire answer.
nim_winning_move.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
nim_winning_move(heaps) → dictA move that forces the XOR of all heaps to 0, or None if the position is already a loss for whoever moves next.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.