Skip to content
Advertisements

Snap! (BYOB): Lesson 3 – Building a Number Guessing Game Using Variables, Loops, and If/Else Statements

For background information and how to get started with Snap! (formerly known as BYOB), click here for lesson 1. I’m using BYOB version 3.1.1 for my examples, but you can follow along with either version linked below.

Snap! homepage

BYOB download

Previous lessons:

My experience with Snap! made learning C++ much easier. If you can wrap your mind around the logic for this game, you’re already thinking like a programmer. I always begin by asking myself what the program requires. Is there something that needs to change or something that needs counted? Those are my variables. Is there a decision to be made? That might be an if/else statement. You always want to think through the logic before jumping in.

For this exercise, I’m using blocks from the following categories:

  • Looks (Purple)
  • Control (Brown)
  • Sensing (Blue)
  • Operators (Green)
  • Variables (Orange)

After this tutorial, you should be able to create a number guessing game. This information easily transfers to coin flips or similar simple games.

This program requires 4 variables:

  • randomNumber
  • ourGuess
  • correctGuesses
  • totalGuesses

Note: Your variables can have different names but they should be clear in their purpose and follow thisCaseFormat.

Click Make a variable, one-by-one enter variable names in the pop-up box.

guessinggame (1)

guessinggame (2)

guessinggame (3)

Leave checkmarks

The checkmarks display our variable names on the stage. You won’t display them during gameplay, but they should be up for “debugging” before the game is ready.

guessinggame (4)

Under Control, grab the repeat until __ block.

guessinggame (5)

Under Operators, grab the equals block.

guessinggame (6)

Place the equals block after repeat until.

guessinggame (7)

Under Variables, grab the correctGuesses variable you created and place it inside the equals block. Manually enter a number in the right-hand side of the equals block.

guessinggame (8)

This is the main loop of the program. It’s pretty straightforward – the program loops whatever is inside the block until the player has made 3 correct guesses. This is an idea you’ll run into many times in programming.

Jumping out of the loop for a moment, under Variables, grab the set __ to __ block. By default, this will show one of the variables. Click the dropdown menu to bring up options. Set totalGuesses and correctGuesses to 0. Repeat the same steps for randomNumber but place it inside the loop.

guessinggame (9)

guessinggame (10)

Under Operators, grab the pick random __ to __ block.  Manually enter a number range.

guessinggame (11)

Under Sensing, grab the ask ____ and wait block. This block makes the Sprite ask the user a question then waits for a response. Manually enter the question or comment.

guessinggame (12)

Under Variables, grab another set __ to __ block. Set the first option to ourGuess and grab the answer block under Sensing for the right-hand side.

guessinggame (13)

Under Control, grab the if/else block. If/else statements are common in programming so get familiar with them. Looking at the program so far, a random number is picked, the user makes a guess, then something needs to happen if it is correct, or else something needs to happen if it’s incorrect. First, let’s build a “check if it’s correct” block.

guessinggame (14)

Under Operators, grab an equals block and place it to the right of if. Under Variables, grab ourGuess and randomNumber, and place them inside the equals block.

guessinggame (15)

Under Looks, grab the say __ for __ secs block. Manually enter the response for a correct guess and amount of time you want it displayed.

guessinggame (16)

Under Variables, grab the change __ by __ block. Set the first part to correctGuesses and right-hand side to 1. Notice what is happening in the if part of this statement. The guess is compared to the random number, if that is correct, a statement is output, then correct guesses increases by one.

guessinggame (17)

If the guess is incorrect, we only need to display a message saying it’s wrong. Nothing changes on the counts because we’re not counting incorrect guesses. But we are counting total guesses, so that variable is placed after the if/else statement (because we want it to happen whether the guess was right or wrong) but inside the repeat loop.

Under Looks, grab the say __ for __ secs block. Manually enter a message. Under Variables, grab the change __ by __ block. Select totalGuesses and increase it by 1.

guessinggame (18)

guessinggame (19)

Jumping out of the repeat loop, I’m going to add blocks that relay the total number of guesses to the user. Under Looks, grab the say __ for __ secs block. Don’t worry about changing the text yet.

guessinggame (20)

The simple say block has the one text box but this program requires multiple boxes since it needs to output both text and the total guess count. Under Operators, grab the join __ __ block, place it inside the say block, grab a second join block, and place it inside the first join block. This creates 3 boxes of changeable output.

guessinggame (21)

guessinggame (22)

Now the boxes can be customized. The first and last boxes inside the join blocks should have text. The middle box should pass through totalGuesses. Under Variables, grab another totalGuesses block.

guessinggame (23)

Below is the finished program. From top to bottom: totalGuesses and correctGuesses are set to 0. The repeat block starts and continues looping through every step inside until the criteria is met, in this case, that’s correctGuesses equaling 3.

Still inside the loop, a randomNumber is chosen between 1 to 5 inclusive. The user is asked for input and the program waits for an answer. That answer is passed on to ourGuess. Then we jump into the if/else statement. If ourGuess matches randomNumber, a message is displayed and the correctGuesses count goes up by 1. Else, a message displays and no count is changed.

We come out of the if/else statement, remaining in the repeat loop, and totalGuesses increases by 1. The program runs again, starting from the repeat until line. Once 3 correctGuesses are counted, we come out of the repeat loop where a message is displayed and the totalGuesses is relayed back to the user.

guessinggame (24)

Run the program with the variables displayed on the stage. Test the program with correct and incorrect answers. Watch the count to see if everything works.

guessinggame (25)

guessinggame (26)

Unclick next to the variables you created under Variables to remove the boxes from the stage.

guessinggame (27)

The game is more challenging when you can’t see the answers. 🙂

guessinggame (28)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: