A better way to implement a “MiniMax” recursion [closed]
-
21-12-2019 - |
Question
I'm using the MinMax algorithm for a game, and since there are a lot possibilities the MinMax recursion takes way too long, even with "alpha-beta pruning"
My code looks some what like this:
min(state,depth,alpha,beta):
if stopingCond:
return value
for moves in allmoves:
state.do(move)
beta = min(beta, max(state,depth,alpha,beta) )
if alpha >= beta: return beta
return beta
max(state,depth,alpha,beta):
if stopingCond:
return value
for moves in allmoves:
state.do(move)
alpha = max(beta, min(state,depth,alpha,beta) )
if alpha >= beta: return alpha
return beta
I know that sometimes you can use for
loops instead of recursion
but I couldn't find a way to convert it.
If any one has a good idea I would be glad to hear it!
Thanks,
Solution
Generally the recursion in minmax can't be replaced by an equivalent iterative algorithm, and that's why there are optimization methods, like beta pruning, that try to improve performances by stopping some branches of the recursion tree as soon as possible.
It is possible that for your game, minmax (or, generally, a recursive algorithm) is not needed altogether and there could be other ways to find the optimal solution using other techniques, but without a precise set of rules is impossible to tell.