Created
August 1, 2013 05:12
-
-
Save heyjinkim/6128561 to your computer and use it in GitHub Desktop.
Number Game in Ruby
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
print `clear` | |
puts "Welcome to the game. Enter your name:" | |
name = gets.chomp | |
max_guesses = 3 | |
correct_number = rand(1..10) | |
(1..max_guesses).each do |i| | |
print "Guess a number between 1 and 10! --> " | |
guess = gets.chomp.to_i | |
left_guess = max_guesses-i | |
if guess === correct_number | |
puts "Correct! Well done." | |
break | |
elsif guess > correct_number | |
puts "Your number was a bit high." | |
elsif guess < correct_number | |
puts "Your number was a bit low." | |
end | |
puts left_guess>0 ? "You have #{left_guess} picks left." : "You're DONE. Sorry!" | |
end | |
puts "----------------\n The number was #{correct_number}. Goodbye #{name}! \n----------------" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @dasibre!