Minimax in Connect Four

Definition

Minimax is a recursive algorithm that searches a game tree by alternately maximizing for the player and minimizing for the opponent. It is the foundation of every Connect Four engine.

Explanation

Minimax is the algorithm at the heart of every Connect Four engine. The name describes its operation: it minimizes the opponent's best outcome while maximizing the player's best outcome. The algorithm works recursively. From any position, it considers each possible move, then for each move it considers the opponent's best response, then for each response it considers the player's best counter-response, and so on down to a depth limit or until the game ends.

The recursion produces a tree of evaluations. At each node, the algorithm computes the value of that position by looking at the values of its children. If it is the player's turn at a node, the value is the maximum of the children's values (the player picks the best option). If it is the opponent's turn, the value is the minimum of the children's values (the opponent picks the option worst for the player). Propagating these min and max operations up the tree produces an evaluation for the root position and identifies the best move.

Pure minimax is correct but slow. The Connect Four game tree has trillions of nodes. Searching all of them naively would take centuries even on modern hardware. This is why minimax is always combined with optimizations. The most important optimization is alpha-beta pruning, which skips entire branches of the tree when it can prove they cannot affect the final answer. With alpha-beta, minimax becomes practical for searches to depth 20 or more, which is enough to play perfectly in most positions.

Modern Connect Four engines layer many additional optimizations on top of minimax. Iterative deepening searches progressively deeper. Transposition tables cache previously computed positions. Move ordering helps alpha-beta prune more aggressively. Heuristic position evaluation lets the engine cut off search at non-terminal positions and still produce reasonable answers. All of these techniques accelerate minimax without changing its fundamental correctness. Understanding minimax gives you a window into how the engine sees the game and why its moves are sometimes counterintuitive but always correct.

Example

The engine considers playing column 4. It then considers each of the 7 opponent responses, evaluating each. It picks the response that minimizes its own value, then chooses column 4 only if that value is the highest among all initial moves.

Related Articles

How the Engine Works

Put It Into Practice

Understanding minimax is one thing. Applying it is another.