Nexus Learn

Lecture 17: Game Playing: Minimax

Lecture Notes CS-511 Artificial Intelligence 7 min read Updated 8 Nov 2026

↓ Download notes (Word)

On this page
  1. Learning Outcomes
  2. 1. Games as Search
  3. 2. The Minimax Algorithm
  4. 3. When the Tree Is Too Big
  5. 4. Common Mistakes
  6. Word Bank
  7. Self-Check Questions

Learning Outcomes

SLO

After this lecture you will be able to...

CLO

Bloom

SLO 17.1

Describe a game as a search problem (players, actions, terminal test, utility) and classify kinds of games

CLO3

C2 Understand

SLO 17.2

Apply the minimax algorithm to back up values in a game tree and choose the best move

CLO3

C3 Apply

SLO 17.3

Explain the cost of minimax and use a depth limit with an evaluation function

CLO3

C3 Apply

§1.1 Kinds of games

Slide L17-S06

In all earlier search problems only one agent acted. In a game a second agent, the opponent (adversary), chooses every other move and wants us to lose; searching in this setting is called adversarial search. Two questions classify a game:

Perfect information (all visible)

Imperfect information (something hidden)

Deterministic (no luck)

Chess, dama (checkers), tic-tac-toe, Go

Battleship, Stratego

Chance (dice, shuffled cards)

Ludo, backgammon

Rang, poker, bridge, Scrabble

Zero-sum: one player's gain is exactly the other's loss (win +1, loss -1, draw 0). This lecture covers deterministic, perfect-information, two-player, zero-sum games.

§1.2 A game as a search problem

Slide L17-S07

Part

Meaning

Tic-tac-toe

S0

Initial state

Empty board

PLAYER(s)

Whose turn

X if counts are equal, else O

ACTIONS(s)

Legal moves

Any empty square

RESULT(s, a)

State after move a

Board with the new mark

TERMINAL-TEST(s)

Is the game over?

Three in a row or board full

UTILITY(s)

Final score for MAX

+1 / -1 / 0

MAX (us, X) wants a high utility; MIN (the opponent, O) wants a low one. A strategy tells MAX what to do for every possible reply of MIN - it is not one path as in the 8-puzzle.

§1.3 The game tree

Slide L17-S08

Root = the current position; children = positions after each legal move. One level = one ply (one move by one player).

Empty board: minimax value 0 (0 = draw with perfect play); nodes visited 549,946

Complete games (terminal leaves): 255,168

2. The Minimax Algorithm

§2.1 Backing up values

Slide L17-S10, S11

Work from the leaves upward. A MIN node takes the SMALLEST child value (the opponent's best reply); a MAX node takes the LARGEST. Worked example (MIN nodes B, C, D under the MAX root A):

MIN B: min(3, 12, 8) = 3

MIN C: min(2, 4, 6) = 2

MIN D: min(14, 5, 2) = 2

MAX A: max(3, 2, 2) = 3

Best move: B

Nodes visited: 13

(1 root + 3 MIN + 9 leaves)

Key idea: do not head for the biggest leaf (14 under D) - MIN would answer with 2. MAX plays B and is guaranteed at least 3 whatever MIN does; a mistake by MIN can only help MAX.

§2.2 The algorithm

Slide L17-S12

def minimax(node, maximizing=True, counter=None, depth=0, trace=None):

"""Plain minimax on a nested-list tree. counter[0] counts nodes visited; trace collects (depth, value, kind)."""

if counter is not None: counter[0] += 1

if not isinstance(node, list):

return node

vals = [minimax(c, not maximizing, counter, depth + 1, trace) for c in node]

v = max(vals) if maximizing else min(vals)

if trace is not None: trace.append((depth, "MAX" if maximizing else "MIN", vals, v))

return v

Minimax is a depth-first search: go down to the terminal states, score them with UTILITY, then back the values up. At the root, MAX plays the move whose child has the highest value.

§2.3 The sticks game

Slide L17-S13

Take 1 or 2 sticks in turn; whoever takes the last stick wins. Minimax finds the rule by itself: with a multiple of 3 sticks the player to move loses, because the opponent can always leave a multiple of 3 again.

1 sticks: value +1 (MAX wins), nodes 2

2 sticks: value +1 (MAX wins), nodes 4

3 sticks: value -1 (MIN wins), nodes 7

4 sticks: value +1 (MAX wins), nodes 12

5 sticks: value +1 (MAX wins), nodes 20

6 sticks: value -1 (MIN wins), nodes 33

7 sticks: value +1 (MAX wins), nodes 54

8 sticks: value +1 (MAX wins), nodes 88

9 sticks: value -1 (MIN wins), nodes 143

§2.4 Tic-tac-toe

Slide L17-S14

Squares are numbered 0-8 left to right, top to bottom. X to move:

X O .

X . O

. . .

X plays square 2: value +0

X plays square 4: value +1

X plays square 6: value +1

X plays square 7: value +0

X plays square 8: value +1

Square 6 wins immediately (column 0-3-6); squares 4 and 8 make a fork (two threats at once); squares 2 and 7 only draw.

3. When the Tree Is Too Big

§3.1 Properties of minimax

Slide L17-S16

Property

Minimax

Complete?

Yes, if the tree is finite

Optimal?

Yes, against an optimal opponent

Time

O(b^m) (b = moves per position, m = maximum depth)

Space

O(bm) - depth-first

Tic-tac-toe: at most 9! = 362,880 move sequences

Chess: b = 35, m = 80 -> 35^80 = 10^124 nodes

Go: b = 250, m = 150 -> 250^150 = 10^360 nodes

Chess at 1 billion nodes per second: about 10^107 years

Chess depth 2: 35^2 = 1,225 nodes

Chess depth 4: 35^4 = 1,500,625 nodes

Chess depth 6: 35^6 = 1,838,265,625 nodes

Chess depth 8: 35^8 = 2,251,875,390,625 nodes

§3.2 Depth limit and evaluation function

Slide L17-S17

Real programs stop at a fixed depth (cut-off) and replace UTILITY by an evaluation function: a fast estimate of how good a position is for MAX - a heuristic for games. Tic-tac-toe: eval = open lines for X - open lines for O (an open line has no opponent mark on it). Chess: material (pawn 1, knight 3, bishop 3, rook 5, queen 9) plus king safety and mobility.

def open_lines(b, p):

"""Lines that player p could still complete (no opponent mark on them)."""

other = "O" if p == "X" else "X"

return sum(1 for ln in LINES if all(b[i] != other for i in ln))

def evaluate(b):

"""Evaluation function for depth-limited search: open lines for X minus open lines for O (wins = +/-100)."""

w = winner(b)

if w == "X": return 100

if w == "O": return -100

return open_lines(b, "X") - open_lines(b, "O")

def depth_limited(b, depth, counter=None):

if counter is not None: counter[0] += 1

if terminal(b) or depth == 0:

return evaluate(b)

vals = [depth_limited(result(b, a), depth - 1, counter) for a in actions(b)]

return max(vals) if player(b) == "X" else min(vals)

X in centre (square 4): eval = 4; depth-2 value = 1

X in corner (square 0): eval = 3; depth-2 value = -1

X in edge (square 1): eval = 2; depth-2 value = -2

Example board:

X . .

. O .

. . .

open lines: X 4, O 5 -> eval = -1

Nodes from the empty board: depth 2 -> 82, full depth -> 549,946

Horizon effect: a disaster just beyond the depth limit (e.g. losing the queen on ply 7 when searching 6) cannot be seen. Programs search 'noisy' positions such as captures a little deeper.

4. Common Mistakes

Wrong idea

Correct idea

MAX should head for the biggest leaf.

MIN chooses too; plan for MIN's best reply.

Back up the average.

MAX = largest child, MIN = smallest child.

A MIN node is our move.

MIN is the opponent; levels alternate.

Minimax searches the whole chess tree.

Too big: depth limit + evaluation function.

The evaluation is the true value.

It is an estimate; the horizon effect can fool it.

Word Bank

Word

Simple meaning

Example

Adversary

Opponent who wants you to lose

The other chess player

Zero-sum

My gain = your loss

Win +1 / loss -1

Utility (pay-off)

Final score of a game for a player

+1, 0, -1

Ply

One move by one player

4 plies = 2 moves each

Back up

Pass values from children to the parent

min(3, 12, 8) = 3

Evaluation function

Quick estimate of a position

Chess material count

Horizon effect

Missing an event just past the depth limit

Queen lost on ply 7

Self-Check Questions

  1. Classify Ludo, chess and Rang. (see §1.1)
  2. Name the six parts of a game definition. (see §1.2)
  3. Back up the values: B [5, 9], C [7, 3, 8], D [6, 6]. (see §2.1)
  4. Who wins the sticks game with 7 sticks? Why? (see §2.3)
  5. What are the time and space costs of minimax? (see §3.1)
  6. Evaluate X in the centre, O in a corner. (see §3.2)

Reading: Russell & Norvig (4e) 5.1-5.2 (games, minimax), 5.4 (evaluation functions, cut-off, horizon effect).

← All CS-511 material