Tic Tac Toe AI Bugs 请帮我修复它

我正在尝试创建一个永远不会输的井字游戏 AI。我的代码有时有效,但其他时候不起作用。有时它会找到最佳移动并正确打印出来,但有时它会失败。例如,一旦 AI 占据中心,如果您按 9 并占据右上角,那么 AI 将占据左上角。然后,如果您选择右下角,AI 将选择左侧中间的瓷砖,然后如果您选择右侧的中间瓷砖,那么您将获胜。我希望人工智能采取中间的权利来阻止你获胜。我是一名初学者程序员,所以如果代码没有以最佳方式编写,请原谅。这是为什么?另外,您能否详细说明您的解释,以便我了解出了什么问题?代码如下:

the_board = {'7': ' ','8': ' ','9': ' ','4': ' ','5': ' ','6': ' ','1': ' ','2': ' ','3': ' '}


def print_board(board):
    print(board['7'] + '|' + board['8'] + '|' + board['9'])
    print('-+-+-')
    print(board['4'] + '|' + board['5'] + '|' + board['6'])
    print('-+-+-')
    print(board['1'] + '|' + board['2'] + '|' + board['3'])


def check_for_win(current_state_of_the_board,player_sign):
    if current_state_of_the_board["7"] == current_state_of_the_board["8"] == current_state_of_the_board["9"] == player_sign:
        return "Victory"
    elif current_state_of_the_board["4"] == current_state_of_the_board["5"] == current_state_of_the_board["6"] == player_sign:
        return "Victory"
    elif current_state_of_the_board["1"] == current_state_of_the_board["2"] == current_state_of_the_board["3"] == player_sign:
        return "Victory"
    elif current_state_of_the_board["1"] == current_state_of_the_board["4"] == current_state_of_the_board["7"] == player_sign:
        return "Victory"
    elif current_state_of_the_board["2"] == current_state_of_the_board["5"] == current_state_of_the_board["8"] == player_sign:
        return "Victory"
    elif current_state_of_the_board["3"] == current_state_of_the_board["6"] == current_state_of_the_board["9"] == player_sign:
        return "Victory"
    elif current_state_of_the_board["7"] == current_state_of_the_board["5"] == current_state_of_the_board["3"] == player_sign:
        return "Victory"
    elif current_state_of_the_board["9"] == current_state_of_the_board["5"] == current_state_of_the_board["1"] == player_sign:
        return "Victory"
    else:
        return "No Victory"


def get_copy_of_the_board(current_board,move,player_sign):
    copy_of_the_board = current_board
    if copy_of_the_board[str(move)] == " ":
        copy_of_the_board[str(move)] = player_sign
        return copy_of_the_board
    else:
        return copy_of_the_board


def check_for_computer_win():
    for number in range(1,10):
        print( check_for_win(get_copy_of_the_board(the_board,number,"X"),"X"))
        if check_for_win(get_copy_of_the_board(the_board,"X") == "Victory":
            return str(number)
        else:
            return "Check next condition"


def check_for_player_win():
    for number in range(1,10):
        print(check_for_win(get_copy_of_the_board(the_board,"O"),"O"))
        if check_for_win(get_copy_of_the_board(the_board,"O") == "Victory":
            return str(number)
        else:
            return "Check next condition"


def check_for_computer_forks():
    for number in range(1,10):
        first_copy = get_copy_of_the_board(the_board,"X")
        for second_number in range(1,10):
            if check_for_win(get_copy_of_the_board(first_copy,second_number,"X") == "Victory":
                return str(number)
            else:
                return "Check next condition"


def check_for_player_forks():
    for number in range(1,"O")
        for second_number in range(1,"O") == "Victory":
                return str(number)
            else:
                return "Check next condition"


def get_computer_move():
    if check_for_computer_win() != "Check next condition":
        comp_move = check_for_computer_win()
        return comp_move
    else:
        if check_for_player_win() != "Check next condition":
            comp_move = check_for_player_win()
            return comp_move
        else:
            if check_for_computer_forks() != "Check next condition":
                comp_move = check_for_computer_forks()
                return comp_move
            else:
                if check_for_player_forks() != "Check next condition":
                    comp_move = check_for_player_forks()
                    return comp_move
                else:
                    for move in ["5"]:
                        if the_board[move] == " ":
                            comp_move = "5"
                            return comp_move
                    for move in ["7","9","1","3"]:
                        if the_board[str(move)] == " ":
                            return move

                    for move in ["4","8","6","2"]:
                        if the_board[str(move)] == " ":
                            return move


def game():
    count = 0
    turn = "computer"
    while count < 9:
        if turn == "computer":
            print("The computer move is: ")
            final_computer_move = get_computer_move()
            if the_board["1"] == "O":
                pass
            else:
                if final_computer_move == "1":
                    pass
                else:
                    the_board["1"] = " "
            the_board[final_computer_move] = "X"
            print_board(the_board)
            if check_for_win(the_board,"X") == "Victory":
                print("The computer has won!")
                exit()
            else:
                turn = "player"
                count += 1
        else:
            player_move = input("Where would you like to move?")
            the_board[player_move] = "O"
            print_board(the_board)
            if check_for_win(the_board,"O") == "Victory":
                print("You have won!")
                exit()
            else:
                turn = "computer"
                count += 1


game()

提前致谢!!

dezhaochao 回答:Tic Tac Toe AI Bugs 请帮我修复它

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/594.html

大家都在问