The following pseudo-code illustrates the Minimax algorithm with alpha-beta pruning.
//This top-level function computes the best action for MAX to take
ALPHA-BETA(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), α, β)
//Determine if this action resulted in a higher utility
if u > value
value ← u
action ← a
end
//MIN is guaranteed a value of β elsewhere, so MIN would never allow the game to progress to this state
// Return back...no need to keep checking actions
if value ≥ β
return (value, action)
end
//Determine if this action resulted in the highest utility we've seen so far in the game tree
if value > α
α ← value
end
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), α, β)
//Determine if this action resulted in a lower utility
if u < value
value ← u
action ← a
end
//MAX is guaranteed a value of α elsewhere, so MAX would never allow the game to progress to this state
//Return back...no need to keep checking actions
if value ≤ α
return (value, action)
end
if value < β
β ← value
end
end
return (value, action)
end