Home/Game AI/Minimax
Search algorithmsCTRL K

Minimax

· game tree search · perfect opponent
/gameai/minimax
VISITED
1/ 40
nodes entered
LEAVES
0/ 27
positions scored
BEST
—
value at the current node
DEPTH
0/ 3
plies below the root
TURN
MAX
who moves at this node
currentevaluatedbest lineunvisited
b = 3 · depth 3 · 27 leaves · seed 7 · 93 steps
step 0 / 92
Depth3
minimax.ts
1
function minimax(node: Node, depth: number, maximizing: boolean): number {
2
  if (depth === 0 || node.isLeaf) return node.value;
3
  let best = maximizing ? -Infinity : Infinity;
4
  for (const child of node.children) {
5
    const value = minimax(child, depth - 1, !maximizing);
6
    best = maximizing ? Math.max(best, value) : Math.min(best, value);
7
  }
8
  return best;
9
}
CURRENT STEP
line 1

Enter a MAX node at depth 0: MAX will keep the largest child value.

// how it works

Understanding Minimax

minimax · assume the worst, pick the best
01

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.

02

The four stages

1
descendvisit the first child until a leaf
2
scoreleaf returns its evaluation
3
back upMAX keeps the largest, MIN the smallest
4
choosethe root's best child is the move
03

Complexity

best
O(b^d)no pruning: every leaf is scored
average
O(b^d)3⁴ = 81 leaves at depth 4
worst
O(b^d)same, the tree is always complete
space
O(d)one path of the recursion
04

Versus its siblings

LEAVES SCORED · b = 3, d = 4
minimax
81
negamax
81
expectimax
81
alpha-beta
44
alpha-beta, ordered
17
same tree · ordered means the best move is tried first
05

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
06

When to use

Two-player, turn-based, perfect-information games with a small enough tree: tic-tac-toe, Connect 4 with a depth limit, endgames.
As the base every real engine builds on: alpha-beta, iterative deepening and transposition tables are all minimax with less work.
07

Pitfalls

Exponential: each extra ply multiplies the work by the branching factor. Depth-limit it and evaluate the frontier.
The evaluation function is everything; minimax only propagates it. A bad heuristic gives confident bad moves.
The horizon effect: a disaster one ply beyond the depth limit is invisible. Quiescence search extends captures.
08

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.

Back to Game AI