Last active
February 18, 2021 01:21
-
-
Save Miqueas/0486fb91bb9a94911cb717aa4b511359 to your computer and use it in GitHub Desktop.
[Ruby] Classic guessing game
This file contains hidden or 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
# 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