python alpha beta_Python AlphaBeta Purning for Minimax极大

我想为3-Men's Morris游戏实现一个代理,它非常类似于tic-tac-toe游戏-我想使用Minimax策略和Alpha-Beta修剪,这是我在Python中基于StackOverflow上的this post和{a3}的代码,但是它不起作用!!它给出了一个错误的解决方案,即使当前状态的继承者之一是解决方案def alpha_beta(state,alpha,beta,turn,depth):

if int(terminal_test(state,turn)) == int(MY_NUMBER):

return 1 #win

elif (int(terminal_test(state,turn))!=0) and (int(terminal_test(state,turn))!=int(MY_NUMBER)) :

return -1 #loose

else:

if int(depth) == 13:

return 0 #reached limit

moves = successors(state,turn,int(depth))

#valid moves for player based on rules

for move in moves:

state = make_move(state,move,turn)

current_eval = -alpha_beta(state, -beta, -alpha, 2-int(turn),int(depth)+1)

state = undo_move(state,move,turn)

if current_eval >= beta:

return beta

if current_eval > alpha:

alpha = current_eval

return alpha

def rootAlphaBeta(state,depth, turn):

best_move = None

max_eval = float('-infinity')

moves = successors(state,turn,int(depth))

alpha = float('infinity')

for move in moves:

state = make_move(state,move,turn)

alpha = -alpha_beta(state, float('-infinity'), alpha, 2-int(turn),int(depth)+1)

state = undo_move(state,move,turn)

if alpha > max_eval:

max_eval = alpha

best_move = move

#best_move which is selected here is not really the best move!

return best_move

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值