-2

在这些天里,我一直在努力使用 minimax 算法,我可以说我终于明白了(感谢 stackoverflow 中的另一篇文章)。因此,我打开了我的编辑器,并尝试将其实现为极其简单的(请不要责怪我的代码:P)井字游戏,只是为了尝试一下。一切正常,但计算机移动功能总是检索我-1。我不是要你给我代码,只是要“为什么”这样做。我已经多次搜索代码,但一无所获。该代码可能与我在网上找到的另一个代码非常相似。这是我的代码:

# COMPUTER AI

def computer_move():
    best_move = minimax_recurse(game_board,active_player, 0)
    print "The best move is ", best_move
    make_move(game_board,best_move, active_player)


    print "COMPUTER MOVE DONE"


def minimax_recurse(game_board,player,depth):

    winner = is_winner(game_board)
    if winner == active_player  :
        return 1
    elif winner is not active_player :
        return -1
    elif len(get_move_list(game_board)) == 0 :
        return 0

    if player == player1 :
        other_player = player2
    other_player = player1

    if player == active_player :
        alpha = -1
    alpha = 1

    movelist = get_move_list(game_board)

    for move in movelist :
        board2 = game_board

        make_move(board2,move,player)

        subalpha = minimax_recurse(board2, other_player, depth + 1)

        if player == active_player :
            if depth == 0 and alpha <= subalpha:
                best_move = move

            alpha = max(alpha,subalpha)
            return alpha

        else :
            alpha = min(alpha,subalpha)
            return alpha



# BOARD FUNCTIONS
game_board = ([1,2,3],[4,5,6],[7,8,9])
def print_board(board) :

    for row in board :
        print row

def make_move(game_board,player_move,active_player):

    x = 0
    y = 0
    player_move = int(player_move)
    if player_move == 1 :
        x = 0
        y = 0
    elif player_move == 2 :
        x = 0
        y = 1
    elif player_move == 3 :
        x = 0
        y = 2
    elif player_move == 4 :
        x = 1
        y = 0
    elif player_move == 5 :
        x = 1
        y = 1
    elif player_move == 6 :
        x = 1
        y = 2
    elif player_move == 7 :
        x = 2
        y = 0
    elif player_move == 8 :
        x = 2
        y = 1
    elif player_move == 9 :
        x = 2
        y = 2
    elif player_move >= 10 :
        print "value is too high"
        skip = False
        return board

    if game_board[x][y] == "O" or game_board[x][y] == "X" :
        print "move not avaiable"
        return game_board

    game_board[x][y] = active_player
    return game_board

def is_winner(board):
    for i in range (0,3) :
        if board[i][0] == player1 and board[i][1] == player1 and board[i][2] == player1 :
            return player1

        if board[i][0] == player2 and board[i][1] == player2 and board[i][2] == player2 :
            return player2

    # checking for obliqual, that's quite bad and slow check but it works
    if board[0][0] == player1 and board[1][1] == player1 and board[2][2] == player1 :
        return player1
    if board[0][0] == player2 and board[1][1] == player2 and board[2][2] == player2 :
        return player2

    if board[2][0] == player1 and board[1][1] == player1 and board[0][2] == player1 :
        return player1
    if board[2][0] == player2 and board[1][1] == player2 and board[0][2] == player2 :
        return player2

    return None


def get_move_list (game_board) :

    move = [0]

    for row in game_board :
        for i in row :
            if isinstance(i,int) == True :
                move.append(i)
    move.remove(0)
    return move



# Main Loop
player1 = "X"
player2 = "O"
print_board(game_board)
while True :
    active_player = player1
    # this is for player move
    print get_move_list(game_board)
    player_move = int(raw_input("Please insert your move >>> "))
    make_move(game_board,player_move,active_player)
    print_board(game_board)

    if is_winner(game_board) == player1 :
        print "Player1 is the winner"
        break
    if is_winner(game_board) == player2 :
        print "Player2 is the winner"
        break
    print get_move_list(game_board)
    # computer time
    active_player = player2
    computer_move()
    print_board(game_board)

    if is_winner(game_board) == player1 :
        print "Player1 is the winner"
        break
    if is_winner(game_board) == player2 :
        print "Player2 is the winner"
        break
4

2 回答 2

2

在不调试所有代码的情况下,似乎错误的一件事是您使用“best_move”变量(全局未初始化)作为移动的持有者以及 minimax_recurse 的 -1/0/+1 结果的持有者。所以它被你的 minmax 算法覆盖了。你需要更多的变量,更清晰的初始化,以及一致的使用。

于 2012-05-17T19:07:38.613 回答
1

-1 总是返回的根本原因是winner is not active_player返回Truewhen winneris None。您可以使用变量来跟踪其他(非活动)玩家,或者您可以使用三元运算符:elif winner is (player1 if player2 == active_player else player2)

虽然这不是唯一的问题:

if player == active_player :      
    alpha = -1    
alpha = 1 

这将始终将 alpha 设置为 1。直接在上面的行有同样的问题。另一个答案中指出的变量命名问题也是如此。

于 2012-05-17T19:18:51.960 回答