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----------------"
Copy link

ghost commented Aug 1, 2013

I made a similar program using Java. What I did was let the user pick the highest number the game can generate, instead of 10 the user can put any number. I also made it so you have unlimited turns but at the end, after you guess the number, it will tell you how many turns it took. Good job though. By the way, what did you write this program in?

@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