⚡0 XP🔥0

Game Playing - Minimax and Alpha-Beta Pruning

Lesson 4 of 8 ⏱ 6 min read ✎ 1 exercises ❓ 4-question quiz ▶ Runs Python

How AI plays two-player games - game trees, the minimax algorithm, an unbeatable tic-tac-toe player in Python, alpha-beta pruning, evaluation functions and depth limits, and how AlphaGo-style systems differ.

Games were one of the first big challenges for AI: clear rules, a clear goal, and an opponent trying to beat you. The ideas - looking ahead, assuming the opponent plays well, and pruning hopeless options - apply to planning and negotiation too.

Game trees#

In a two-player, turn-taking, perfect-information game (tic-tac-toe, chess, Go):

  • MAX (our AI) wants the highest score; MIN (the opponent) wants the lowest.
  • Each node of the game tree is a position; each edge is a move.
  • Terminal positions get a utility: +1 win for MAX, -1 loss, 0 draw.

The minimax algorithm#

Work backwards from the leaves: at MAX nodes take the maximum of the children's values, at MIN nodes the minimum. The value at the root is the best MAX can guarantee against a perfect opponent.

                 MAX            → 3  (MAX picks the left move)
          /       |       \
        MIN      MIN      MIN   → 3, 2, 2
       / | \    / | \    / | \
      3 12  8  2  4  6  14  5  2
▶ Try it YourselfPython
Ctrl + Enter to run
Output
  MIN [3, 12, 8] -> 3
  MIN [2, 4, 6] -> 2
  MIN [14, 5, 2] -> 2
MAX [3, 2, 2] -> 3
value of the game: 3
हिंदी में समझें

Minimax में AI मान लेता है कि सामने वाला खिलाड़ी हमेशा सबसे अच्छी चाल चलेगा। इसलिए अपनी बारी (MAX) में सबसे बड़ा score चुनो, और विरोधी की बारी (MIN) में सबसे छोटा मान लो। नीचे से ऊपर ऐसे हिसाब लगाने पर root पर वह score मिलता है जो AI पक्का हासिल कर सकता है।

An unbeatable tic-tac-toe AI#

Tic-tac-toe has fewer than 6,000 distinct reachable positions, so minimax can search all the way to the end. Here the AI plays O and scores a position from O's point of view:

▶ Try it YourselfPython
Ctrl + Enter to run
Output
 X |   | O
---+---+---
   | X |  
---+---+---
   |   |  

O plays square 8 (expected result +0), after searching 932 positions

X threatens to complete the diagonal 0-4-8. Minimax discovers that every other move loses (score -1), so it blocks at square 8. Change the board (use "X", "O" or " ") and run again; you will never beat it.

Note

Searching the whole tree from the empty board visits about half a million positions in this simple program. That is fine for tic-tac-toe, impossible for chess (about 10^120 possible games).

Alpha-beta pruning#

Minimax explores every branch, but many cannot change the decision. Alpha-beta pruning keeps two numbers while searching:

  • α (alpha) - the best score MAX can already guarantee,
  • β (beta) - the best score MIN can already guarantee.

When α ≥ β at a node, the remaining children are pruned (skipped): the opponent would never let the game reach this position.

▶ Try it YourselfPython
Ctrl + Enter to run
Output
alpha-beta value: 3
nodes visited - minimax: 13  alpha-beta: 11

After the first branch MAX knows it can get 3. In the second branch, the first leaf is 2 - MIN can force ≤ 2, which is worse than 3 for MAX, so the rest of that branch is skipped. With good move ordering, alpha-beta can search roughly twice as deep as plain minimax in the same time.

Real games: depth limits and evaluation functions#

For chess or Go the tree is far too big to search to the end, so programs:

  1. search only a few moves ahead (a depth limit), and
  2. score the positions at that depth with an evaluation function - for chess, material (queen = 9, rook = 5...), king safety, piece mobility, pawn structure.
def evaluate(position):
    return 9 * (my_queens - their_queens) + 5 * (my_rooks - their_rooks) + 3 * (my_minor - their_minor) + ...
System Approach
Deep Blue (1997, chess) alpha-beta search on special hardware + hand-tuned evaluation
Stockfish (chess) alpha-beta + neural-network evaluation (NNUE)
AlphaGo / AlphaZero (2016-17) Monte Carlo Tree Search guided by neural networks learned through self-play (reinforcement learning)

Summary#

  • Two-player games form game trees; MAX maximises and MIN minimises the utility.
  • Minimax backs values up from the leaves; it plays perfectly when it can search to the end.
  • Alpha-beta pruning gives the same answer while skipping branches that cannot matter.
  • Big games need depth limits, evaluation functions, and - in modern systems - learned neural evaluations.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Minimax on a small game tree

A game tree is given as nested lists - leaves are scores for MAX. Write minimax(node, maximizing) and store the value of tree (MAX moves first) in value. Print it.

Quick Quiz

Q1.

In minimax, the MAX player chooses...

Q2.

What does alpha-beta pruning change?

Q3.

If both players play perfectly, tic-tac-toe ends in...

Q4.

Why do chess programs use a depth limit and an evaluation function?