Chess openings aren't a tree: drawing 3,865 openings as one graph
During the World Chess Championship match I kept noticing games reach well-known positions through unusual move orders. Commentators would say "and now we've transposed into…", and I wanted to see every one of those connections at once. So I built Theoria, a map of every named chess opening. This post is about how it works.
The tree everyone draws
Opening books, databases and most explorers show openings as a tree: a first move, then branches, then more branches. But a chess position doesn't remember how it was reached. In the Caro-Kann, 3.Nc3 dxe4 4.Nxe4 and 3.Nd2 dxe4 4.Nxe4 both put a knight on e4, and the positions are identical. A tree has to draw them as two unrelated branches.
The data
Lichess publishes the names of chess openings as open data (lichess-org/chess-openings, CC0): 3,865 named lines, each with an ECO code and a move sequence. I replayed every line with chess.js and recorded each position along the way.
One node per position
The trick is the key. A FEN string describes a position completely, but its last two fields (the halfmove clock and the move number) depend on how you got there. Drop them and transpositions collapse into the same node:
const positionKey = fen => fen.split(' ').slice(0, 4).join(' '); The 3,865 lines become 8,207 positions and 8,423 moves. 204 positions have more than one move leading into them, and because those merges compound, 3,941 positions (almost half) can be reached by more than one move order. Using only moves from named openings, the Semi-Slav has 30 different move orders and the Queen's Gambit Declined Orthodox main line has 100.
Storing it in Neo4j
I was an AI intern at Neo4j, so a graph database was the natural home. The model is small:
(:UniversePosition {key})-[:LEGAL_MOVE {san, uci, revision}]->(:UniversePosition) (:OpeningLine {id})-[:REACHED {ply}]->(:UniversePosition) (:OpeningSnapshot)-[:CONTAINS {depth}]->(:UniversePosition) (:OpeningSnapshot)-[:ANNOTATES {names, stories}]->(:UniversePosition) (:OpeningCatalog {activeRevision}) Each import writes a complete snapshot and switches activeRevision in the same transaction, so readers never see half a catalogue. Questions about transpositions become short queries. Here is every move order into the Semi-Slav:
MATCH (:OpeningSnapshot {id: $rev})-[a:ANNOTATES]->(target:UniversePosition) WHERE 'Semi-Slav Defense' IN a.names MATCH (:OpeningSnapshot {id: $rev})-[:CONTAINS {depth: 0}]->(start:UniversePosition) MATCH path = (start)-[:LEGAL_MOVE*..8]->(target) WHERE all(m IN relationships(path) WHERE m.revision = $rev) RETURN [m IN relationships(path) | m.san] AS moves Laying out 8,207 positions
A graph full of cross-links is hard to draw legibly, so the layout draws a spanning tree and puts the rest on top:
- Each position keeps its shallowest parent as its tree parent. Every other incoming move becomes a cross-link (217 of them).
- Positions sit on rings by depth, with
radius = 350 * sqrt(depth). The square root gives the early plies, where most of the branching happens, more room. - Each subtree gets an angle in proportion to the number of lines ending beneath it, so big openings like the Sicilian get wide wedges.
- Families come from the part of the name before the colon: "Sicilian Defense: Najdorf Variation" belongs to the Sicilian Defense. Unnamed positions take the family of their nearest named ancestor. That gives 149 families, each with its own colour.
The layout runs once per catalogue version on the server and is cached. The browser receives one compact payload of parallel arrays.
Drawing it
The renderer is a plain 2D canvas. An orthographic projection with yaw and pitch lets you rotate and tilt the map, with depth pushed into a shallow bowl. Edges and dots are batched into Path2D objects by colour and highlight level, so the whole graph is drawn with a small number of calls rather than one per node. Hit-testing is brute force over the projected points, which is fast enough at this size.
Highlighting is just masks. Selecting a position lights its path from the start and every continuation, transpositions included. The White/Black and first-move filters dim everything outside them.
Famous games and traps
To give casual visitors somewhere to start, I pinned 9 famous games (from the Immortal Game of 1851 to Kasparov's Immortal of 1999) and 7 classic traps at their critical moment. Each game score was taken from its Wikipedia article and replayed with chess.js, so every move is checked to be legal. From any position you can play on against a small practice bot.
What I learned
- Choose the node key carefully: it decides what the graph means.
- A spanning tree with the extra edges layered on top keeps 8,000 nodes readable.
- Code and data deploy separately. My first deploy shipped the new interface while the database still held the old snapshot.
Try it at theoria.up.railway.app. I'd love to hear what you'd build on top of an opening graph.