我的minimax算法tic-tac-toe-AI的代码似乎不起作用,我不知道为什么如果一个移动导致了损失,它似乎是在重复动作方面有问题,并且返回一个负值;它没有区分防守移动和进攻移动。
与其选择将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))

最佳答案

我发现你的代码有两个问题如果修复它们,在这种情况下至少会得到6
第一个问题是newBoard = board行实际上不是在复制列表,它只是在复制引用。可以通过将其更改为newBoard = board[:]来修复。
第二个问题是bestScore的起始值实际上并没有超出预期范围,所以每次都不会得到bestIndex的值。我把bestMove, bestScore = 0, 0改成bestMove, bestScore = 0, -11似乎对我有用。

关于python - Minimax算法井字游戏无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50192878/

10-12 18:35