In this article, I'll 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 numbers between 0 and 9. If the user guessed correctly, the game will print a message and exit. If the user guesses wron

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 numbers between 0 and 9. If the user guessed correctly, the game will print a message and exit. If the user guesses wrong, the game will print a message and ask the user to guess again.

Prerequisites

You need to install Ruby on your computer. If you don't have Ruby installed, you can download it from ruby-lang.org.

Getting Started with

First, create a new file called guessing_game.rb and open it in your favorite text editor. We will first print a welcome message to the user.

puts "Welcome to the guessing game!"

Next, we will generate a random number between 0 and 9 and store it in a variable named secret_num.

secret_num = rand(0 .. 9)

This rand method will generate a random number between 0 and 9. .. operator will create a range from 0 to 9.

Next, we will create an initialization variable:

  • guess- This will store the user's guess
  • guess_count- This will store the number of guesses made by the user
  • guess_limit- This will store the maximum guesses the user can make
  • out_of_guesses- This will store a boolean value indicating whether the user has reached the maximum guess

guess = nilguess_count = 0guess_limit = 3out_of_guesses = false

Next, we will create a while Loop, the loop will run as long as the user does not reach the maximum number of guesses and the user does not guess 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 endend

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 converts the string into an integer.

This if statement will check whether the user has reached the maximum number of guesses. If the user has reached the maximum number of guesses, set the out_of_guesses variable to true and exit the loop while exits the loop.

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 fails to reach 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 = nilguess_count = 0guess_limit = 3out_of_guesses = falsewhile 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 endendif 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, please open the 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 with Ruby. The game will ask the user to guess a number between 0 and 9. If the user guessed correctly, the game will print a message and exit. If the user guesses wrong, the game will print a message and ask the user to guess again.