The following pseudo-code illustrates the Minimax algorithm for returning the optimal move in a 2-player zero sum game.
//This top-level function computes the best action for MAX to take
MINIMAX(state)
(best_value, best_action) ← MAX-VALUE(state)
return best_action
end
MAX-VALUE(state)
if TERMINAL-TEST(state)
return UTILITY(state)
end
value ← NEGATIVE-INFINITY
action ← null
for each a in ACTIONS(state)
// Compute the highest possible utility (for MAX) if MAX were to take this action
u ← MIN-VALUE(RESULT(state, a))
if u > value
value ← u
action ← a
end
return (value, action)
end
MIN-VALUE(state)
if TERMINAL-TEST(state)
return UTILITY(state)
end
value ← POSITIVE-INFINITY
action ← null
for each a in ACTIONS(state)
// Compute the lowest possible utility (for MAX) if MIN were to take this action
u ← MAX-VALUE(RESULT(state, a))
if u < value
value ← u
action ← a
end
return (value, action)
end