DEV Community

Cover image for Find The Number
Thomas Bnt
Thomas Bnt

Posted on • Updated on

Find The Number

Find The Number

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.

Example of the script

  • 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
Enter fullscreen mode Exit fullscreen mode
  • GitHub

The repository is on GitHub, check out now.

GitHub logo thomasbnt / Find-The-Number

My first script in Ruby.

Discussion (6)

Collapse
anbrandt profile image
Andrzej Brandt

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.

Collapse
thomasbnt profile image
Thomas Bnt Author

oh yes I know, I had just made the code with capital letters to better find me. 😁

Collapse
peter profile image
Peter Kim Frank

Good use of a GIF to show the gameplay :)

Collapse
thomasbnt profile image
Thomas Bnt Author

Yeah, it seems easier to show this game 👌

Collapse
nektro profile image
Meghan (she/her)

Nice!

Collapse
thomasbnt profile image
Thomas Bnt Author

Thank you ! ☺️