← All animations

πŸ”΄ Connect Four

Drop a disc, the AI drops one back, first to line up four wins. The opponent is a minimax search: it imagines your reply, and its reply, and yours… as deep as the look-ahead depth you set. Slide it from 1 (greedy and beatable) up to 6 (genuinely tough) and feel the board getting smarter as it learns to think further ahead.

1. Play it β€” you're red, the AI is yellow

Click a column to drop your disc; gravity pulls it to the lowest empty slot. The AI replies by running minimax with alpha-beta pruning to the depth you choose. Win by getting four in a row β€” horizontal, vertical, or diagonal β€” and the winning line lights up.

depth 4 Your move β€” drop a disc

Depth 1 only checks its own immediate move (greedy). Each extra ply lets it see one move further into the future β€” blocking traps you haven't sprung yet and setting up double threats.

2. Peek inside the AI β€” why it picked that column

Turn this on and, before the AI moves, it shows the score it gives each legal column β€” a green bar means "good for me", red means "good for you / I'd lose". The AI plays the tallest bar. Watch it spike to block when you have three in a row, and build toward its own four.

Enable, then make a move to see the AI's evaluation

Connect Four is "solved": with perfect play the first player wins by opening in the centre column. This heuristic AI isn't perfect β€” at low depth you can out-plan it β€” but the centre bias you'll see in its scores is exactly why the centre is so strong.

3. How the AI thinks β€” heuristic minimax

Minimax: assume the opponent plays their best

Minimax explores a tree of future moves. On its own turns the AI picks the move that maximises its score; on your turns it assumes you'll pick the move that minimises it (your best is its worst). It backs those values up the tree, so the score of a move already accounts for the best reply, and the best counter-reply, and so on.

Why we can't brute-force it: a heuristic

The full Connect Four tree has ~4.5 trillion positions β€” too many to enumerate in a browser. So we stop at a fixed depth and score the not-yet-finished board with an evaluation function: a quick guess at who's ahead. Ours scans every 4-in-a-row window and rewards lines where the AI has discs and you have none (two = a little, three = a lot), penalises your lines the same way, and nudges discs toward the centre (more lines pass through the middle). A real win or loss scores Β±infinity.

Depth vs. strength

The deeper the search, the further ahead the AI sees β€” so it stops spotting only the obvious and starts avoiding traps two and three moves out, and setting double threats you can't block both of. Alpha-beta pruning lets it skip branches that can't change the answer, so it reaches deeper for the same work. Slide the depth up in scene 1 and the same evaluation function plays markedly tougher β€” pure look-ahead.

"Solved" games have a known perfect outcome

A game is solved when the result under perfect play is known. Connect Four was solved in 1988: the first player wins by starting in the centre column. Checkers is solved as a draw. Chess and Go are not β€” their trees are still far too big β€” so the world's best engines, like our toy here, rely on heuristic search rather than perfect knowledge.