代码执行中没有错误,但最小最大值算法的输出不正确,请看一下。AI_makemove函数从主循环调用,board_state是实际电路板的副本。函数AI_makemove应该返回计算机对用户的最佳移动,board_state是棋盘的当前状态,深度是棋盘中填充的位置数,check_if_won如果状态是当前玩家的获胜状态,则函数返回true。
def AI_makemove(board_state , isAI , depth):
temp_board = copy.deepcopy(board_state)
depth+=1
print(temp_board , depth , isAI)
if isAI:
bestVal = -9999
a = b = 0
for i in range(0,3):
for j in range(0,3):
if temp_board[i][j] == 0:
temp_board1 = copy.deepcopy(temp_board)
temp_board1[i][j] = 2
if check_if_won(2,temp_board1):
return [1 , i, j]
if depth == 9:
return [bestVal , a ,b]
l = AI_makemove(temp_board1,False,depth)
if int(l[0]) > bestVal:
bestVal = int(l[0])
a = int(l[1])
b = int(l[2])
else:
bestVal = +9999
a = b = 0
for i in range(0, 3):
for j in range(0, 3):
if temp_board[i][j] == 0:
temp_board1 = copy.deepcopy(temp_board)
temp_board1[i][j] = 1
if check_if_won(1,temp_board1):
return [-1 , i, j]
if depth == 9:
return [bestVal , a ,b]
l = AI_makemove(temp_board1,True,depth)
if int(l[0]) < bestVal:
bestVal = int(l[0])
a = int(l[1])
b = int(l[2])
return [bestVal , a ,b]
我尝试了几次调试代码,但无法修复它,所以我用不同的方法再次编写代码并且它起作用了。这是代码