Skip to main content
gamesalgorithmsjavascript

Connect Four: Play Against a Minimax AI With Alpha-Beta Pruning

Free browser Connect Four against a real minimax AI opponent using alpha-beta pruning. Drop discs, connect four in a row, and try to outsmart the computer.

WebdevToolbox Team

Connect Four is famously “solved” — with perfect play, the first player always wins. Building a perfect solver requires searching a game tree far too large to explore exhaustively in a browser on every move, so our Connect Four tool uses a more practical approach: minimax search with alpha-beta pruning, looking 4 moves ahead, combined with a position-scoring heuristic.

How the AI decides its move

For each of the 7 possible columns, the engine simulates dropping a disc there, then recursively explores how the game could continue for several more moves using minimax — alternating between maximizing the computer’s outcome and minimizing the human’s. Alpha-beta pruning cuts off branches that can’t possibly change the final decision, making the search dramatically faster without changing the result.

When the search hits its depth limit without a decisive win/loss, a heuristic function scores the position by counting potential winning lines (2-in-a-row, 3-in-a-row) for each side — rewarding positions where the computer has more open lines than the opponent.

Why the center column matters

The center column (and its neighbors) participate in more possible four-in-a-row lines than the edge columns — horizontally, vertically, and along both diagonals. Move-ordering in the search checks central columns first, both for search efficiency and because central control is genuinely stronger strategically.

Verified blocking behavior

We tested the engine by building a three-in-a-row threat (three red discs with one open end) — the computer correctly identified and blocked the winning move before it could be completed, confirming the lookahead search catches real tactical threats rather than just playing on heuristics alone.

Play a game — if you don’t spot threats early, the AI will punish the mistake.

Tagged with

gamesalgorithmsjavascript

Related posts