Last active
February 8, 2017 13:46
-
-
Save pungrue26/a46e5f7ecdf1cfc0e1683cf4252d277e to your computer and use it in GitHub Desktop.
minimax implementation
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
def custom_score(game, player): | |
"""Calculate the heuristic value of a game state from the point of view | |
of the given player. | |
Parameters | |
---------- | |
game : `isolation.Board` | |
An instance of `isolation.Board` encoding the current state of the | |
game (e.g., player locations and blocked cells). | |
player : object | |
A player instance in the current game (i.e., an object corresponding to | |
one of the player objects `game.__player_1__` or `game.__player_2__`.) | |
Returns | |
---------- | |
float | |
The heuristic value of the current game state to the specified player. | |
""" | |
if game.is_loser(player): | |
return float("-inf") | |
if game.is_winner(player): | |
return float("inf") | |
own_moves = len(game.get_legal_moves(player)) | |
opp_moves = len(game.get_legal_moves(game.get_opponent(player))) | |
return float(own_moves - opp_moves) | |
def minimax(self, game, depth, maximizing_player=True): | |
"""Implement the minimax search algorithm as described in the lectures. | |
Parameters | |
---------- | |
game : isolation.Board | |
An instance of the Isolation game `Board` class representing the | |
current game state | |
depth : int | |
Depth is an integer representing the maximum number of plies to | |
search in the game tree before aborting | |
maximizing_player : bool | |
Flag indicating whether the current search depth corresponds to a | |
maximizing layer (True) or a minimizing layer (False) | |
Returns | |
---------- | |
float | |
The score for the current search branch | |
tuple(int, int) | |
The best move for the current branch; (-1, -1) for no legal moves | |
""" | |
if self.time_left() < self.TIMER_THRESHOLD: | |
raise Timeout() | |
print ("minimax in! ", maximizing_player, depth) | |
print(game.print_board()) | |
player = game.__player_1__ if maximizing_player == True else game.__player_2__ | |
if depth == 0: | |
score = custom_score(game, game.get_opponent(player)) | |
return_tuple = (-1, -1) if (maximizing_player == True and score == float("-inf")) or (maximizing_player == False and score == float("inf")) else game.get_player_location(game.get_opponent(player)) | |
print ("minimax return 0 :") | |
print (score, return_tuple) | |
return score, return_tuple | |
return_score = float("-inf") if maximizing_player == True else float("inf") | |
return_tuple = (-1, -1) | |
for move in game.get_legal_moves(player): | |
new_board = game.forecast_move(move) | |
tmp_value, tmp_tuple = self.minimax(new_board, depth - 1, not maximizing_player) | |
print (tmp_value, tmp_tuple) | |
if maximizing_player == True and tmp_value > return_score: | |
return_score = tmp_value | |
return_tuple = tmp_tuple | |
elif maximizing_player == False and tmp_value < return_score: | |
return_score = tmp_value | |
return_tuple = tmp_tuple | |
print ("minimax return 1:") | |
print (return_score, return_tuple) | |
return return_score, return_tuple |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment