This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Desmond Owusu Ansah
In this article I will show you how to create a simple number guessing game in Ruby. The game will be a command line application that will ask the user to guess a number between 0 to 9. If the user guesses correctly, the game will print a message and exit. If the user guesses incorrectly, the game will print a message and ask the user to guess again.
Prerequisites
You will need to have Ruby installed on your computer. If you don't have Ruby installed, you can download it from ruby-lang.org.
Getting started
To get started, create a new file called guessing_game.rb
and open it in your favorite text editor. We will start by printing a welcome message to the user.
puts "Welcome to the guessing game!"
Next, we will generate a random number between 0 to 9 and store it in a variable called secret_num
.
secret_num = rand(0 .. 9)
The rand
method will generate a random number between 0 to 9. The ..
operator will create a range from 0 to 9.
Next, we will create a initialize the following variables:
-
guess
- this will store the user's guess -
guess_count
- this will store the number of guesses the user has made -
guess_limit
- this will store the maximum number of guesses the user can make -
out_of_guesses
- this will store a boolean value indicating if the user has reached the maximum number of guesses
guess = nil
guess_count = 0
guess_limit = 3
out_of_guesses = false
Next, we will create a while loop that will run as long as the user has not reached the maximum number of guesses and the user has not guessed the secret number.
while guess != secret_num and !out_of_guesses
# if guess count is less than guess limit then increment guess count
if guess_count < guess_limit
puts "Enter your guessed number"
guess = gets.chomp.to_i
guess_count += 1;
else
# if guess count is greater than guess limit then set out_of_guesses to true
out_of_guesses = true
break
end
end
The gets
method will read a line from the user's input. The chomp
method will remove the newline character from the end of the string. The to_i
method will convert the string to an integer.
The if
statement will check if the user has reached the maximum number of guesses. If the user has reached the maximum number of guesses, the out_of_guesses
variable will be set to true
and the while
loop will exit.
Next, we will check if the user has reached the maximum number of guesses. If the user has reached the maximum number of guesses, we will print a message to the user and exit the game. If the user has not reached the maximum number of guesses, we will print a message to the user and ask them to guess again.
if out_of_guesses
puts "You lose!"
puts "You have reached the maximum number of guesses."
puts "The secret number was #{secret_num}"
else
puts "Congratulations! You won!"
puts "You guessed the secret number #{secret_num} and you did it in #{guess_count} guesses."
end
Finally, your code should look like this:
puts "Welcome to the number guessing game!"
secret_num = rand(0 .. 9)
guess = nil
guess_count = 0
guess_limit = 3
out_of_guesses = false
while guess != secret_num and !out_of_guesses
# if guess count is less than guess limit then increment guess count
if guess_count < guess_limit
puts "Enter your guessed number"
guess = gets.chomp.to_i
guess_count += 1;
else
# if guess count is greater than guess limit then set out_of_guesses to true
out_of_guesses = true
break
end
end
if out_of_guesses
puts "You lose!"
puts "You have reached the maximum number of guesses."
puts "The secret number was #{secret_num}"
else
puts "Congratulations! You won!"
puts "You guessed the secret number #{secret_num} and you did it in #{guess_count} guesses."
end
To run the game, open a terminal and navigate to the directory where you saved the guessing_game.rb
file. Then run the following command:
ruby guessing_game.rb
Conclusion
In this article, we created a simple number guessing game in Ruby. The game will ask the user to guess a number between 0 to 9. If the user guesses correctly, the game will print a message and exit. If the user guesses incorrectly, the game will print a message and ask the user to guess again.
Follow me
If you enjoyed this article, please follow me on
- Github @Owusu-Desmond
- Twitter @DesmondOwusuDev
- LinkedIn @desmond-owusu-ansah
- Youtube @Desmond Owusu Ansah
This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Desmond Owusu Ansah
Desmond Owusu Ansah | Sciencx (2022-09-19T01:16:24+00:00) Build a number guessing game in Ruby. Retrieved from https://www.scien.cx/2022/09/19/build-a-number-guessing-game-in-ruby/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.