These free mini-courses will give you a strong foundation in web development. Track your progress and access advanced courses on HTML/CSS, Ruby and JavaScript for free inside our student portal.
Scroll down...
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
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.
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:
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
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
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
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.