
CS 61B: Lecture 18
Friday, October 8, 2010
Game tree search
How could we design a program that plays Tic Tac Toe? The stan-
dard technique searches for the best moves by using a game tree,
which looks much like a family tree. A game tree is not a data
structure; it is the structure of a sequence of recursive method calls.
The ancestor at the top of the tree is the current grid, accompa-
nied by a record of whose turn it is. The children of the ancestor
form the set of possible grids that result from considering each legal
move. These children have children of their own, which represent
each of the opponent’s possible countermoves.
0000000000000000000000000000
0000000000000000000000000000
0000000000000000000000000000
0000000000000000000000000000
0000000000000000000000000000
0000000000000000000000000000
0000000000000000000000000000
0000000000000000000000000000
0000000000000000000000000000
1111111111111111111111111111
1111111111111111111111111111
1111111111111111111111111111
1111111111111111111111111111
1111111111111111111111111111
1111111111111111111111111111
1111111111111111111111111111
1111111111111111111111111111
1111111111111111111111111111
Move
Human
Move
Computer
Move
Computer
1
1-1
1-1
1
MAX
MAX
MIN
-1
-1
1
1
If we assume that both opponents are infinitely intelligent and
will always choose the best move, then we can determine the com-
puter’s best move using the game tree. Each legal grid (coupled
with information about whose turn it is) is assigned a numerical
score that indicates how optimistic we are about winning. For con-
creteness, give a grid a score of 1 if the computer is guaranteed
a win (assuming it plays perfectly), a score of −1 if the com-
puter’s opponent is guaranteed a win, and a score of 0 if perfect
players will draw. (It’s better to give these constants names like
COMPUTER_WIN,HUMAN_WIN, and DRAW, but these names are
too long for the figures herein.)
How do we score a given grid? It’s easy if the game is over.
Suppose the computer is playing X. If there are three X’s in a row,
assign the grid a 1; for three collinear O’s, assign it a −1. If the
grid has no empty squares left, and nobody has won, assign it a 0.
The score of any other grid is computed by a minimax algo-
rithm. We consider each possible move, and determine the child
grid that each move creates. We assign a score to each child grid by
calling the minimax algorithm recursively. Then we assign a score
to the current (parent) grid. If it’s the computer’s turn, we choose
the move that yields the maximum score, and assign the same score
to the current grid. If it’s the opponent’s turn, we assume that the
opponent plays perfectly. So we choose the move that yields the
minimum score, and assign that score to the current grid.
Minimax is recursive, and the figure above illustrates the recur-
sive method calls that minimax executes when evaluating several
grids’ scores. Again, no tree data structure is created! At any one
point in time, the program stack represents one path down the tree
(but the root of the tree is at the bottom of the stack). Each grid’s
score is printed to its left. Because the top-level grid has a score of
one, the computer is guaranteed a win, which is obtained by choos-
ing the second of the three possible moves.
The following pseudocode computes a grid’s score and the best
move from that grid (which determines the grid’s score). A Best
object holds a record of the best move and its score.
public class Grid {
public Best chooseMove(boolean side) {
Best myBest = new Best(); // My best move
Best reply; // Opponent’s best reply
if (the current Grid is full or has a win) {
return a Best with Grid’s score, no move;
}
if (side == COMPUTER) {
myBest.score = -2;
} else {
myBest.score = 2;
}
for (each legal move m) {
perform move m; // Modifies "this" Grid
reply = chooseMove(! side);
undo move m; // Restores "this" Grid
if (((side == COMPUTER) &&
(reply.score > myBest.Score)) ||
((side == HUMAN) &&
(reply.score < myBest.Score))) {
myBest.move = m;
myBest.score = reply.score;
}
}
return myBest;
}
}
Why is myBest.score initially set to −2 or 2? By setting
the initial score out of range, we ensure that at least one legal move
will be assigned to myBest. It is a simple way, albeit not the most
transparent way, to make sure that chooseMove always accepts
the first move investigated.
Each grid in the game tree at left represents one invocation of
chooseMove. The children of each grid represent recursive calls,
which are executed in order from left to right. When any particular
grid is invoked, that grid and its ancestors (parent, grandparent,
etc.) are stack frames on the program stack.
Simple pruning
If, at any grid in the game tree, a player discovers a guaranteed
winning move, there’s no reason to continue to search for a better
move. Hence, we can save time by pruning away some recursive
calls. The previous example now looks like this.
0000000000000000000000000000
0000000000000000000000000000
0000000000000000000000000000
0000000000000000000000000000
0000000000000000000000000000
0000000000000000000000000000
0000000000000000000000000000
0000000000000000000000000000
1111111111111111111111111111
1111111111111111111111111111
1111111111111111111111111111
1111111111111111111111111111
1111111111111111111111111111
1111111111111111111111111111
1111111111111111111111111111
1111111111111111111111111111
Move
Human
Move
Computer
No need to
continue
No need to
continue
−1
1−1
1
MAX
MIN
1