Connect Four

Drop discs, make four in a row, and watch out for double threats.

Red to move
Human

Tip: press 1–7 to drop a disc.

Under the hood

How the Connect Four AI thinks

Connect Four is a solved game: with perfect play the first player always wins. This AI doesn't solve it. It searches a fixed number of moves ahead and guesses who's winning from there, which makes it a good opponent to push around.

search negamaxpruning alpha-betadepth 1–9 plieseval 69 four-in-a-row windows
01

Centre columns first

It tries columns in the order 4, 3, 5, 2, 6, 1, 7. Strong moves are usually central, and trying them first lets alpha-beta throw away most of the tree early, which is what makes depth 9 fast enough to run in your browser.

02

It scores every possible four

There are 69 places a four-in-a-row can fit. Past its horizon, the AI looks at each one: a line with three of its own discs and a gap is worth +5, two with room is +2, and an opponent's three-with-a-gap costs it −4. Centre-column discs get a small bonus.

03

Fast wins, slow losses

A win is worth a million points minus the number of moves it takes. So it grabs the quickest win, and when it's losing it drags the game out in case you slip. It also always takes a win in one and blocks yours.

04

Depth is its horizon

At depth 1 it only sees its own move. At 2 it sees your reply. Double threats (two ways to win at once) take 4 plies to spot, so low depths walk straight into them. Ties between equal moves are broken at random.

Set a trap

Raise the depth until you can't win, then drop to 3 and try to build a double threat: two different ways to connect four that it can only block one of. Then notice which depth first starts seeing it coming.