How the Four in a Row AI Works

Ever wondered how a Connect 4 bot decides where to play? Here’s what’s happening inside our Four in a Row AI, explained without the maths.

1. It looks ahead

The computer imagines every move it could make, then every reply you could make, then its next move, and so on. Each layer of moves is called a “ply”. The Hard level looks many plies ahead.

2. It scores positions

At the end of each imagined line, it gives the position a score: big plus for a win, big minus for a loss, and smaller scores for things like discs in the middle or lines of three.

3. It assumes you play your best (minimax)

The computer picks the move that is best for it, assuming you’ll always reply with the move that’s worst for it. This is called minimax.

Connect 4 AI minimax tree: three candidate moves, the opponent's replies with scores, and the move with the best worst case chosen
Minimax: pick the move whose worst case is best.

4. It skips moves it doesn’t need to check

A trick called alpha-beta pruning lets it ignore lines that can’t change its decision. Checking middle columns first makes this much faster.

How the levels differ

Level select screen for playing the computer: Easy, Medium or Hard
LevelWhat the AI does
EasyDoesn’t look ahead: takes a win it can see, usually blocks yours, and otherwise plays near the middle
MediumLooks a few moves ahead, always takes wins and blocks threes
HardLooks deep ahead and uses solver knowledge in many positions

Think you can beat it? Play the computer.