Last active
December 11, 2015 12:19
-
-
Save brettchalupa/4600237 to your computer and use it in GitHub Desktop.
A fun little game in Ruby for guessing a number between 1 and 100 (inclusive).
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 RandomGame | |
def play | |
in_game = true | |
while in_game | |
puts "Guess a number between 1 and 100 (inclusive)." | |
random_number = generate_random | |
print "> " | |
guess = gets.chomp.to_i | |
if guess > random_number | |
puts "Your guess is too high." | |
elsif guess < random_number | |
puts "Your guess is too low." | |
else | |
puts "You guessed correct." | |
end | |
puts "The number was #{random_number}. You guessed #{guess}" | |
puts "Play again? (y/n)" | |
print "> " | |
play_again = gets.chomp | |
if play_again != "y" | |
in_game = false | |
end | |
end | |
puts "Thank you for playing!" | |
end | |
def generate_random | |
rand(100) + 1 | |
end | |
end | |
random_game = RandomGame.new | |
random_game.play |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment