Skip to content

Instantly share code, notes, and snippets.

@brettchalupa
Last active December 11, 2015 12:19
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 brettchalupa/4600237 to your computer and use it in GitHub Desktop.
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).
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