Created
August 6, 2014 21:29
-
-
Save BWoodfork/0f0518bd5e1340680c8f to your computer and use it in GitHub Desktop.
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 minimax(fake_board, depth = 0, scores = {}) | |
return -1 if fake_board.game_over? | |
return 0 if fake_board.tie_game? | |
get_empty_spaces(fake_board).each do |space| | |
fake_board.fill_space(space, fake_board.token_that_is_up) | |
scores[space] = -1 * minimax(fake_board, depth + 1, {}) | |
fake_board.spaces[space] = nil | |
end | |
best_space = scores.each {|key, value| return key if value == scores.values.max } | |
best_scored_space = scores.each {|key, value| return key if value == scores.values.min } | |
if depth == 0 | |
return best_space | |
elsif depth > 0 | |
return best_scored_space | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment