How the Connect 4 AI Works: Bitboards, Negamax and Alpha-Beta
How the Connect 4 AI works: a bitboard-based negamax search with alpha-beta pruning and a transposition table, tuned to examine on the order of a million positions per move in your browser.
The Connect 4 opponent on LK Forge is not a chatbot and not a neural network. It is a classical game-tree search — the same family of algorithm behind competitive chess engines — running entirely in your browser, with nothing sent to a server. Here is exactly how it turns a 7×6 grid into a move.
The whole board fits in two numbers
Connect 4 has 42 cells, so the engine stores the position as bitboards: one 64-bit integer per player, one bit per cell. That sounds like a micro-optimisation, but it is the reason the search is fast enough to feel instant. Testing "did that move make four in a row?" becomes a handful of bit-shifts and ANDs instead of a loop over the grid — you shift the player's bitboard by one row, one column and both diagonals, AND the results together, and a non-zero result means four connected discs.
// four-in-a-row test over a single player's bitboard function isWin(b) { const dirs = [1n, 7n, 6n, 8n]; // │ ─ ╲ ╱ (bit offsets) for (const d of dirs) { const m = b & (b >> d); if (m & (m >> (2n * d))) return true; // four in a row } return false; }
Negamax with alpha-beta pruning
To choose a move the engine runs negamax — the single-function form of minimax that works because Connect 4 is zero-sum: whatever is good for me is exactly as bad for you, so one player's score is just the negation of the other's. It plays out possible futures for both sides, scores the leaf positions, and picks the move with the best guaranteed outcome.
On its own that tree is enormous, so the engine adds alpha-beta pruning: it tracks the best score each side can already guarantee (alpha and beta) and abandons any branch that provably cannot beat it. A transposition table then caches positions reached by different move orders so each is analysed once, and iterative deepening searches depth 2, then 3, then 4, and so on until the time budget runs out — always leaving a usable move ready.
// negamax + alpha-beta, one player's turn (simplified from the engine) function negamax(cur, mask, depth, alpha, beta) { if (depth <= 0) return evaluate(cur, mask); let best = -Infinity; for (const mv of legalMoves(mask)) { const v = -negamax(mask ^ cur, mask | mv, depth - 1, -beta, -alpha); if (v > best) best = v; if (best > alpha) alpha = best; if (alpha >= beta) break; // prune: this branch can't matter } return best; }
The position score itself is deliberately simple: it rewards open three-in-a-rows that could still become four and control of the centre column, where more winning lines pass through. Almost all of the engine's strength comes not from a clever evaluation but from how deep it can search before it has to guess.
How much is one more move of lookahead worth?
We measured it. Taking the exact shipped engine and pinning it to a fixed search depth, then playing 120 colour-balanced self-play games between each pair of adjacent depths, gives an Elo gain for every extra ply. The result is lopsided: the first extra ply — going from depth 1 to depth 2 — is worth +953 Elo, because a depth-1 engine only avoids one-move blunders and otherwise plays close to random. After that, each further ply adds a small, non-monotonic amount.
Elo gained by each extra ply of fixed-depth search, 120 colour-balanced games per step (Elo = 400·log₁₀(p/(1−p)), draws count half). Full method and the other three engines are in What One Ply of Search Is Worth.
The three difficulty levels
All three levels run the same engine; the only knob is how far it looks ahead:
- Beginner — depth 2, plus deliberate random mistakes, so its threats are punishable.
- Medium — depth 7: it always takes an immediate win and blocks yours, and plays a solid tactical game.
- Hard — iterative deepening up to depth 22 with about 1.1 seconds per move. In practice it reaches roughly depth 15 in the opening and 19+ in the midgame, examining on the order of a million positions per move. It runs in a background worker so the page stays responsive, and it is extremely strong — though we do not claim it plays perfectly.
That is the whole engine: pack the board into two integers, search it with negamax and alpha-beta, cache what you have seen, and go as deep as the clock allows. Want to see it decide move by move? Read minimax and alpha-beta, step by step, or watch the depth trade-off across four of our engines in What One Ply of Search Is Worth.
FAQ
Does the Connect 4 AI use a neural network or an LLM?
No — the Connect 4 AI is a classical game-tree search — negamax with alpha-beta pruning over bitboards, with a transposition table and iterative deepening. There is no machine-learned model and no server. The engine runs entirely in your browser in a background worker.
How deep does the Connect 4 AI search?
Search depth depends on the level. Beginner searches to depth 2 and mixes in deliberate mistakes, Medium to depth 7, and Hard runs iterative deepening up to depth 22 with about 1.1 seconds per move. In practice Hard reaches roughly depth 15 in the opening and 19 or more in the midgame, examining on the order of a million positions per move.
Why does one more move of lookahead matter so much?
In a seeded self-play benchmark of the shipped engine, the very first extra ply — going from a depth-1 to a depth-2 search — was worth +953 Elo, because a depth-1 engine only avoids one-move blunders and otherwise plays almost at random. Each later ply adds far less: +9 to +55 Elo per step from depth 3 to 7. In other words, almost all of the jump from playing nearly randomly to playing dangerously happens in that first extra ply, and searching deeper past that mostly sharpens play rather than transforming it.
What is a bitboard?
A bitboard stores the whole board as a single integer, one bit per cell. Connect 4 uses one 64-bit number per player, so testing for four in a row takes a handful of bit-shift and AND operations instead of scanning the grid — which is what lets the engine search a million positions a second in a browser tab. That same compact representation doubles as the cache key for the transposition table, letting the engine instantly recognize a position it already analysed by a different move order.
Take on the same engine, three levels deep.
Play Connect 4 →