9
Part II · Games with turns

Hex

Red owns the top and bottom edges, blue owns the left and right, and whoever joins their own two edges first wins. Nobody has ever solved this game on a full-size board, so Robo plays it by imagination: thousands of random games before every move. It is beatable. This chapter shows you how, and why a full Hex board can never be a tie.

Here is the board: a slanted diamond of hexagons. You are red, and the top and bottom edges are yours. Robo is blue and owns the left and right edges. You take turns placing one stone of your color on any empty cell. Stones never move and never come off. You win the moment an unbroken chain of red cells joins the top edge to the bottom edge. Robo wins with a blue chain from left to right. The four corner cells belong to both colors.

That is the whole game, and there is no such thing as a tie: fill the board and exactly one player has connected. The next section proves it.

One more thing. Robo cannot solve this game. Nobody can, once the board gets big, and even 6 by 6 is far beyond the look-at-every-game method from Chapter 7. So this Robo plays by imagination: before each move it plays thousands of random games in its head, some for every empty cell, and picks the cell that won most often. The last section explains the method. It is real, and it is beatable.

Hex

6 by 6. Red joins top to bottom. Blue joins left to right. No draws.
Robo strength
random finishes per empty cell
You: top to bottomRobo: left to right
Games imagined0
Robo's picknone yet
Its win rate0%
Thinking time0 ms

No draws, ever

In tic-tac-toe the board fills up and nobody wins. Try to make that happen in Hex. Fill every cell with red and blue in any pattern at all, and there is always a winner. The button below fills a board at random; find the winning chain by clicking its cells, edge to edge. Or clear the board, paint it by hand, and try to build a full board with no winner. You will not manage it.

Fill the board

Every full board has exactly one winner. Find the chain, or try to build a board without one.

Chains traced: 0 of 3

Half of the fact is easy. There can never be two winners: a red chain from top to bottom is a wall, and a blue chain from left to right would have to cross it, but no cell is both colors. The other half, that there is always at least one, is the surprise. Think of the red cells as land and the blue cells as water. If no dry path runs from the top shore to the bottom shore, the water must get all the way across from left to right. It feels obvious. It is true, but a real proof takes care, and the shape of the cells matters: on a board of squares, where cells that touch only at a corner do not count as connected, a checkerboard has no winner at all. Two hexagons that touch always share a whole edge.

In 1979 the mathematician David Gale proved something strange: this Hex fact is equivalent to a famous theorem of topology (say: tuh-POL-uh-jee) called Brouwer's fixed point theorem. Take two identical maps of Texas, crumple one up, and drop it on top of the flat one. Brouwer says some point on the crumpled map lies exactly above the same point on the flat map. Gale showed that each statement can be turned into the other.

The first player can always win, and nobody knows how

Here is a strange fact about Hex. On a board of any size, the player who moves first can force a win. We know this for certain. And on the standard 11 by 11 board, nobody knows what the winning first move is, let alone the plan that follows it. The proof has the same shape as the one for Chomp in Chapter 8: strategy stealing.

Claim. The first player has a winning strategy.

  1. Suppose not. Hex has no draws, so one of the two players must have a winning strategy (the rule from Chapter 5, and it needs the no-draw fact from the last section). If it is not the first player, it is the second: a rulebook that tells blue how to answer every red move and always end up connected.
  2. Red steals it. Red plays a stone anywhere, then pretends to be the second player: red treats blue's reply as the first move of the game and answers it from the rulebook.
  3. The spare stone. Sometimes the rulebook tells red to play on a cell where red already has a stone, the one from step 2. Then red plays anywhere else, and that new stone becomes the spare.
  4. A spare stone never hurts. If a set of red stones connects top to bottom, the same set plus one more red stone still connects, and the extra stone gives blue nothing, because blue's chains are made of blue. So red, following blue's winning rulebook with a stone to spare, wins. Both players cannot win, so step 1 was wrong.

Look at what the proof does not do. It never says which first move wins. It only shows that a winning plan for the second player is impossible. Step 4 is what makes it work, and it is the step that fails almost everywhere else. In chess an extra piece can block your own rook. In Hex an extra stone can only add chains, never remove them, and never a blue one.

For small boards, computers have gone the rest of the way and found the winning moves: every board up to 9 by 9 is solved. The 11 by 11 board is still out of reach: its winning first move exists, and nobody has found it.

How Robo thinks when it cannot think it through

