Connect Four AI: the minimax algorithm behind Neon Drop
Every good phone game needs an opponent that answers quickly, plays a fair game, and can be beaten when you want it to be. For Neon Drop, my four-in-a-row game, that opponent is a classic minimax search with alpha-beta pruning and a small evaluation function. This post walks through how the Connect Four AI actually works, what its heuristic reveals about good four-in-a-row strategy, and where a depth-limited search falls short of perfect play.
(Four-in-a-row is the public-domain game mechanic; “Connect Four” is a Hasbro trademark. The algorithms are identical, so I use both terms.)
How the AI searches the game tree
Minimax treats the game as a tree of moves. From the current board, the AI (the maximizing player) considers every legal column, then imagines your best reply (the minimizing player), then its reply, and so on. Leaf positions are scored: a win for the AI is +10,000,000, a win for you is −10,000,000, and a draw is 0. The AI plays the move whose worst-case outcome is best.
The depth limit matters, because the full tree is enormous. With seven columns the branching factor is about seven, so looking six moves ahead reaches on the order of a hundred thousand positions before any pruning.
Alpha-beta pruning
Alpha-beta pruning is what makes that depth practical on a phone. It tracks the best score the maximizer can already guarantee (alpha) and the best the minimizer can guarantee (beta). Once a branch can no longer change the decision, the search drops it. The move it returns is exactly the one plain minimax would pick, found by evaluating far fewer positions.

Even with a simple left-to-right move order, alpha-beta cuts the work about 19-fold at six-ply depth in Neon Drop’s search. Searching the center columns first, where more lines run, would prune even more.
The evaluation heuristic
When the search hits its depth limit without a win or loss, it has to estimate how good the position is. Neon Drop scores every window of four cells (horizontal, vertical, and both diagonals) and adds a bonus for the center column, where the most winning lines pass through.
// score one window of four cells, from the AI's point of view
if pieceCount == 4 { score += 1000 }
else if pieceCount == 3 && emptyCount == 1 { score += 10 }
else if pieceCount == 2 && emptyCount == 2 { score += 4 }
if opponentCount == 3 && emptyCount == 1 { score -= isHard ? 80 : 15 } // block threats

Three of the AI’s pieces in a window with room to finish it is worth +10; two with room is +4; a piece in the center column adds +6. The last line is defense: an opponent three-in-a-row that could become four is penalized, and much more harshly on Hard (−80 versus −15).
Difficulty levels
Search depth on its own makes a stiff, robotic ladder. Neon Drop’s three levels combine depth with two other knobs.
enum Difficulty {
case easy, medium, hard
var searchDepth: Int { // plies to look ahead
switch self { case .easy: return 2; case .medium: return 4; case .hard: return 6 }
}
var blunderChance: Int { // percent chance of a random legal move
switch self { case .easy: return 30; case .medium: return 10; case .hard: return 0 }
}
}
Easy looks two moves ahead and plays a random legal move 30% of the time; Medium looks four ahead and blunders 10%; Hard looks six ahead, never blunders, and weights blocking your threats more heavily. The blunder chance is what makes the easier levels feel human: they slip the way a casual player does, instead of playing a weaker but still machine-consistent game.

In positions with a free win available or an immediate threat to block, Hard is perfect, Medium is right about 90% of the time, and Easy about 75%, which tracks the blunder rates. Measured over 1000 trials each.
What the AI reveals about Connect Four strategy
The heuristic weights are a compact summary of good four-in-a-row strategy:
- Play the center. The center column sits on the most horizontal, vertical, and diagonal lines, so it earns an explicit bonus. Strong players open in the center for the same reason.
- Build and block threes. A three-in-a-row with an open fourth cell is one move from winning, which is why it scores far above a two, and why the AI drops everything to block yours.
- Aim for two threats at once. The real winning idea is a fork: two threats a single block cannot both stop. Minimax finds these on its own, because a position that forces a win a few moves later already scores as a win.
The same evaluation that drives the AI works as a checklist for a human: control the center, respect threes, and set up double threats. (For a very different game-modeling problem, I wrote about predicting the next pitch in baseball.)
Is Connect Four solved?
Yes. Standard 7x6 four-in-a-row is a solved game: with perfect play the first player wins by starting in the center column (Victor Allis, 1988; see also John Tromp’s solver). Neon Drop’s AI is a strong heuristic player rather than a perfect solver: it searches a limited depth and leans on the evaluation function, which is fast and fun to play against without being the game-theoretic optimum.
Scope and limits
The engine is a clean, standard implementation, and it leaves a few things on the table. It uses a plain 2D board rather than bitboards, and it has no transposition table, move ordering, or opening book, all of which a competition-grade solver would use. The search also reasons about the base drop game only: the power-ups (Bomb, Blocker, Undo) and the 5x5 and 8x8 boards work in the app, but the AI does not plan around them. On the larger boards the same code still searches, over more columns.
Try it
Neon Drop is free on the App Store. Pick a difficulty and see how far ahead you can think.
The figures come from a faithful Python port of the app’s Swift AI, run to measure node counts and tactical accuracy; the numbers are illustrative of the method.
This is an independent project I build on my own time. The views are my own and do not represent any current or former employer.
Frequently asked questions
Is Connect Four a solved game?
Yes. On the standard 7x6 board, four-in-a-row is a solved game: with perfect play the first player wins by starting in the center column, a result proved by Victor Allis in 1988.
Does the first player always win at Connect Four?
Only with perfect play from the center column. Most real games are decided by mistakes, so the theoretical first-player advantage rarely settles a casual game.
What algorithm do Connect Four AIs use?
The common approach is minimax search with alpha-beta pruning and a heuristic evaluation function. Neon Drop uses exactly that, with search depth and a blunder chance setting the difficulty.
What is the best Connect Four strategy?
Control the center column, watch for your opponent's three-in-a-rows and block them, and try to create two threats at once (a fork) so a single block cannot stop both.
How does alpha-beta pruning speed up the search?
It skips branches that cannot change the final decision by tracking the best score each side can already guarantee. It returns the same move as plain minimax while evaluating far fewer positions, which is what lets the AI look six moves ahead instantly.
How strong is the Neon Drop AI?
On Hard it looks six moves ahead, never blunders, and blocks threats aggressively, which makes a tough casual opponent. It is a heuristic player rather than a perfect solver, so a strong human who plays the solved first-player line can still beat it.