Created
May 18, 2012 16:07
-
-
Save mikeebert/2726071 to your computer and use it in GitHub Desktop.
Refactored Minimax
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 set_min_and_max_players(board) | |
@max = MaxPlayer.new(board.next_player_symbol, -50) | |
@min = MinPlayer.new(board.opponent_symbol, 50) | |
end | |
def minimax_score(board) | |
score = game_value(board) | |
return score unless score == -1 #-1 means a game is still in progress | |
player = set_player(board.next_player_symbol) | |
best_score = player.starting_score | |
board.available_spaces.each do |space| | |
test_board = copy(board) | |
test_board.place_move(player.symbol, space) | |
new_score = minimax_score(test_board) | |
best_score = player.compare(best_score, new_score) | |
end | |
return best_score | |
end | |
def set_player(symbol) | |
symbol == @max.symbol ? @max : @min | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment