function minimax(node: Node, depth: number, maximizing: boolean): number {if (depth === 0 || node.isLeaf) return node.value;
let best = maximizing ? -Infinity : Infinity;
for (const child of node.children) {const value = minimax(child, depth - 1, !maximizing);
best = maximizing ? Math.max(best, value) : Math.min(best, value);
}
return best;
}
Enter a MAX node at depth 0: MAX will keep the largest child value.
Understanding Minimax
The idea
Minimax scores a position by looking ahead. At the leaves a static evaluation says how good the position is for the maximising player. One level up, the player to move picks the child that is best for them: MAX takes the largest value, MIN takes the smallest. The root's value is the outcome of perfect play from both sides.
The tree here has three moves per position and random leaf scores from −9 to 9. Every node is visited, every leaf is scored: with branching b and depth d that is b^d leaves, which is why chess engines never search this way without pruning.
The four stages
Complexity
Versus its siblings
Pseudocode
MINIMAX(node, depth, maximizing)
if depth = 0 or node is a leaf: return value(node)
best ← −∞ if maximizing else +∞
for each child of node
value ← MINIMAX(child, depth − 1, not maximizing)
best ← max(best, value) if maximizing else min(best, value)
return best
When to use
Pitfalls
History
John von Neumann proved the minimax theorem in 1928, the founding result of game theory. Claude Shannon described a minimax chess program in 1950, and Alan Turing hand-simulated one in 1951 before any machine could run it. Deep Blue's 1997 win over Kasparov was minimax with alpha-beta at 200 million positions a second.