Skip to content

Instantly share code, notes, and snippets.

@MatteoPierro
Created November 11, 2022 23:09
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 MatteoPierro/ce653c775afd405e34fab3be460e6838 to your computer and use it in GitHub Desktop.
Save MatteoPierro/ce653c775afd405e34fab3be460e6838 to your computer and use it in GitHub Desktop.
Guess Number Code For Beer
class GuessNumber
attr_accessor :min, :max
def initialize
@min = 1
@max = 1000
end
def guess
return (max + min) / 2
end
def lower
@max = guess - 1
end
def higher
@min = guess + 1
end
end
guesser = GuessNumber.new
response = ''
attempts = 0
while response != 'correct'
if attempts == 10
exit
end
puts guesser.guess
STDOUT.flush
response = STDIN.gets.strip
attempts += 1
if response == 'higher'
guesser.higher
elsif response == 'lower'
guesser.lower
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment