【问题标题】:Minimax algorithim tic tac toe not workingMinimax 算法 tic tac toe 不起作用
【发布时间】:2018-10-16 00:01:26
【问题描述】:

我的 minimax 算法 tic tac toe AI 的代码似乎无法正常工作,我不知道为什么。如果移动导致损失,recursion 方面似乎有问题并返回负值;它不区分防御性动作和进攻性动作。

它没有选择将 X 放置在位置 6 以阻止对手连续达到 3,而是将其放置在另一个图块上

board = [
        "X", "X", "O",
        "O", "O", "X",
        "-", "-", "-",
        ]

opp = "O"
plyr = "X"

def getOpenPos(board):
    openPos = []
    for index, state in enumerate(board):
        if state == "-":
            openPos.append(index)
    return openPos

def winning(board, plyr):
    if ((board[0] == plyr and board[1] == plyr and board[2] == plyr) or 
        (board[3] == plyr and board[4] == plyr and board[5] == plyr) or 
        (board[6] == plyr and board[7] == plyr and board[8] == plyr) or
        (board[0] == plyr and board[4] == plyr and board[8] == plyr) or
        (board[1] == plyr and board[4] == plyr and board[7] == plyr) or
        (board[2] == plyr and board[4] == plyr and board[6] == plyr) or
        (board[0] == plyr and board[3] == plyr and board[6] == plyr) or
        (board[2] == plyr and board[5] == plyr and board[8] == plyr)):
        return True
    else:
        return False 

def minimax(board, turn, FIRST):
    possibleMoves = getOpenPos(board)
    #check if won
    if (winning(board, opp)):
        return -10
    elif (winning(board, plyr)):
        return 10

    scores = []

    #new board created for recursion, and whoevers turn it is
    for move in possibleMoves:
        newBoard = board
        newBoard[move] = turn


        if (turn == plyr):
            scores.append( [move,minimax(newBoard, opp, False)] )
        elif (turn == opp):
            scores.append( [move, minimax(newBoard, plyr, False)] )

    #collapse recursion by merging all scores to find optimal position
    #see if there is a negative value (loss) and if there is its a -10
    if not FIRST:
        bestScore = 0
        for possibleScore in scores:
            move = possibleScore[0]
            score = possibleScore[1]
            if score == -10:
                return -10
            else:
                if score > bestScore:
                    bestScore = score
        return bestScore

    else:
        bestMove, bestScore = 0, 0
        for possibleScore in scores:
            move = possibleScore[0]
            score = possibleScore[1]
            if score > bestScore:
                bestMove = move
                bestScore = score

        #returns best position
        return bestMove



print(minimax(board, plyr, True))

【问题讨论】:

    标签: python algorithm artificial-intelligence minimax


    【解决方案1】:

    我发现您的代码存在两个问题。如果你修复它们,在这种情况下你至少会得到6

    第一个问题是newBoard = board 行实际上并没有制作列表的副本,它只是制作了引用的副本。这可以通过将其更改为 newBoard = board[:] 来解决。

    第二个问题是bestScore 的起始值实际上并未超出预期范围,因此您不会每次都得到bestIndex 的值。我将bestMove, bestScore = 0, 0 更改为bestMove, bestScore = 0, -11,它似乎对我有用。

    【讨论】:

      猜你喜欢
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多