0

I have a Tic Tac Toe game which works perfectly, but is there a way that I could alter the MiniMax algorithm that I have. So it's simpler in a way or even shorted.

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
mkj
  • 2,761
  • 5
  • 24
  • 28

2 Answers2

1

If you are looking to optimize / speed up Min Max Take a look at Alpha Beta Pruning - Same algo but with optimal shortcuts

  • am happy with the speed of the searching, but i feel that the code is abit complicted and just wondered if there is a way that i could simplify this in any way. I havent learned much regarding Alpha Beta Priuning. Thanks – user3535340 Apr 15 '14 at 11:40
1

I think someone may have done this for you already...see wikipedia about Negamax, "a variant form of minimax search that relies on the zero-sum property of a two-player game."

From 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
גלעד ברקן
  • 23,602
  • 3
  • 25
  • 61