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
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?