← Back to the Visual Lab

Combinatorial games · directed state spaces

Nim as a graph

Take counters from one pile—or make the same move by following an arrow. Every node is a position and whose turn it is; every directed edge is one legal move. An optimal computer reveals that the game is really a navigation problem through a finite graph.

Graph display

01 · Game board

Take from one pile

Your turn
Choose a counter to remove it and every counter to its right.

Path through the graph

0 moves
  1. The first move will appear here.

02 · Directed state graph

Navigate the game

Building the complete graph…

Moves follow the current node · drag and scroll to explore
Current Legal move Start Played path Outside future cone End · Victory End · Defeat

The starting node and both zero-stone end nodes are marked. Arrow keys rotate the view, plus and minus zoom, and R resets it. The bright trail records the played path. Nodes outside the directed future cone are dimmed, with past states dimmed most strongly. Highlighted neighboring nodes can be clicked as moves; keyboard users can choose the same destinations with the Legal next states control. Nim-sums are hidden on graph nodes unless they are shown with the graph display control.

Turn You
Counters left 12
Nim-sum 2
Position class Winning
State graph —

The two terminal nodes include whose turn comes next: an empty board with the computer to move means Victory, because you took the last counter; an empty board with you to move means Defeat. Pile-order symmetries are folded together in the graph, so positions such as [5, 3, 1] and [1, 5, 3] share a node without changing the game.