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.
Top comments (6)
nice one. however, I would strongly recommend checking the styling guide for Ruby - github.com/rubocop-hq/ruby-style-g.... In the code example, you are using uppercase letters in naming variables - a practice that is foreign in Ruby.
oh yes I know, I had just made the code with capital letters to better find me. 😁
Good use of a GIF to show the gameplay :)
Yeah, it seems easier to show this game 👌
Nice!
Thank you ! ☺️