RALTechnologies

Insights · Engineering

Designing a fair game board: the maths behind LetterLock.

LetterLock is our trivia board game. Two teams claim letters on a board of hexagons and race to connect their edges. Underneath the trivia is a small piece of mathematics that makes the game fair, and some engineering that makes sure it always names the right winner.

The problem with the classic board

The TV quiz format that inspired LetterLock used a board of hexagons where one side had to cross the board in one direction and the other side in the other. The board was not square in hex terms, so the two paths were not the same length: one side always had less ground to cover. For a television show that is a quirk. For a game people play again and again with friends, it is a built-in unfairness that the losing team will notice.

Borrowing the board from Hex

The fix comes from Hex, a connection game invented independently by Piet Hein in the 1940s and by John Nash a few years later. Hex is played on a rhombus of hexagons, typically 11 by 11. One player owns the left and right edges, the other owns the top and bottom. Players take turns claiming cells, and the first to link their two edges with a connected chain wins.

The rhombus has a lovely symmetry: rotate it and the left-right problem becomes the top-bottom problem. Both players face exactly the same shape of task, with exactly the same number of cells between their edges. LetterLock uses the same board in three sizes: 4 × 4 for a quick game, 5 × 5 as the classic, and 7 × 7 for longer, more strategic matches.

Why a hex board can never draw

A surprising and very convenient fact about Hex is that the game cannot end in a draw. If every cell is filled, exactly one player has a winning chain. The intuition is that hexagons meet three at a point, never four. On a square grid, two diagonal squares can touch at a corner, letting both players' paths "cross" each other at that point. On a hex grid there are no such corners: wherever two of your cells touch, they share a whole side, and so a chain from one edge to the other is a solid wall that the other player cannot get through. Either you have built that wall, or your opponent has found a way round, which is their winning chain.

This is more than a curiosity. The result, often called the Hex theorem, has been shown to be equivalent to the Brouwer fixed-point theorem, a famous result in topology. For a party game it means something simpler: every LetterLock game has a winner, so nobody has to invent a tie-breaker at the end of the night.

Who goes first?

Hex also has a well-known first-player advantage. Nash showed with a "strategy-stealing" argument that the first player has a winning strategy in principle: an extra cell can never hurt you, so if the second player had a winning strategy, the first player could steal it and win. Nobody knows what that strategy is on large boards, but the advantage is real.

Strict Hex players use the swap rule: after the first move, the second player can choose to take that move as their own. We tried this in LetterLock and later removed it, because in a fast party game the extra decision confused more people than it helped. Instead, LetterLock alternates who picks first and which direction each team plays every game in a best-of-3 or best-of-5 series, and gives the board fresh letters each game. Across a series, both teams get the same opportunities.

There is also a twist that makes LetterLock different from pure Hex: claiming a cell is not guaranteed. You have to answer a question correctly, and if you miss, your opponent can steal the hex. That makes the first-move advantage much smaller in practice, and it adds a layer of strategy Hex does not have: a hex that matters to both teams is worth more, and picking it with a letter your team is unsure about can hand it to your opponent.

Detecting the winner, correctly

The most important rule in the game is "who has won?", and it is checked after every claim. If that check is ever wrong, nothing else about the game matters. We built it test-first, before any screens existed.

Union-find

The game uses a union-find structure (also called a disjoint-set structure). Each team has two virtual nodes standing for its two edges. When a team claims a hex, we join that hex with any neighbouring hexes the team already owns, and with an edge node if the hex sits on that edge. A team has won the moment its two edge nodes end up in the same set. Each check takes almost constant time, so it is fast even on the largest board.

A slow, obviously-correct oracle

Fast code is easy to get subtly wrong, so the tests compare it against a deliberately simple flood fill: start from every hex a team owns on one edge, walk to neighbouring hexes the team owns, and see whether the walk reaches the other edge. It is slower, but it is easy to read and easy to trust. The tests then generate large numbers of random boards and random move orders and require the two methods to agree every time, on every board size.

Events, not snapshots

Every action in a game (a pick, a correct answer, a steal, an undo) is recorded as an event in an append-only log, and the board is calculated from the events. That makes undo trivial, makes a saved game a short list of events, and means online rooms can send the same events to every device. If two screens ever disagreed about the board, the event log would show exactly where.

Why this matters beyond a game

The habits are the same ones we bring to client systems: identify the logic that would do the most damage if it were wrong, write it as plain code that is separate from the interface, test it against a simpler independent version, and record changes as events so you can always explain how you got to the current state.

You can try the board yourself at letterlock.raltech.dev, read the full rules and the strategy guide, or see the full LetterLock case study.