【问题标题】:find best move using alphabeta TicTacToe使用Alphabeta井字游戏找到最佳移动
【发布时间】:2016-12-12 23:08:39
【问题描述】:

试图找到最佳着法以及得分。我已经让我的程序正确地返回游戏的分数,但我希望它也能返回移动。如何更改我的代码以使其执行此操作? 类似于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

【问题讨论】:

  • 天哪,你是 15 年前的我。开发一款无与伦比的井字游戏是我进入编程领域的入门药物。我似乎记得一个奇妙的树,其中包含我从未完全开始工作的 if..then 语句。这是我了解可读代码重要性的第一课。编辑:哦等等,alpha-beta-pruning?没关系,你比我早了很多。
  • 大声笑! 2年前入的,明年高中! :)

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


【解决方案1】:

您可以跟踪迄今为止的最佳移动,例如:

    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

其他情况类似

【讨论】:

  • 是的,但是当我想返回分数时,best_move 对如果game_state.is_gameover(),它还没有定义。
  • 定义为None
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-06
  • 1970-01-01
  • 1970-01-01
  • 2016-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多