python alpha beta_Python AlphaBeta Purning for Minimax极大

该篇博客讲述了作者尝试为3-Man's Morris游戏编写一个使用Minimax策略和Alpha-Beta剪枝的代理,但遇到错误解决方案。文章详细探讨了代码实现过程中的问题,并提供了可能的错误排查和优化建议。
摘要由CSDN通过智能技术生成

我想为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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值