Games and adversarial search

Tic-tac-toe

Plays tic-tac-toe against a minimax, alpha-beta, or depth-limited player, shows the minimax value of every move and the game tree below the position, counts the nodes minimax and alpha-beta visit under different move orderings, and scores cut-off positions with a weighted evaluation function.

  • Lecture reference: Games and Adversarial Search · slide 6
  • Lecture reference: Games and Adversarial Search · slides 10–11
  • Lecture reference: Games and Adversarial Search · slides 21–25

Game

Squares 1–9 row by row. Click a square, or move with the arrow keys and press Enter.

X to move MAX · Human

Minimax value 0: a draw with perfect play by both sides.

Values on empty squares

Every move has the same value, so none is marked best. Values are for MAX (X): +1 X wins, 0 draw, −1 O wins, with perfect play by both sides. X picks the largest value, O the smallest; ties go to the first square.

X is MAX and moves first; O is MIN. Terminal utilities for MAX: +1 when X wins, 0 for a draw, −1 when O wins. Lecture reference: Games and Adversarial Search · slide 6

Game tree below the current position

Each board shows its minimax value, backed up from the terminal states: MAX takes the largest child value, MIN the smallest. Heavier edges and frames mark the best children; the shaded square is the move just played. Click a child board to play its move. Lecture reference: Games and Adversarial Search · slides 10–11

Search statistics nodes visited

From the empty board

  • Minimax 549,946
    255,168 terminal states
  • Alpha-beta, square order 18,297
    3.3% of minimax · 7,330 terminal · 4,237 cutoffs
  • Alpha-beta, center, corners, edges 7,275
    1.3% of minimax · 2,893 terminal · 2,156 cutoffs
  • Alpha-beta, best move first 2,312
    0.42% of minimax · 781 terminal · 1,045 cutoffs

Move ordering

Pruning does not affect the final result; the amount of pruning depends on move ordering. With perfect ordering the time to find the best move drops from O(bm) to O(bm/2): search depth is effectively doubled.

Lecture reference: Games and Adversarial Search · slide 23

The whole game

From the empty board the game tree has 549,946 nodes and 255,168 complete games (131,184 won by X, 77,904 by O, 46,080 drawn), but only 5,478 distinct positions. A transposition table stores each once.

Lecture reference: Games and Adversarial Search · slide 26

Nodes visited counts every call of Max-Value and Min-Value, the root included. A cutoff is a return at v ≥ β (MAX) or v ≤ α (MIN) that leaves children unsearched. Center, corners, edges tries square 5, then 1, 3, 7, 9, then 2, 4, 6, 8; best move first sorts children by their minimax value. Lecture reference: Games and Adversarial Search · slides 21–22

Evaluation function

Eval(s) = w₁ X₂(s) + w₂ X₁(s) + w₃ O₂(s) + w₄ O₁(s) Lecture reference: Games and Adversarial Search · slide 24

X₂(s) lines with two X and no O
= 0
X₁(s) lines with one X and no O
= 0
O₂(s) lines with two O and no X
= 0
O₁(s) lines with one O and no X
= 0

Eval of the current position: 0

plies
Depth-limited values and minimax values of each move
MoveCutoff 3Minimax
Square 1 (top left) +30 (draw)
Square 2 (top middle) +20 (draw)
Square 3 (top right) +30 (draw)
Square 4 (middle left) +20 (draw)
Square 5 (center) returned+50 (draw)
Square 6 (middle right) +20 (draw)
Square 7 (bottom left) +30 (draw)
Square 8 (bottom middle) +20 (draw)
Square 9 (bottom right) +30 (draw)

Depth-limited search visits 586 nodes and scores 504 of them with Eval; with alpha-beta pruning, 167 nodes.

Cutting off search

Horizon effect: a state may be misjudged because of an event just beyond the depth limit, such as a damaging move by the opponent that can be delayed but not avoided. Remedies: quiescence search does not cut off search at unstable positions; a singular extension is a strong move that is still tried when the normal depth limit is reached.

Lecture reference: Games and Adversarial Search · slide 25

Weights

In chess, a weight may be a piece’s material value (pawn 1, knight 3, rook 5, queen 9) and its feature the advantage in that piece. Weights can come from game databases or from the program playing many games against itself. Here a win scores ±100: each line counts toward at most one feature, so |Eval| ≤ 8 × 10 and a win always outranks an evaluation.

Lecture reference: Games and Adversarial Search · slide 24

Games and adversarial search

Types of game environments

DeterministicStochastic
Perfect informationfully observableChess, checkers, goBackgammon, monopoly
Imperfect informationpartially observableBattleshipsScrabble, poker, bridge

Tic-tac-toe: deterministic, perfect information.

Lecture reference: Games and Adversarial Search · slide 3

Alternating two-player zero-sum games

  • Players take turns.
  • Each terminal state has a utility for each player.
  • The sum of both players’ utilities is a constant. Here X gets +1, 0, or −1 and O the negative, so the sum is 0.
Lecture reference: Games and Adversarial Search · slide 4
  • The opponent’s moves are not known, so the solution is a strategy (policy): a mapping from each state to the best move in that state.
  • The time to make a move is limited, and branching factor, depth, and the number of terminal configurations are huge.
  • Chess: branching factor ≈ 35 and depth ≈ 100 give a search tree of 10¹⁵⁴ nodes, against ≈ 10⁸⁰ atoms in the observable universe. Tic-tac-toe: at most 9 moves, 549,946 nodes.
Lecture reference: Games and Adversarial Search · slide 5

Advanced techniques

  • Transposition table to store previously expanded states. This tool keeps minimax values by board: 5,478 distinct positions instead of 549,946 nodes.
  • Forward pruning to avoid considering all possible moves.
  • Lookup tables for opening moves and endgames.
Lecture reference: Games and Adversarial Search · slide 26

Chess playing systems

SystemDepthStrength
Baseline 200 million node evaluations per move (3 min), minimax with a decent evaluation function and quiescence search5-ply≈ human novice
Baseline + alpha-beta the same budget with alpha-beta pruning10-ply≈ typical PC, experienced player
Deep Blue 30 billion evaluations per move, singular extensions, an evaluation function with 8000 features, large databases of opening and endgame moves14-ply≈ Garry Kasparov
Hydra (ca. 2006) 36 billion evaluations per second, advanced pruning techniques18-ply≈ better than any human alive?
Lecture reference: Games and Adversarial Search · slide 27