For today’s lesson, I wanted to do something that was both simple and fun. When you get to interact with the program in the terminal, you will get super charged and want to do more – it’s a very exciting experience. So let’s do it!
Today we will be creating a game of Rock, Paper, Scissors that will be run and played within the terminal. To get started, let’s create a directory and file. In your terminal, navigate to the desktop and then run the following command,
mkdir terminal_game. Then type in
terminal_game to move into that folder and then type in,
touch game.rb. This will create a Ruby file inside of your directory – we will be writing all of our code in here.
So, what are the main components of this game? Well, first we need to be prompted by the computer to choose
Scissors. Then, the computer needs to pick one and we need to calculate who the winner was based on our choices. This is at the heart of the game, and as you can imagine will require some loops and if / else statements.
The first thing that we want to address is our loop, which is where all of the choices by the user will be made. It would make sense that we would want to maybe play a few round of this, so instead of using a standard
loop, we can use a
while loop. We can set a variable,
play_again = 'y' and then call on it in our while loop like so,
while (play_again == 'y'). This way, we will run through this loop just so long as the user wants to continue playing.
We have preset the variable to be
y and as such the while loop will run regardless on the first go around, so now that we are in the loop lets prompt the user. We can display to the screen using a
puts. Lets try this,
puts "Chose Rock (r), Paper (p) or Scissors (s): " . We can then check to see what the user inputted and then set that value, let’s take a look.
This way, we can tell them they inputted a wrong choice if they didnt input
r, p or s. Then we can use our if else block to check the value and then reassign it. If the user is inputting
r then we can redefine
user_choice = 'rock'. That’s it, now
rock instead of
This covers our first problem, getting the user to make a choice. Now, we need to allow the computer to make a choice as well. To achieve something approximating a human, we will use the
rand function. Since there are 3 choices:
Scissors, we will use a random function that looks like this:
computer = rand(3). This will produce a number between 0 and 2, since we are operating on a zero-based index. To make this more human readable, we can adjust that to be
computer = rand(3) + 1, this will add 1 to the random number generated which will produce either 1, 2 or 3 as a result – much easier to understand.
Great, the user has chosen as well as the Computer. Now, let’s calculate the results! This is pretty straightforward, we are just going to evaluate the inputs for both
Computer and then calculate based on that.
This, of course, is going to use a rather long if else blockchain, so let’s dig in. To start, we have two variables –
user_choice. We need to check the values of both at the same time for each draw. We will begin our if else block by checking to see if the two variables are equal. From there, we check the value of one variable and make sure to use the
&& operator to check for the value of both the user and computer simultaneously. Something like the following will work for now:
This will both check the value of the ‘hand’ between players as well as give the user the option to continue playing another round. There you have it, you just created a simple game of Rock, Paper, Scissors in Ruby. Later down the road, you can use a very similar approach to build a game of BlackJack, albeit a little more complicated.