Build a number guessing game in Ruby
Published: 2022-09-19 01:16:24

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](https://www.ruby-lang.org/en/downloads/).

## 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.

```ruby
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`.

```ruby
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

```ruby
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.

```ruby
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.

```ruby
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:

```ruby
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:

```bash
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](https://github.com/Owusu-Desmond)
- Twitter [@DesmondOwusuDev](https://twitter.com/DesmondOwusuDev)
- LinkedIn [@desmond-owusu-ansah](https://www.linkedin.com/in/desmond-owusu-ansah-09274a223/)
- Youtube [@Desmond Owusu Ansah](https://www.youtube.com/channel/UCJGz0BLcCjlkqrn47eexJXg)