min = +infinity
for each move m
{
makeMove(board, m)
result = maximizer(board)
if (result < min)
min = result
retractMove(board, m)
}
return min
}
maximizer(board)
{
if noMoreMoves()
return eval(board)
max = -infinity
for each move m
{
makeMove(board, m)
result = minimizer(board)
if (result > max)
max = result
retractMove(board, m)
}
return max
}
As noted in class, the minimax routines sometimes do needless search. Suppose two pieces of information are available to the minimizer and the maximizer: best and worst. Let best stand for "the best that can be expected" and worst stand for "worst case possible". Note that the minimizer's best that can be expected is the maximizer's worst case possible and vice versa. Let's modify the routines so that they take advantage of this information.
for each move m
{
if worst <= best then break
makeMove(board, m)
result = maximizer(board, best, worst)
if (result < worst)
worst = result
retractMove(board, m)
}
return worst
}
maximizer(board, worst, best)
{
if noMoreMoves()
return eval(board)
for each move m
{
if worst >= best then break
makeMove(board, m)
result = minimizer(board, worst, best)
if (result > worst)
worst = result
retractMove(board, m)
}
return worst
}
These two routines are often combined into a single routine. Since best
for minimizer is worst for maximizer, and vice versa, the variables
alpha
and beta are used instead of best and worst to avoid
confusion. Let alpha and beta represent the range of possible
results. The minimizer is interested in improving its knowledge of the
worst case possible, which, for it, is beta since it represents
the top end of the range. The maximizer is interested in improving its
knowledge of its worst possible case which is represented by alpha.
The minimizer, therefore, updates beta while the maximizer updates
alpha.
for each move m
{
if alpha >= beta then break
makeMove(board, m)
result = alphaBeta(board, !mover, alpha, beta)
if mover is minimizer and result < beta
beta = result
else if mover is maximizer and result > alpha
alpha = result
retractMove(board, m)
}
if mover is minimizer
return beta
else
return alpha
}