Skip to content

Instantly share code, notes, and snippets.

@Miqueas
Last active February 18, 2021 01:21
Show Gist options
  • Save Miqueas/0486fb91bb9a94911cb717aa4b511359 to your computer and use it in GitHub Desktop.
Save Miqueas/0486fb91bb9a94911cb717aa4b511359 to your computer and use it in GitHub Desktop.
[Ruby] Classic guessing game
# Magic number to guess
MagicNum = rand 100
# User lives
lives = 3
puts "Guess the number!\n"
puts 'Enter your choice:'
print '> '
# User input
choice = gets.chomp.to_i
# While the user has lives, then ask for a choice
loop do
if choice != MagicNum && lives != 0
puts 'Bad! Try again:'
print '> '
choice = gets.chomp.to_i
lives -= 1
redo
elsif choice != MagicNum && lives == 0
puts 'You loss!'
puts "The number is #{MagicNum}"
break
else
puts 'You win!'
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment