使用alphabeta TicTacToe找到最佳动作

时间:2016-12-12 23:08:40

标签: python python-3.x tic-tac-toe alpha-beta-pruning

试图找到最佳动作以及得分。我已经让我的程序正确地返回游戏的分数,但我希望它也能返回移动。如何更改我的代码以便它执行此操作? 与thisthis类似。查看我失败的代码here,如果游戏结束,则返回None应该是移动代码。

def alphabeta(game_state, alpha, beta, our_turn=True):
    if game_state.is_gameover():
         return game_state.score()
    if our_turn:
        score = -9999
        for move in game_state.get_possible_moves():
            child = game_state.get_next_state(move, True)
            temp_max = alphabeta(child, alpha, beta, False) 
            if temp_max > score:
                score = temp_max
            alpha = max(alpha, score)
            if beta <= alpha:
                break
        return score
    else:
        score = 9999
        for move in game_state.get_possible_moves():
            child = game_state.get_next_state(move, False)
            temp_min = alphabeta(child, alpha, beta, True)
            if temp_min < score:
                score = temp_min
            beta = min(beta, score)
            if beta <= alpha:
                break
        return score

1 个答案:

答案 0 :(得分:2)

到目前为止,您可以跟踪最佳动作,例如:

    if game_state.is_gameover():
         return game_state.score(), None
    if our_turn:
        score = -9999
        for move in game_state.get_possible_moves():
            child = game_state.get_next_state(move, True)
            temp_max, _ = alphabeta(child, alpha, beta, False) # _ to disregard the returned move
            if temp_max > score:
                score = temp_max
                best_move = move
            alpha = max(alpha, score)
            if beta <= alpha:
                break
        return score, best_move

和其他情况相似