Skip to content

Instantly share code, notes, and snippets.

@heyjinkim
Created August 1, 2013 05:12
Show Gist options
  • Save heyjinkim/6128561 to your computer and use it in GitHub Desktop.
Save heyjinkim/6128561 to your computer and use it in GitHub Desktop.
Number Game in Ruby
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----------------"
@dasibre
Copy link

dasibre commented Aug 2, 2013

Wow, thats soooo concise and just beautiful........

@heyjinkim
Copy link
Author

Thanks @dasibre!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment