int Minimax(state, level, alpha, beta)

Minimax
Two Player Game Playing
• Use backtracking to implement computer version
of 2-player games: tic-tac-toe, chess, checkers,
connect four
• Minimax algorithm:
– Backtracking algorithm for choosing next move in an
2-player game
– Value associated with each position of the game
– The player wants to make the move that maximizes the
minimum value of the position resulting from the
opponent’s following move
Game Tree
X
X
X
X
X
X
X
X
X
Game Tree
X
X moves
X
O
O
X
X
X
X
O
X X
O
X
O
O
O
O
O X
O
O
O moves
O
X X
O moves
X moves
X
X
X
X O
X
X
X
X O
X
X
X
X X
O
X X
O
X
O
O X
O
O
O
O O
O
O X
O
O X
O
X O
X
X O
X
X X
X
X X
O
X X
O
O X
O
O X
O
O X
O
O X
O
Game Tree
X
X moves
X
X
O
O
O
1
X X
X
X
X
X
O
X X
O
X
O
O
O
O
O X
O
O moves
O
-1
1
X moves
X
0
X O
X
X
X
X O
X
X
X
X X
O
X X
O
X
O
O X
O
O
O
O O
O
O X
O
O X
O
0
O moves
X
0
-1
1
X O
X
X O
X
X X
X
X X
O
X X
O
O X
O
O X
O
O X
O
O X
O
0
0
1
Minimax Algorithm - Strategies
• Winning Strategy
– If the root has value 1, then player 1 has a
winning strategy
• Termination guarantee implies an eventual win for
player 1
– If the root has value -1, then player 2 has a
winning strategy
– If the root has value 0, then the best a player
can do is guarantee himself a draw
• Does tic-tac-toe have a winning strategy?
Payoff Functions
• The game tree can be generalize to trees
with payoff values
– Same minimax rules apply
• Chess
– Use payoff to estimate probability of a win
Minimax Algorithm
• Backtracking algorithm on the game tree
• Tells us the best move (and whether it’s a
winning strategy)
• Start with the board at some current position
– Explore all possibilities from this position
– Assign values {-1, 0, 1} after all children have
returned
Minimax Algorithm
X
X moves
X
X
O
O
O
1 = max(1, -1, 0)
X X
X
X
X
X
O
X X
O
X
O
O
O
O
O X
O
O moves
O
-1 = min(0, -1)
1
X moves
X
0 = min(0, 1)
X O
X
X
X
X O
X
X
X
X X
O
X X
O
X
O
O X
O
O
O
O O
O
O X
O
O X
O
0 = max(0)
O moves
X
0 = max(0)
-1
1 = max(1)
X O
X
X O
X
X X
X
X X
O
X X
O
O X
O
O X
O
O X
O
O X
O
0
0
1
Minimax Pseudocode
int Minimax(state, level) {
if(state is a solution) //base case
return state’s value
if(level is a max) {
while(state has more children)
Minimax(child, level+1)
if value returned is > best so far, store it
}
return maximum returned value
} else { //level is a min level
while(state has more children)
Minimax(child, level+1)
if value returned is < best so far, store it
}
return minimum returned value
}
}
Alpha-Beta Pruning
max
A
5
min
max
B
I
P
2
5
4
C
F
J
M
Q
T
2
6
8
5
4
6
D
E
G
H
K
L
N
O
R
S
U
V
2
1
6
1
8
3
5
2
3
4
2
6
Alpha-Beta Pruning
• Rules for pruning:
– At a max node q:
• If p is a min node with parent q and p & q have
“tentative” values v1 and v2 such that v1  v2, then
you can prune all the unconsidered children of p.
– At a min node q:
• If p is a max node with parent q and p & q have
“tentative” values v1 and v2 such that v1  v2, then
you can prune all the unconsidered children of p.
Minimax Algorithm
X
X moves
X
X
O
O
O
= 1-  = 
X X
X
X
X
X
O
X X
O
X
O
O
O
O
O X
O
O moves
O
= 10  = 
0
1
X moves
X
= 1 0 = 
0
X O
X
X
X
X O
X
X
X
X X
O
X X
O
X
O
O X
O
O
O
O O
O
O X
O
O X
O
= 10 = 
O moves
X
-1
= 1 0 = 
X O
X
X O
X
X X
X
X X
O
X X
O
O X
O
O X
O
O X
O
O X
O
0
0
1
Alpha-Beta Pseudocode
int Minimax(state, level, alpha, beta) {
if(state is a solution) //base case
return state’s value
if(level is a max) {
while(state has more children and alpha < beta)
Minimax(child, level+1, alpha, beta)
if value returned is > alpha, store it
}
return alpha
} else { //level is a min level
while(state has more children and alpha < beta)
Minimax(child, level+1, alpha, beta)
if value returned is < beta, store it
}
return beta
}
}