简化Tic Tac Toe的MiniMax算法

时间:2014-04-15 09:52:16

标签: algorithm search minmax

我有一个完美的Tic Tac Toe游戏,但有一种方法可以改变我的MiniMax算法。所以它在某种程度上更简单甚至是短路。

def maximized_move(self,gameinstance):
    ''' Find maximized move'''    
    bestscore = None
    bestmove = None
    for m in gameinstance.get_free_positions():
        gameinstance.mark(self.marker,m)

        if gameinstance.is_gameover():
            score = self.get_score(gameinstance)
        else:
            move_position,score = self.minimized_move(gameinstance)

        gameinstance.revert_last_move()

        if bestscore == None or score > bestscore:
            bestscore = score
            bestmove = m
    return bestmove, bestscore
def minimized_move(self,gameinstance):
    ''' Find the minimized move'''
    bestscore = None
    bestmove = None
    for m in gameinstance.get_free_positions():
        gameinstance.mark(self.opponentmarker,m)

        if gameinstance.is_gameover():
            score = self.get_score(gameinstance)
        else:
            move_position,score = self.maximized_move(gameinstance)

        gameinstance.revert_last_move()

        if bestscore == None or score < bestscore:
            bestscore = score
            bestmove = m
    return bestmove, bestscore

2 个答案:

答案 0 :(得分:1)

如果您正在寻求优化/加速Min Max看看Alpha Beta修剪 - 相同的算法,但有最佳的快捷方式

答案 1 :(得分:1)

我认为有人可能已经为你做过这样的事情......请参阅维基百科关于Negamax,&#34;一种依赖于双人游戏的零和属性的极小极大搜索的变体形式。 &#34;

来自frayn

SEARCHING_FUNCTION {
  Decrease depth by 1
  Loop through all moves
    Play move
    if ( depth = 0 ) move_score = static_position_score
    else move_score = - Opponent's_best_move_score    
    if ( move_score > best_move_score ) then ( best_move = move )        
    Undo Move     
  End of Loop
  Return best_move_score
} END