Chapter 7's Robo solved tic-tac-toe by looking at every possible game. A 6 by 6 Hex board has 36 cells. Just the first ten moves can happen in 36 × 35 × 34 × ... × 27 ways, about 920 trillion (9.2 × 1014), and at that point the game has barely started.

So Robo does something different, much dumber, and surprisingly effective. For each empty cell, Robo imagines putting its stone there and then finishing the game at random: the remaining cells are filled with red and blue stones in a random order, and Robo checks who won (someone always has). It does this N times per cell, counts how many finishes it won, and plays the cell with the most wins. The strength control sets N: 50, 200, or 800 finishes per cell. This is the Monte Carlo method (say: MON-tee CAR-lo), named after the casino town, because it turns gambling into measurement.

Why would that work? A random finish is a terrible game; both sides play nonsense. But a genuinely strong cell wins a little more often even in nonsense games, and a nearly useless one loses a little more often. Robo cannot see a plan, only a rate. That is why you can beat it: a quiet plan, like the bridge in the Challenge below, is invisible to it until it is finished.

Inside Robo's head

The five best cells from Robo's latest move in the game above, with the counts that chose them.

Same position, fresh random games. Watch the numbers move.
Math corner

Estimating a probability by counting. Each cell has a true win rate: the fraction of all possible random finishes it wins. Robo cannot compute that fraction, so it samples N finishes and counts:

win rate ≈ wins ÷ N

The estimate wobbles, and the wobble shrinks as N grows, but slowly. For a rate near one half, the typical error is about

1 ÷ (2 × √N)

which is 7 percentage points at N = 50, 3.5 at N = 200, and 1.8 at N = 800. Multiplying N by 4 cuts the error in half: to be twice as accurate you need four times as many games. Press "Imagine again" and watch the top of the table shuffle. That is the wobble, and it is why two cells a few wins apart are really a coin flip.

Big idea

When a game is too big to solve, you can still measure it. Try each move, finish the game at random many times, and count. More samples give a better estimate, but four times the samples buy only twice the accuracy. The programs that beat the world's best Go players are built on a much smarter version of this idea.

Try it on paper

1. Hex on a 2 by 2 board. Red moves first. Which first moves win? Decide, then click a cell to check.

Red to move. Click your first move.
Answer

The two wide corners, b1 and a2. From b1, red touches both cells of the bottom row, a2 and b2, so blue can block only one. The sharp corners lose: from a1 red can reach the bottom row only through a2, and blue takes it.

2. The 5 by 5 board has one cell that is the same distance from all four edges. Which is it? Now spin the board half a turn. What happens to the edges, and to that cell?

Answer

c3, the center. Spin the board 180 degrees and it lands exactly on itself: top edge onto bottom, left onto right, every cell onto its opposite, and c3 onto itself. Symmetry does not prove it is the best move, but computers have solved the 7 by 7 and 9 by 9 boards completely, and on both the center is a winning first move.

3. Red to move on the 4 by 4 board below. There is exactly one winning move. Find it on paper, then click it. The board checks with an exact search and, if you are wrong, shows blue's winning reply.

Red to move. Click the winning cell.
Answer

c2. It touches b2, reaches the top edge two ways (b1 and d1), and makes a bridge down to b4 through b3 and c3. Blue can block only one thing per turn.

Challenge

The bridge. Two stones of one color that do not touch, with exactly two empty cells between them that touch both stones, are as good as connected: if Robo plays in one of those cells, you play in the other. Robo, playing by win rates, does not know this. Below, red to move, and exactly one move wins. It builds two bridges at once. Find it. Then win a real game against Robo in which you build at least one bridge; the game log tells you when you have.

Red to move. Click the winning cell.
Answer

c3. It bridges up to d1 through c2 and d2, and down to b5 through b4 and c4. Whichever cell of a bridge blue takes, red takes the other. Any other red move lets blue play c3 itself, leaving its b3, c3, d3 wall only two gaps from complete. It works against an edge too: a stone one row in, with two empty cells below it, is already connected. Against Robo, build a chain of bridges from your top edge to your bottom edge and answer every intrusion in the other cell.

True story

Hex was invented in 1942 by Piet Hein, a Danish poet, designer, and inventor, who published it in the Copenhagen newspaper Politiken under the name Polygon. In 1948 John Nash, then a graduate student at Princeton, invented the same game again without knowing about Hein's. Princeton students called it Nash, and sometimes John, because it was played on the hexagonal tiles of a bathroom floor. Nash is also credited with the strategy-stealing proof above. In 1952 Parker Brothers sold it as Hex, and that name stuck.

Stars in this chapter

Earn them by doing the clever thing, not by clicking around.