Created
May 18, 2012 15:26
-
-
Save mikeebert/2725858 to your computer and use it in GitHub Desktop.
Unrefactored Minimax score and players
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) | |
if board.next_player == :player1 | |
@max_symbol = board.player1_symbol | |
@max_player = :player1 | |
@min_symbol = board.player2_symbol | |
@min_player = :player2 | |
elsif board.next_player == :player2 | |
@max_symbol = board.player2_symbol | |
@max_player = :player2 | |
@min_symbol = board.player1_symbol | |
@min_player = :player1 | |
end | |
end | |
def minimax_score(board) | |
score = game_value(board) | |
return score unless score == -1 | |
if board.next_player == @min_player | |
scoring_for_player = @min_player | |
new_score = 100 | |
best_score = 5 | |
else | |
scoring_for_player = @max_player | |
new_score = -100 | |
best_score = -5 | |
end | |
board.available_spaces.each do |space| | |
test_board = copy(board) | |
if test_board.next_player == @min_player | |
test_board.place_move(@min_symbol, space) | |
else | |
test_board.place_move(@max_symbol, space) | |
end | |
new_score = minimax_score(test_board) | |
best_score = compare_min(best_score,new_score) if scoring_for_player == @min_player | |
best_score = compare_max(best_score,new_score) if scoring_for_player == @max_player | |
end | |
return best_score | |
end | |
def compare_min(best_score,new_score) | |
new_score < best_score ? new_score : best_score | |
end | |
def compare_max(best_score,new_score) | |
new_score > best_score ? new_score : best_score | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment