Skip to content

Instantly share code, notes, and snippets.

@NeMO84
Created January 11, 2011 09:01
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 NeMO84/774214 to your computer and use it in GitHub Desktop.
Save NeMO84/774214 to your computer and use it in GitHub Desktop.
Let me know if things can be improved from both a coding perspective and a performance perspective. Thanks!
# Nirmit Patel
MAX_NUMBER = 100
MIN_NUMBER = 1
MAX_NUMBER_OF_GUESSES = 8
computers_number = rand(MAX_NUMBER - MIN_NUMBER + 1) + MIN_NUMBER
puts "Hello player. What is your name?"
name = gets
name = name.chomp
choice = true
while choice
puts "Hello #{name}. I'm guessing a number between #{MIN_NUMBER} and #{MAX_NUMBER}."
puts "You need to guess the number. I will tell you if you are too low or too high."
puts "Make educated guesses because you only get #{MAX_NUMBER_OF_GUESSES} tries."
puts "If you need help you can enter 'clue'."
counter = MAX_NUMBER_OF_GUESSES
found_answer = false
start_time = Time.now
end_time = nil
is_exit = false
loop do
break if counter <= 0
puts "What is your guess?(#{counter} tries remaining)"
guess = gets.chomp
if guess == "clue"
counter+=1
puts "Clue: The number ends in #{computers_number.to_s[computers_number.to_s.length-1]}"
elsif guess.downcase == "exit"
is_exit = true
break
elsif guess.to_i < computers_number
puts "Your guess was too low!"
elsif guess.to_i > computers_number
puts "Your guess was too high!"
else
# If we get here, the guess was right!
found_answer = true
break
end
counter -= 1
end
end_time = Time.now
if found_answer
puts "You got the number right! It was #{computers_number}."
elsif is_exit
break
else
puts "Sorry you didn't guess the answer within #{MAX_NUMBER_OF_GUESSES} tries!"
end
puts "It took you #{end_time-start_time} seconds to finish."
puts "Wanna play again? (Y or N)"
choice = gets.chomp.downcase == "y"
end
puts "Goodbye and thanks for playing!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment