Created
August 26, 2016 02:01
-
-
Save alexdovzhanyn/a5b7130a8fed1ac09f8e538f705150c1 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
puts "Min?"; @minimum = gets.chomp.to_i; @base = @minimum | |
puts "Max?"; @maximum = gets.chomp.to_i; @guesses = 0 | |
def search(min, max) | |
guess = min + (max - min) / 2 | |
puts guess | |
answer(min, max, guess) | |
end | |
def answer(min, max, guess) | |
ans = gets.chomp | |
if ans == "yes" | |
@guesses += 1 | |
puts "Nice! Took #{@guesses} guesses!" | |
elsif ans == "tl" | |
min = guess | |
@guesses += 1 | |
search(min, max) | |
elsif ans == "th" | |
max = guess | |
@guesses += 1 | |
search(min, max) | |
end | |
end | |
search(@minimum, @maximum) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment