Hello ! The code and the post show my first script in Ruby, please be indulgent.
- Description
It is a basic 'game' under console/terminal that must be run with ruby.
The goal? Find the exact number.
- The system
A simple random number for the chances and a condition to check whether it is small or big. If he is too big or too small, he will mark it for you.
- Code
MaxChiffre = 20
NumberToGuess = Random.new.rand(MaxChiffre)
MaxChance = Random.new.rand(2..6)
a = nil
puts "Enter your number between 0 and #{MaxChiffre} included, you only have #{MaxChance} chances."
for num in 1..MaxChance
next if a == NumberToGuess
a = gets.chomp.to_i
if a > NumberToGuess
puts "Number too big"
elsif a < NumberToGuess
puts "Number too small"
end
end
puts "Congratulations, you've found it !" if a == NumberToGuess
puts "Pity ! It was #{NumberToGuess}. " if a != NumberToGuess
- GitHub
The repository is on GitHub, check out now.