Created
November 11, 2022 23:09
-
-
Save MatteoPierro/ce653c775afd405e34fab3be460e6838 to your computer and use it in GitHub Desktop.
Guess Number Code For Beer
This file contains hidden or 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
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