Advanced maths

Sprague Grundy calculator for finite impartial games and disjunctive sums

Evaluate a finite normal-play impartial game DAG and find every move that makes a supplied disjunctive sum have zero nim-sum.

Your numbers

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

Evaluate a finite normal-play impartial game DAG and find every move that makes a supplied disjunctive sum have zero nim-sum.

THE FORMULAg(v)=mex{g(w):v→w}; g(v₁+…+v_t)=g(v₁) XOR … XOR g(v_t).

A worked example

For positions with moves i→j for every j<i, i=0,1,2,3, Grundy values are 0,1,2,3. Starts (2,3) have XOR 1; move the second component 3→2 to reach XOR zero.

Good to know

Explicit move DAG with 1–500 positions and at most 5,000 arcs; 1–20 starting positions may repeat and represent independent game copies. Validate acyclic normal-play impartial rules, evaluate mex in reverse topological order, and return the XOR and every legal component move that makes it zero. No cyclic games, misere play, implicit move generation or partizan strategies.

Describe every legal move explicitly

Enter one through five hundred positions labeled zero through n−1 and a list of directed move pairs from to. Every legal successor must appear. The graph must be acyclic and contain at most five thousand arcs. A position with no outgoing move is terminal; under normal play the player unable to move loses.

Starting positions describe independent components of a disjunctive sum. A turn changes exactly one component along one listed arc. Repeated labels are allowed and mean separate copies of the same game position, not a request to move several copies simultaneously. Up to twenty components are supported, so the input distinguishes game graph size from sum size.

Sprague Grundy for finite impartial games and disjunctive sums exampleMex values and a zero-making sum move0123Node i has moves to every j < i; g(i) = iStarts (2,3): XOR 1→After (2,2): XOR 0Move component 2 from position 3 to position 2
Scroll across to see the full diagram.
For positions with moves i→j for every j<i, i=0,1,2,3, Grundy values are 0,1,2,3. Starts (2,3) have XOR 1; move the second component 3→2 to reach XOR zero.

Compare this with the Graph Grundy number calculator with a worst case greedy vertex order. Compute mex nimbers on a supplied move DAG, combine independent starting positions by XOR, and return winning component moves. The similarly named existing graph Grundy number optimizes greedy vertex coloring on an undirected graph, an unrelated invariant.

Compute minimum excluded values

The Grundy value of a terminal position is zero. For any other position, collect the Grundy values of its legal successors and take the smallest nonnegative integer absent from that set, called mex. Repeated successor values count only once for mex, so three moves to value zero still give mex one.

A reverse topological traversal evaluates successors before predecessors. This avoids recursive circular definitions and ensures every supplied position receives a justified value. A directed cycle violates the finite-DAG model even if some exits are available, because ordinary Sprague–Grundy evaluation here assumes the entire move relation is well founded.

Combine components with bitwise XOR

For independent normal-play impartial components, the Grundy value of their sum is the bitwise XOR of their values. A zero sum is a losing position under optimal play; a nonzero sum admits a move to zero. Ordinary integer addition gives the wrong answer because equal binary contributions cancel under XOR.

For each legal move in each component, replace that component value and recompute the sum. The formula newXor=oldXor XOR oldValue XOR newValue makes this direct. Return every move producing zero, with its component index, since two identical starting labels still denote separate legal choices on different copies.

Check the four-position example

Position zero is terminal. Position one can reach value zero, so its mex is one. Position two can reach values zero and one, so its mex is two. Position three can reach zero, one and two, giving mex three. Thus the default graph realizes values 0,1,2,3 exactly.

Starts two and three have XOR 2 XOR 3=1. Moving the second component from three to two leaves values two and two, whose XOR is zero. Moving the first component to either zero or one cannot match the unchanged value three, so neither is a zero-making move. The single listed winning move can therefore be independently checked.

Read the DAG and the sum separately

The figure shows moves descending among the four default positions, with Grundy labels on nodes. Those arrows describe the options of one component. The starting sum places two separate tokens at positions two and three; it does not add an arc between those positions or combine their outgoing move lists into one new component.

A winning move is identified relative to the current complete sum. The same local move can be useful in one sum and losing in another because the other components change the required target value. Always inspect the component index and resulting nim-sum rather than treating every move from a nonzero-Grundy node as automatically winning.

Respect the game assumptions

Both players must have the same available moves from each position, with no hidden information or chance choices. The last move wins. Misère play changes terminal interpretation and generally invalidates this XOR conclusion. Partizan games, in which the players have different options, require different game values and are outside this page.

The graph is an explicit rule specification. If a legal move was omitted, the mex values may be wrong even when the table calculation is internally consistent. If a forbidden move was added, the modeled game is different. Validate the game rules before using the winning-move list as a strategy for a real puzzle.

Use exact values as a strategy certificate

Every returned Grundy value can be audited by listing its successor values, checking that all smaller nonnegative integers occur and that the claimed value does not. Every returned winning move can be audited by one XOR calculation. These checks explain the result without requiring a complete game tree for every possible play sequence.

The calculator does not generate moves from heap formulas or infer a periodic pattern for an infinite family. It solves the finite DAG supplied. Preserve the complete arc list and starting multiset with the result, especially when positions were given descriptive names elsewhere. The exact answer applies to those rules and the stated normal-play convention.

A few questions, answered

Can starting labels repeat?

Yes. They represent independent copies of the same game position.

Why XOR rather than addition?

The Sprague–Grundy theorem combines independent impartial normal-play games by bitwise XOR.

Are cycles accepted?

No. The supplied move graph must be acyclic.

Does this solve misère games?

No. The last-move-wins normal-play convention is required.

Find your calculator

Start typing to explore the library.