This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MinimaxPlayer(IsolationPlayer): | |
"""Game-playing agent that chooses a move using depth-limited minimax | |
search. You must finish and test this player to make sure it properly uses | |
minimax to return a good move before the search time limit expires. | |
""" | |
def get_move(self, game, time_left): | |
"""Search for the best move from the available legal moves and return a | |
result before the time limit expires. | |
************** YOU DO NOT NEED TO MODIFY THIS FUNCTION ************* | |
For fixed-depth search, this function simply wraps the call to the | |
minimax method, but this method provides a common interface for all | |
Isolation agents, and you will replace it in the AlphaBetaPlayer with | |
iterative deepening search. | |
Parameters | |
---------- | |
game : `isolation.Board` | |
An instance of `isolation.Board` encoding the current state of the | |
game (e.g., player locations and blocked cells). | |
time_left : callable | |
A function that returns the number of milliseconds left in the | |
current turn. Returning with any less than 0 ms remaining forfeits | |
the game. | |
Returns | |
------- | |
(int, int) | |
Board coordinates corresponding to a legal move; may return | |
(-1, -1) if there are no available legal moves. | |
""" | |
self.time_left = time_left | |
# Initialize the best move so that this function returns something | |
# in case the search fails due to timeout | |
best_move = (-1, -1) | |
try: | |
# The try/except block will automatically catch the exception | |
# raised when the timer is about to expire. | |
return self.minimax(game, self.search_depth) | |
except SearchTimeout: | |
pass # Handle any actions required after timeout as needed | |
# Return the best move from the last completed search iteration | |
return best_move | |
def terminal_test(self, game): | |
if self.time_left() < self.TIMER_THRESHOLD: | |
raise SearchTimeout() | |
return not bool(game.get_legal_moves()) | |
def max_value(self, game, depth): | |
if self.time_left() < self.TIMER_THRESHOLD: | |
raise SearchTimeout() | |
if depth >= self.search_depth: | |
return self.score(game, self) | |
depth += 1 | |
if self.terminal_test(game): | |
return -1 | |
v = float("-inf") | |
for m in game.get_legal_moves(): | |
v = max(v, self.min_value(game.forecast_move(m), depth)) | |
return v | |
def min_value(self, game, depth): | |
if self.time_left() < self.TIMER_THRESHOLD: | |
raise SearchTimeout() | |
if depth >= self.search_depth: | |
return self.score(game, self) | |
depth += 1 | |
if self.terminal_test(game): | |
return 1 | |
v = float("inf") | |
for m in game.get_legal_moves(): | |
v = min(v, self.max_value(game.forecast_move(m), depth)) | |
return v | |
def minimax(self, game, depth): | |
if self.time_left() < self.TIMER_THRESHOLD: | |
raise SearchTimeout() | |
depth = 1 | |
best_score = float("-inf") | |
best_move = None | |
for m in game.get_legal_moves(): | |
v = self.min_value(game.forecast_move(m), depth) | |
if v > best_score: | |
best_score = v | |
best_move = m | |
return best_move |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment