我有一个完美运行的井字游戏,但是有没有办法可以改变我拥有的 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