1. Introduction & Setup


In this guide we're going to write a simple number guessing game in Ruby. Once finished your program will look similar to this:

To be able to run the game, we need to install a program that translates our written Ruby code into something that the computer can understand (ones and zeroes).

Sadly, corporate devices are usually locked down and it's not possible to install additional software without administrator privileges. This is why we will use an online service to write our game.

Your first job is to go to repl.it/signup and sign up for an account. After finishing the registration process, follow the visual instructions below.

Click on "+ New repl" on the sidebar:

Select Ruby, name the project and click "Create repl":

Once done, you should end up on a page that looks like this:

This will be your work environment for the rest of the exercise. You can minimize the "files" column, we won't need it.


2. Running our first program


First some basics:
We write our code into the white area. This area is called the "text editor". It works like any other plain text editor you know (Notepad, TextEdit). Once we have written the code we can execute it using the "Run" button on the top. The result of our program will be printed out on the black area. The black area is called the "console".

First exercise:
Type the code below into the text editor (white area). DO NOT COPY & PASTE IT!

puts "This is my first Ruby program - how exciting!"
Ruby is very fussy about spelling. So make sure you type in everything exactly as shown above (including the spaces and quotations marks).

Once you have typed in the code above click on "Run" and look at the result in the console (black area). If everything worked it should look something like this:


3. Breaking down the game


One of the most important skills a programmer needs, is the ability to break down big problems down into smaller parts. To do so we can try to create a version of the game that isn't as complex as the one illustrated at the beginning. Breaking down the simpler version could look something like this:

  1. Tell the computer to generate a random number. This is the number the player has to guess.
  2. Tell the player what he has to do: "Please enter a number between 0 and 100".
  3. Get the input from the player.
  4. Tell the player what he has guessed and show the number that was randomly generated.

If we translate these steps into actual Ruby code it would look something like this:

# 1. Generate a random number and assigns it to the variable "random_number"
random_number = rand(0..100)
# 2. Print out the instructions for the player
print "Please enter a number between 0 and 100: "
# 3. Get input from the player and assign it to the variable "guess"
guess = gets.chomp.to_i
# 4. Print out the result
puts "You've guessed #{guess}, the random number was: #{random_number}"

IMPORTANT: A # (pound sign) at the beginning of the line indicates a comment which will be not executed by the program and is used to add notes directly in our program.

If we run the program the result should look something like this:


4. Letting the player try multiple times


In the chapter before we've create a simplified version of the game. Let's try to identify the next part that is still missing when compared to the final version:

Right now the game ends after one guess. The player should be able to continue entering numbers untill he guesses the random number.

Extend your program so that it looks like the code below:

random_number = rand(0..100)
print "Please enter a number between 0 and 100: "

# Start the loop
loop do
  # Get the input from the player
  guess = gets.chomp.to_i
  # Break out of the loop if the guess is equal to the random number
  break if guess == random_number
  # Print out this message if the guess is not equal to the random number
  puts "You've guessed #{guess} but the random number is different! Try again:"
end

# Print out the winning message
puts "Yaaay, you won!"

Run the program again. What has changed? Try to explain what every line is doing.


5. Giving hints to the player


If you've played the previous version a few times you'll notice that the game is still quite tedious. That's because we're not giving the player any hints whether he is moving into the right direction with his guesses. Let's fix that.

Once more, extend your program so that it looks like the code below:

random_number = rand(0..100)
print "Please enter a number between 0 and 100: "

loop do
  guess = gets.chomp.to_i

  # Print out some hints depending on the input
  puts "Please enter a positive number"     if guess < 0
  puts "Please enter a number below 100"    if guess > 100
  puts "Your number is too low. Try again"  if guess < random_number
  puts "Your number is too high. Try again" if guess > random_number

  break if guess == random_number
end

puts "Yaaay, you won!"

Run the program. Does it work?


6. Congratulations!


Congratulations, you've just finished your first game in Ruby! Have some cake!

Extra: Do you have any ideas on how you could improve the game?
Extra: What code would you need to add a limit the number of times a player can guess the random number before he looses?


Additional Links



How to continue learning Ruby?