← Hex against the computer

Hex Strategy: Bridges, Edge Templates and How to Beat the Computer

The numbers on this page come from simulations that run the game's own code — its evaluation, its search and its three levels. Normal plays at random among its three best moves, so its figures rest on 5,000 games per rule. Hard has no randomness; its winning lines were replayed move by move against the engine with the real clock. Expert thinks against a clock (on our test computer it reached five or six moves ahead), so it was simulated at a fixed depth of four (500 games per rule), and its winning line was checked at depths five and six and with the real clock. The theory — no draws, the first player wins, the solution of 7×7 — comes from published mathematics and is marked as such.
The short answer

In Hex you win by connecting, not by blocking, and the tool for it is the bridge: two of your stones with two empty cells between them cannot be cut, because if the computer takes one cell you take the other. Hard looks only two moves ahead and plays the same reply every time, so seven moves beat it, the fewest a win can take: g1, f2, e3, d4, c5, b6, b7. Against Expert, d4, f2, f3, f4, e6, f1, f5, e7 won every time we tried it at its usual depth, and a player who weighs both paths — his own and the computer's — wins 95% of games against Normal.

The board here, and how cells are named

The board is a 7×7 rhombus of hexagons. You are red and join the top edge to the bottom; the computer is blue and joins the left edge to the right. You always move first, and there is no swap rule, so the computer can never take your opening stone for itself.

The game shows no labels, so this page names cells like a chessboard: columns a–g from left to right along your top edge, rows 1–7 from top to bottom. Each row sits half a cell to the right of the one above, so the board leans to the right. The centre is d4; your edges are rows 1 and 7, the computer's are columns a and g.

Every inner cell has six neighbours. For d4 they are c4 and e4 in its own row, d3 and e3 above it, and c5 and d5 below it. The rule to remember: above a cell are the same letter and the next one, below it the previous letter and the same one. So a straight line down the board does not run down one column: it drifts one letter to the left every row, like g1, f2, e3, d4, c5, b6, a7.

The two pointed corners, a1 and g7, have only two neighbours each; the two blunt corners, g1 and a7, have three. Corner cells belong to both players' edges.

Why there are no draws, and why you are winning

Two theorems stand behind every game here; the article under the board tells their history, so this is only what they mean for you.

  • No draws. When the board is full, exactly one player is connected: a red chain from top to bottom and a blue one from left to right cannot both exist, and blocking one always builds the other. So every stone you place to stop the computer is also part of some path of yours. (Theory: the Hex theorem, proved by David Gale in 1979.)
  • The first player wins. If the second player had a winning strategy, the first could play anywhere and then follow it; an extra stone never hurts in Hex. That contradiction — Nash's strategy-stealing argument — shows the first player can always win, without saying how. (Theory.)
  • 7×7 has been solved. In 2003 a team at the University of Alberta worked out, by computer, which opening moves win on this board size; the centre is one of them. Without a swap rule, that means you start every game here from a won position. (Theory: published result, not recomputed here — a 49-cell board is far beyond brute force.)

In practice the theorem promises nothing: from a won position a random player beat Normal once in 5,000 games and never beat Hard in 2,000. What wins is technique, and the rest of this page is about that.

The bridge and the edge templates

Two adjacent stones are connected for good. Two stones that are not adjacent but share two empty neighbours are connected too, as long as you answer: if the computer takes one of the shared cells, you take the other. That shape is the bridge, and it is the basic move of Hex, because it covers two rows with one stone where an adjacent step covers one.

From d4, a bridge toThe two shared cellsDirection
c6c5 and d5two rows down, one letter back
e2d3 and e3two rows up, one letter on
e5e4 and d5one row down, one letter on
c3c4 and d3one row up, one letter back

The first two are the ones that carry you across the board; the last two move you sideways while keeping contact.

The edges have the same trick, called an edge template. A stone on row 6 touches two cells of row 7 — c6 touches b7 and c7 — so if both are empty it is already connected to the bottom: whichever the computer takes, you take the other. A stone on row 2 is connected to the top the same way (c2 touches c1 and d1). Hex players also know larger templates, such as the «ziggurat» for a stone on the third row from an edge, but they need more empty space and more care (theory).

The habit that goes with all of this is to answer the intrusion: when the computer plays into one of your bridges or templates, close it at once. We gave a simple player — one that shortens its own path and nothing else — this single extra rule. Against Normal its wins rose from 63% to 67% (5,000 games each), against Hard from 8.8% to 15% (2,000 games each). Every winning line on this page ends the same way: the computer takes one cell of a template and you take the other.

The computer knows about bridges: its evaluation treats a bridge between two of its own stones as almost finished. It does not know about edge templates, so it counts a stone on your second row as still one cell short of the edge.

Block from a distance, not face to face

The classic advice is that a blocking stone placed right in front of an enemy stone does little: there are always two cells to step around it. A block works when it stands a few rows ahead, where it can meet the path wherever it turns (theory, and long practical experience).

Hard shows why. In the seven-move win below, every one of its stones goes right beside your last one, and you simply step to the other side: g1, f2, e3, d4, c5, b6 run straight down the short diagonal while Hard answers g2, f3, e4, d5, c6 and then a7, too late.

Measured on this engine, what matters is to count both paths. Two simple players, 5,000 games each against Normal:

How the move is chosenWins vs Normal
A random empty cell0.02% (1 game)
Shorten your own path to the far edge, ignore the computer's63%
The same, and answer intrusions into bridges and edge templates67%
Shorten your path and lengthen the computer's: the cell with the best difference95%
The same, looking two moves ahead99.1%

«Path» here means what the computer itself measures: the number of empty cells still missing from the cheapest chain between a player's two edges. The jump from 63% to 95% is the whole value of defence: a move that makes your path one cell shorter and the computer's one cell longer is worth two.

The first move

Theory says the centre wins on 7×7. Against the engines here the first move matters much less than what follows it: we fixed each of the 49 first moves and let five fixed players continue the game against Hard and Expert (one game each, since neither involves chance), and a player who weighs both paths one move ahead played 100 games from each first move against Normal.

First movevs Normal (100 games)Fixed tests won (of 5)
g1 (blunt corner)96%4
e2, f2, g2 (next to it)99–100%3 each
d4 (the centre)97%1
a1, g7 (pointed corners)70%, 90%0, 2
b431%0
All 49 moves, average87%1.3

Two things stand out. The cells around the blunt corner g1 did best, while the centre, the theoretical winner, won only one of its five fixed tests against these particular engines. And the weak first moves are mostly edge cells and a few on the left side, such as b4, a5 and a6: against Normal, edge openings averaged 84% and inner ones 89%.

The fixed tests: a one-move-ahead player and a two-move-ahead player against Hard, one- and three-move-ahead players against Expert at depth four, and a four-move-ahead player against Expert at depth five. They are single games, so treat the column as a hint, not a ranking.

How each level plays, and how to beat it

All three levels first take a winning cell if they have one, then block yours if you threaten to win next move. After that:

  • Normal scores every candidate cell by one move ahead — its own path minus yours — and plays one of the best three at random.
  • Hard looks two moves ahead: its move and your best reply. No randomness, no clock: the same position always gets the same answer.
  • Expert runs the same search deeper and deeper for about 1.2 seconds and plays the best move of the last depth it finished. On our test computer that was five moves ahead, sometimes six, and up to eight near the end; a slower phone gets less. Its play is fixed for a given depth, but the depth depends on the device.

Hard, in seven moves. No win can be shorter, since your chain needs a stone in each of the seven rows. Play g1, f2, e3, d4, c5, b6, b7. Hard answers g2, f3, e4, d5, c6, a7, and b7 completes the chain: b6 had both b7 and a7 below it, and Hard could take only one.

A second seven-move line starts in the centre: d4, e3, f2, d5, f1, c6, c7. Hard answers d2, b3, a5, g3, d6, b7. Your chain runs f1–f2–e3–d4–d5–c6–c7. Both lines also beat the engine at three moves ahead: it answers the first line in the same way and the second differently, and loses both.

Expert, in eight moves. Play d4, f2, f3, f4, e6, f1, f5, e7. Expert answers e2, e3, e4, c6, a7, e5, d7, and you win. Your column f1–f4 joins e6 by a bridge (e5 and f5), e6 joins the bottom by an edge template (d7 and e7), and Expert breaks into both too late. At depth six it answers c6 with d6 and a7 with b7 instead, and the line still wins; it also won all three times we played it against the real clock.

The Expert line depends on how deep Expert gets, so on a slow device it can fail: at depths two to four, and at seven, the computer answers differently and the line runs into an occupied cell. For a slow device there is another line: e2, c4, c3, b6, c5, c2, c1, b7 wins against the engine at depths two, three and four (at four: e3, d3, g2, b5, d2, b1, a7), and no line of seven moves beats depth four from e2, g1 or d4. Hard's lines do not depend on the device. Normal is random: Hard's centre line beat it in only 20% of 2,000 games, so against Normal play by the principles, not by a list.

Your rulevs Normalvs Hardvs Expert*
A random empty cell0.02%0%0%
Shorten your own path63%8.8%0.6%
… and answer intrusions into bridges and templates67%15%1.2%
Best difference of the two paths, one move ahead95%12 of 49 first moves3 of 49 first moves
The same, two or three moves ahead99.1%21 of 49 (two ahead)15 of 49 (three ahead)

* Expert at a fixed depth of four, weaker than it plays on a fast computer; 500 games per rule for the first three rows. The last two rules have no randomness, so against Hard and Expert they were played once from each first move. Normal: 5,000 games per rule.

The rules of thumb

  1. Think in bridges: two rows down and one letter back (d4 to c6) covers two rows with one stone and cannot be cut.
  2. When the computer plays into a bridge or an edge template, close it at once.
  3. A stone on row 2 or row 6 with two empty cells beyond it is already connected to that edge.
  4. Weigh both paths: the best move usually shortens yours and lengthens the computer's.
  5. Block from a distance; a stone right next to the enemy's is walked around.
  6. Against Hard, play a fixed line: g1, f2, e3, d4, c5, b6, b7.

Weighing both paths alone takes a simple player from 63% to 95% against Normal; the fixed lines win against Hard every time.

Questions and answers

Can a game of Hex end in a draw?

No. When the board is full, exactly one player has a connecting chain; blocking the opponent everywhere means you have connected yourself. This is a theorem (Gale, 1979), and it is why the game here always ends with a winner.

Does the first player always win at Hex?

With perfect play, yes: Nash's strategy-stealing argument proves the first player has a winning strategy on any board size, without saying what it is. That is why serious games use the swap rule. The game here has no swap rule and you move first, so in theory you can always win.

What is the best first move in Hex?

On 7×7 the centre, d4, is a proven winning move. Against the computer here the first move matters less than the play that follows: a player who weighs both paths won 87% of games against Normal on average over all 49 first moves, and the cells around the blunt corner g1 did best.

What is a bridge in Hex?

Two of your stones that are not adjacent but share two empty neighbours, such as d4 and c6 sharing c5 and d5. The opponent cannot cut it in one move: if he takes one shared cell, you take the other. It is the basic way to cross the board quickly and safely.

How do you beat the computer at Hex?

Against Hard here, play g1, f2, e3, d4, c5, b6, b7: it wins in seven moves, the fewest possible, every time. Against Expert at its usual depth, d4, f2, f3, f4, e6, f1, f5, e7 won every time we tried. Against Normal, which is random, weigh both paths and answer intrusions into your bridges: that wins about 95% of games.

What is the swap rule in Hex?

After the first move, the second player may take that stone as his own instead of replying. It stops the first player from opening in the centre, so the first move has to be one that is neither too strong nor too weak. The game here does not use it.

Has Hex been solved?

Small boards have. All first moves on 7×7 were solved by computer in 2003, and 8×8 and 9×9 were solved in the following decade. On the tournament sizes, 11×11 and 13×13, it is known that the first player wins but not how.

Play Hex against the computer →
Where to go from here
To cite this page: “Hex Strategy: Bridges, Edge Templates and How to Beat the Computer”, You vs Computer, https://youvscomputer.com/games/hex-strategy.html