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
class GuessTheNumber | |
def play | |
@nnn = 1.upto(100).to_a.sample | |
# play game | |
puts "Alright, I have a number in mind from 1-100. Pick a number." | |
guess = gets.chomp | |
puts "You said #{guess}." | |
attempt guess | |
end | |
def play_again? | |
response = "" | |
puts "Do you want to play again? [y/n]" | |
until response == "y" or response == "n" | |
response = gets.chomp | |
end | |
#response == "y" | |
response.eql? "y" | |
end | |
def attempt guess | |
guess = guess.to_i | |
if guess < @nnn | |
puts "#{guess} is too low. Try again:" | |
new_guess = gets.chomp | |
attempt new_guess | |
elsif guess > @nnn | |
puts "#{guess} is too high. Try again:" | |
new_guess = gets.chomp | |
attempt new_guess | |
elsif guess == @nnn | |
puts "Yes, the answer is #{@nnn}. Aren't you proud of yourself?" | |
end | |
end | |
end | |
# main | |
g = GuessTheNumber.new | |
keep_playing = true | |
while keep_playing | |
g.play | |
keep_playing = g.play_again? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
here's my previous version.