← Back to Blog

How Minimax AI Works in Connect Four

Playnook Games Blog · 5 min read

Connect Four looks simple — drop discs, get four in a row — but it's a great example for understanding how game-playing AI actually thinks. Here's what's happening behind the scenes when you play against the computer.

The core idea: think ahead, then work backward

Minimax works by imagining future moves several turns ahead, scoring how good each resulting position would be, then working backward to decide the best move right now. The AI assumes you'll always play your best possible move too — so it's not just looking for good outcomes, it's looking for the best outcome assuming an opponent who's also trying to win.

Maximizing and minimizing

The "min" and "max" in minimax refer to alternating turns: the AI tries to maximize its own score on its turn, and assumes you'll try to minimize that same score (i.e., play the move that's worst for the AI) on yours. By simulating this back-and-forth several moves deep, the algorithm finds the move that leads to the best guaranteed outcome, not just the best immediate one.

Why depth matters — and why it's expensive

Every additional move the AI looks ahead multiplies the number of positions it has to evaluate. Looking 4 moves ahead in Connect Four means evaluating thousands of positions; looking 8 moves ahead means millions. This is why difficulty settings usually work by capping how many moves ahead the AI is allowed to search — a shallow search plays weaker, more human-like moves, while a deep search plays close to perfectly.

Alpha-beta pruning: skipping the pointless branches

Alpha-beta pruning is an optimization that lets the AI skip evaluating branches it can already prove won't be chosen — if the AI already knows one option guarantees a good outcome, it doesn't need to keep checking a branch that's already looking worse than that. This can cut the number of positions evaluated dramatically without changing the final decision at all, which is what makes it possible for the AI to search deeper in the same amount of time.

Why the AI sometimes "sees" a trap you didn't

Because minimax evaluates full sequences of moves rather than just the current board, it can spot forced wins that are several moves away — a setup where every one of your replies leads to the same loss. That's usually what's happening when the AI's move seems to come out of nowhere: it already worked out that the position was lost several moves before the trap actually closed.

See it in action

Playnook Games' Connect Four has Easy, Normal, and Hard AI difficulty (minimax search depth 3, 4, and 6) — a good way to feel the difference searching deeper makes.

▶ Play Connect Four