Skip to content

Instantly share code, notes, and snippets.

@BWoodfork
Created August 6, 2014 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BWoodfork/0f0518bd5e1340680c8f to your computer and use it in GitHub Desktop.
Save BWoodfork/0f0518bd5e1340680c8f to your computer and use it in GitHub Desktop.
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