【问题标题】:incorrect output from minimax algorithm for Tic Tac Toe井字游戏的极小极大算法输出不正确
【发布时间】:2019-04-19 13:24:19
【问题描述】:

代码执行没有错误,但是minimax算法的输出不正确,请看一下,` 从主循环调用 AI_makemove 函数,board_state 是实际板的副本。 函数 AI_makemove 应该返回计算机对用户的最佳移动,board_state 是棋盘的当前状态,depth 是棋盘中填充的位置数,如果状态是获胜状态,则 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]

【问题讨论】:

  • 当前:IndentationError - 除此之外:不是minimal reproducible example
  • 您能否详细说明您的代码“不起作用”的原因?你期待什么,实际发生了什么?如果您遇到异常/错误,请按照如何创建minimal reproducible example 页面发布它发生的行和异常/错误详细信息。请edit您的问题将这些详细信息添加到其中,否则我们可能无法提供帮助。
  • @PatrickArtner 算法没有为电脑玩家生成最佳移动,没有错误。我已经更新了问题
  • 在许多地方使用print() 来查看变量中的值 - 并将其与纸上计算中的预期值进行比较。也许这样你可以在你的算法中发现错误。

标签: python-3.x pygame artificial-intelligence tic-tac-toe minimax


【解决方案1】:

我尝试了几次调试代码,但无法修复它,所以我用不同的方法再次编写了代码,它成功了。 Here是代码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    相关资源
    最近更新 更多