Advertisement
Guest User

strategy_minimax

a guest
Mar 8th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. from strategy import Strategy
  2.  
  3.  
  4. class StrategyMinimax(Strategy):
  5. ''' Interface to suggest the best possible moves.
  6. '''
  7.  
  8. def suggest_move(self, state):
  9. ''' (GameState) -> Move
  10.  
  11. Return the best possible move available in state.
  12.  
  13. Overrides Strategy.suggest_move
  14. '''
  15.  
  16. move_dict = gen_move_dict(state)
  17. possible_moves = state.possible_next_moves()
  18. if 1.0 in move_dict.values():
  19. for move_key in move_dict:
  20. if move_dict[move_key] == 1.0:
  21. move = possible_moves[move_key]
  22. return move
  23. elif 0.0 in move_dict.values():
  24. for move_key in move_dict:
  25. if move_dict[move_key] == 0.0:
  26. move = possible_moves[move_key]
  27. return move
  28. move = possible_moves[move_dict.popitem()[0]]
  29. return move
  30.  
  31.  
  32. def score(state):
  33. ''' (GameState) -> tup of (float, float)
  34.  
  35. Precondition: state.over
  36.  
  37. Return a two item tuple representing the score of next_player and
  38. state.opponent() respectively.
  39. '''
  40.  
  41. outcome = state.outcome()
  42. next_player = state.next_player
  43. outcome_tup = (outcome, outcome * -1.0) if next_player == 'p1' else\
  44. (outcome * -1.0, outcome)
  45. return outcome_tup
  46.  
  47.  
  48. def gen_move_dict(state):
  49. ''' (GameState, str) -> dict of {obj: float}
  50.  
  51. Return a dictionary with the indices of each move in
  52. state.possible_next_moves() as keys and the score of next_player
  53. after applying the corresponding move.
  54.  
  55. '''
  56.  
  57. possible_moves = state.possible_next_moves()
  58. player_int = 0 if state.next_player == 'p1' else 1
  59. if not possible_moves:
  60. return {None: score(state)[player_int]}
  61. else:
  62. move_dict = {}
  63. possible_move_length = len(possible_moves)
  64. for move_index in range(possible_move_length):
  65. applied_state = state.apply_move(possible_moves[move_index])
  66. temp_dict = gen_move_dict(applied_state)
  67. scores = temp_dict.values()
  68.  
  69. # max(scores) represents the score of the opponent after
  70. # next_player plays a move. So -max(scores) represents
  71. # the score of next_player before the move is played.
  72.  
  73. # Ternary if is to ensure that move_dict_value doesn't get set
  74. # to -0.0
  75. move_dict_value = -max(scores) if max(scores) != 0.0 else 0.0
  76. move_dict[move_index] = move_dict_value
  77. return move_dict
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement