Demo: Procedural Ruby

A walkthrough of a simple game coming together in Ruby.

Scroll down...

Content

Resources

Comments

Until now, you've mostly been building self-contained scripts. During the next several units, we'll be applying OOP principles and turning your scripts into full-fledged programs. To start bridging the gap, we'll start out working through a procedural games script.

In this brief demo, we'll walk through building a simple children's game using procedural Ruby. We'll take input from the Command Line and display our results to the same. Our goal is to show you simple game mechanics, Ruby scripting, and user input.

We'll build this game in a procedural style, meaning we won't make use of classes or object instances along the way. That's coming up soon enough!

The code for this demo is available on Github

Higher/Lower

The game we're playing is simple: The computer will pick a random number from 1 to 10. You have 3 guesses. After each guess, the computer will tell you if you've won or if the hidden number is higher or lower than your guess.

Initial Planning

Because this is a procedural problem, we just need to think through what's happening from the top and then work our way through it. In this case:

  1. Player starts the game (script)
  2. The game randomly determines the hidden number
  3. Player is prompted for the first guess
  4. The guess is checked against the hidden number
  5. If the guess is right, the game ends. Otherwise player gets feedback about the guess.
  6. Player is prompted for the second guess
  7. The guess is checked against the hidden number
  8. The guess results are again presented
  9. Player is prompted for the third guess
  10. The guess is checked against the hidden number
  11. The guess results are again presented
  12. If the number hasn't been guessed, the game ends in a loss.

Coding it Up

We'll start by putting together the basic linear procedure in one jumbled script. You'll likely be able to jump right to using loops and methods... we've done this for the sake of being explicit.

See the video below (6:37):

Note: If it's your style, these videos can be accelerated to 1.25, 1.5 or 2x speed. See the options icon in the player to do so.

hidden_num = rand(1..10)

puts "Hey, thanks for playing hi/lo!"
puts "You have three gueses to find the number 1-10"
puts "Make your first guess:"
print "> "

guess = gets.chomp.to_i

puts "Your guess was #{guess}!"

if guess == hidden_num
  puts "You WIN! It was indeed #{guess}!"
  exit
elsif guess > hidden_num
  puts "Go lower..."
else
  puts "Go higher..."
end


puts "Make your second guess:"
print "> "

guess = gets.chomp.to_i

puts "Your guess was #{guess}!"

if guess == hidden_num
  puts "You WIN! It was indeed #{guess}!"
  exit
elsif guess > hidden_num
  puts "Go lower..."
else
  puts "Go higher..."
end


puts "Make your final guess:"
print "> "

guess = gets.chomp.to_i

puts "Your guess was #{guess}!"

if guess == hidden_num
  puts "You WIN! It was indeed #{guess}!"
else
  puts "YOU LOSE! It was actually #{hidden_num}."
end

Refactoring into Methods and Loops

The initial approach obviously has a lot of duplication, so we can start to refactor it out to use some loops and methods.

See the video below (11:27):

# Helper to check for victory, loss, and direction
def check_guess(guess, hidden_num, guesses)
  if guess == hidden_num
    puts "You WIN! It was indeed #{guess}!"
    exit
  elsif guesses == 3
    puts "YOU LOSE! It was actually #{hidden_num}."
  elsif guess > hidden_num
    puts "Go lower..."
  else
    puts "Go higher..."
  end
end



# Main Method
def play
  hidden_num = rand(1..10)

  puts "Hey, thanks for playing hi/lo!"
  puts "You have three guesses to find the number 1-10"

  num_guesses = 0

  while num_guesses < 3
    num_guesses += 1

    puts "Make guess ##{num_guesses}:"
    print "> "

    guess = gets.chomp.to_i

    puts "Your guess was #{guess}!"

    check_guess(guess, hidden_num, num_guesses)

  end

end


# Actually run this puppy
play

Extensions

There are a number of different extensions to this and you're welcome to try them out on your own. One typical pattern would be checking user input. You might build a loop around your input until it's valid:

is_valid = false
until is_valid
  puts "Make a guess:"
  print "> "
  guess = gets.chomp.to_i

  # make sure it's within range
  is_valid = true if (1..10).include?(guess)
end

Wrapping Up

This is just a bite-sized script to get you thinking about procedural Ruby. In this case, we were very (needlessly) explicit about doing things the long way. In reality, you'll almost certainly see the need for a loop or a method without having to actually copy-paste code first. But don't feel like you need to perfect the code going in -- you'll want to have a plan but often it won't become fully clear until you've actually started working.

Octocat 300


Sign up to track your progress for free

There are ( ) additional resources for this lesson. Check them out!

There are no additional resources for this lesson just yet!

Sorry, comments aren't active just yet!

Next Lesson: Test Yourself: Intermediate Ruby