Skip to content

Instantly share code, notes, and snippets.

@tarynsauer
Created November 24, 2013 00:58
Show Gist options
  • Save tarynsauer/7622033 to your computer and use it in GitHub Desktop.
Save tarynsauer/7622033 to your computer and use it in GitHub Desktop.
def minimax_score(board, player, cell, depth)
board.add_marker(cell, player.marker)
if board.game_over?
best_score = get_score(board, player)
elsif player.turn == 1
best_score = 999
board.get_free_positions.each_key do |cell1|
board.add_marker(cell1, player.opponent.marker)
score = minimax_score(board, player.opponent, cell1, depth += 1)
score = (score/depth.to_f)
if score < best_score
best_score = score
end
board.remove_marker(cell1)
end
else ### There is repetition between this and the previous branches.
best_score = -999
board.get_free_positions.each_key do |cell2|
board.add_marker(cell2, player.opponent.marker)
score = minimax_score(board, player.opponent, cell2, depth += 1)
score = (score/depth.to_f)
if score > best_score
best_score = score
end
board.remove_marker(cell2)
end
end
board.remove_marker(cell)
return best_score
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment