← Back to Blog

Connect 4 is a solved game. In 1988, Victor Allis proved that the first player can always win with perfect play. But solving a game in theory and playing it well in practice are different problems. The Hard AI on PlayVersusAI plays at near-perfect strength, finding the best move in every position within a few hundred milliseconds, all running inside your browser. Here is how it works.

Minimax: thinking ahead by thinking for both sides

The fundamental idea behind game-playing AI is called minimax. The algorithm imagines what would happen if both players played perfectly: the current player (the maximiser) picks the move that leads to the best outcome, while the opponent (the minimiser) picks the move that leads to the worst outcome for the current player. By alternating between these perspectives all the way to the end of the game, the algorithm finds the objectively correct move.

In Connect 4, this means looking at every possible column to drop a disc, then every possible response, then every possible reply to that response, and so on until somebody wins or the board fills up. At each level, the algorithm assumes optimal play from both sides.

The problem is scale. Connect 4 has roughly 4.5 trillion possible positions. Even on fast hardware, checking every single one is not feasible in real time. This is where the second idea comes in.

Alpha-beta pruning: ignoring branches that cannot matter

Alpha-beta pruning is an optimisation that dramatically reduces the number of positions minimax needs to examine. The core insight is simple: if you have already found a move that guarantees a certain score, and you discover that a different move could give the opponent a way to do better than that, you do not need to explore that branch any further. It cannot possibly be chosen, so you cut it off (prune it) and move on.

In practice, alpha-beta pruning reduces the effective branching factor from seven (the number of columns) to something closer to three or four. This means the AI can search much deeper into the game tree within the same time budget — often reaching twenty or more moves ahead, which in Connect 4 frequently covers the rest of the game.

The effectiveness of alpha-beta pruning depends heavily on the order in which moves are examined. If the best move is checked first, more branches can be pruned. Our AI uses move ordering heuristics — checking the centre columns before the edges, and checking moves that create threats before neutral moves — to maximise pruning efficiency.

Bitboards: the secret to speed

The third piece of the puzzle is the board representation. A naive approach would store the Connect 4 grid as a 6×7 two-dimensional array and check for wins by scanning rows, columns, and diagonals. This works but is slow, especially when the algorithm needs to check millions of positions per second.

Instead, our AI uses a bitboard representation. The entire board state is encoded as two 64-bit integers — one for each player's discs. Each bit in the integer represents one cell on the board. Checking whether a player has four in a row becomes a matter of bitwise operations rather than nested loops.

The win-check, for example, reduces to four bitwise shift-and-AND operations — one for each direction (horizontal, vertical, and the two diagonals). The entire check takes nanoseconds, which adds up to an enormous speedup when the operation is performed millions of times during a single AI turn.

Evaluation and heuristics

When the search reaches its depth limit without finding a definitive win or loss, the AI needs to estimate who is ahead. The evaluation function scores a position based on several factors: how many three-in-a-row threats each player has, whether those threats are playable (the cell below is occupied), and how much control each player has over the centre columns.

Centre control is particularly important in Connect 4. Discs in the middle columns participate in more potential four-in-a-row lines than discs on the edges, so the evaluation function gives them a higher weight. This aligns with the known optimal strategy: the first player's best opening move is always the centre column.

Difficulty levels

The four difficulty levels on PlayVersusAI control how deeply the AI searches and whether it occasionally makes suboptimal moves. Easy mode searches only a few moves ahead and sometimes plays randomly; Hard mode searches to the maximum depth and always plays the best move it finds. Medium and Hard-minus fall in between, with progressively deeper searches and fewer random mistakes.

Even on Easy, the AI uses the same minimax engine — it simply stops searching earlier and introduces controlled noise into the evaluation. This means the AI still plays logically recognisable moves rather than random nonsense, which makes it more useful as a learning tool.

Running in your browser

All of this runs entirely in JavaScript, in your browser, with no server calls. Modern JavaScript engines compile code to near-native speed, and the bitboard tricks keep the memory footprint minimal. The Hard AI typically evaluates between one and five million positions per move, finishing in under 500 milliseconds on most devices.

If you want to see the AI at work, try playing a game on Hard difficulty and pay attention to how it handles threats. It never misses a forced win and never falls for a trap. Beating it requires the kind of long-range planning that only comes from understanding the game deeply — which is exactly the point.

← All Posts Play Connect 4 →