Variables: Building a scorekeeper

Getting started with the BBC micro:bit - Lesson 6

Objectives

  • Understand what variables are and why and when to use them in a program.
  • Learn how to create, set and change a variable value within a micro:bit program.
  • Learn how to use the basic mathematical blocks for adding, subtracting, multiplying, and dividing variables.

Lesson Resources

  • Lesson Slides
  • micro:bits and USB cables (approximately 1 between 2 students)

Introduction

In our last lesson we looked briefly at the concept of a variable when coding. In this lesson we’ll explore that further while building a scorekeeper device.

Computer programs process information. Some of the information that is input, stored, and used in a computer program has a value that is constant, meaning it does not change throughout the course of the program. Other pieces of information have values that vary or change during the running of a program. Programmers create variables to hold the value of information that may change. 

In a game program, a variable may be created to hold the player’s current score, since that value would change (hopefully!) during the course of the game.

Ask the students to think of some pieces of information in their daily life that are constants and others that are variables.

What pieces of information have values that don’t change during the course of a single day (constants)?

What pieces of information have values that do change during the course of a single day (variables) Constants and variables can be numbers and/or text.

 

Examples:

In one school day…

Constants: The day of the week, the year, student’s name, the school’s address

Variables: The temperature/weather, the current time, the current class, whether they are standing or sitting…

 

Variables hold a specific type of information. The micro:bit’s variables can keep track of numbers, strings, booleans, and sprites. The first time you use a variable, its type is assigned to match whatever it is holding. From that point forward, you can only change the value of that variable to another value of that same type.

  • A number variable could hold numerical data such as the year, the temperature, or the degree of acceleration.
  • A string variable holds a string of alphanumeric characters such as a person’s name, a password, or the day of the week.
  • A boolean variable has only two values: true or false. You might have certain things that happen only when the variable called gameOver is false, for example.

On the micro:bit, a sprite is a special variable that represents a single dot on the screen and holds two separate (x and y) values for the row and column the dot is currently in.

In this lesson we’ll mainly focus on number variables

 

Scoring the game

The objective of this activity is to experience creating and working with variables by pairing up and playing Rock, Paper, Scissors (with your hands this time!)

Ask students to keep track of their scores on paper. You can also have students play in groups of three with the third student acting as the scorekeeper.

Students will keep track of how many times each player wins as well as the number of times the players tie.

 

Play: Have students play Rock Paper Scissors for about a minute. When done, ask the students to add up their scores and how many ‘rounds’ they played.

Play again: Tell students they will now start over and play again for another minute. When done, ask the students to add up their scores and how many ‘rounds’ they played.

 

Ask some students to share how they kept track of player scores. There may be some variety, but most will have written down the players’ names and then beside or below the names, marks representing the ‘wins’ of each player. And they may have made a separate place for recording ties.

They may score it a bit like this:

 

 

Ask the students what parts of the score sheet represent constants, values that do not change through the course of a gaming session?

Example: The players’ names are constants.

 

Ask the students what parts of the score sheet represent variables, values that do change through the course of a gaming session?

Example: The players’ number of wins are variables.

 

Tell the students that they will be creating a program for their micro:bit that will act as a scorekeeper for their next Rock, Paper, Scissors game. They will need to create variables for the parts of scorekeeping that change over the course of a gaming session. What are those variables?

  • The number of times the first player wins
  • The number of times the second player wins
  • The number of times the players tie

 

Creating and naming variables

Lead the students to create meaningful names for their variables.

What would be a unique and clear name for the variable that will keep track of the number of times Player A wins?

Student suggestions could be: PAW, PlayerA, AButtonPress, AButtonCount, PlayerAWins…

Discuss why (or why not) different suggestions make clear what value the variable will hold. In general, variable names should clearly describe what type of information they hold.

From the Variables menu, click ‘Make a Variable’ to make and name these three variables: 

PlayerAWins, PlayerBWins, PlayersTie.

 

 

Initialising the variable value

It is important to give your variables an initial value. The initial value is the value the variable will hold each time the program starts. For our counter program, we will give each variable the value 0 (zero) at the start of the program.

 

 

Updating the variable value

In our program, we want to keep track of the number of times each player wins and the number of times they tie. We can use the buttons A and B to do this.

Pseudocode:

  • Press button A to record a win for player A
  • Press button B to record a win for player B
  • Press both button A and button B together to record a tie

 

We already initialized these variables and now need to code to update the values at each round of the game.

  • Each time the scorekeeper presses button A to record a win for Player A, we want to add 1 to the current value of the variable PlayerAWins.
  • Each time the scorekeeper presses button B, to record a win for Player B, we want to add 1 to the current value of the variable PlayerBWins.
  • Each time the scorekeeper presses both button A and button B at the same time to record a tie, we want to add 1 to the current value of the variable PlayersTie.

 

From the Input menu, drag 3 of the ‘on button A pressed’ event handlers to your Programming Workspace.

 

 

Leave one block with ‘A’. Use the drop-down menu in the block to choose ‘B’ for the second block and ‘A+B’ for the third block.

 

From the Variables menu, drag 3 of the ‘change item by 1’ blocks to your Programming Workspace.

 

 

Place one change block into each of the Button Pressed blocks. Choose the appropriate variable from the pull down menus in the change blocks.

 

 

User feedback

Whenever the scorekeeper presses button A, button B, or both buttons together, we will give the user visual feedback acknowledging that the user pressed a button. We can do this by coding our program to display:

  • an ‘A’ each time the user presses button A to record a win for Player A,
  • a ‘B’ for each time the user presses button ‘B’ to record a win for Player B,
  • a ‘T’ for each time the user presses both button A and button B together to record a tie.

We can display an ‘A’, ‘B’, or ‘T’ using either the ‘show leds’ block or the ‘show string’ block.

 

 

Notice that we added a ‘clear screen’ block after showing ‘A’, ‘B’, or ‘T’. Ask - What do you think would happen if we did not clear the screen? Try it

 

Showing the final values of the variables

To finish our program, we can add code that tells the micro:bit to display the final values of our variables. Since we have already used buttons A and B, we can use the ‘on shake’ event handler block to trigger this event. We can use the ‘show string’, ‘show leds’, ‘pause’, and ‘show number’ blocks to display these final values in a clear way. 

 

Here is the complete program.

 

 

Try it out!

Download the Scorekeeper program to the micro:bit, and have the students play one last round of Rock, Paper, Scissors using their micro:bit to act as the Scorekeeper!

 

Extension

‘Adding’ on with mathematical operations

There is more we can do with the input we received using this program. We can use mathematical operations on our variables.

Example: Perhaps you’d like to keep track of, and show the player the total number of ‘rounds’ that were played. To do this, we can add the values stored in the variables we created to keep track of how many times each player won and how many times they tied.

In order to do this, we can add the code to our program under the ‘on shake’ event handler.

  • First, display a string to show the player that the following sum represents the total number of rounds played.
  • Our program will add the values stored in the variables PlayerAWins, PlayerBWins, and PlayersTie and then display the sum of this mathematical operation.
  • The blocks for the mathematical operations adding, subtracting, multiplying, and dividing are listed in the Math section of the Toolbox.

Note: Even though there are 4 blocks shown for these 4 operations, you can access any of the four operations from any of the four blocks using the dropdown menu in the block.

 

 

Replace the default values of zero with variable name blocks of the variables we want to add together. Notice that because we are adding three variables together we need a second math (+) block. 

First we add the values for PlayerAWins and PlayerBWins, then add PlayersTie.

 

 

Remember that the micro:bit is a device that processes inputs and displays them as output in some way. By storing values in variables, you can perform mathematical operations on that data that provides you with useful information.

Ask - How could we generate other statistics about the games played using the variables and these maths blocks? 

 

Examples:

Calculate and display a player’s wins and/or losses as a percentage of all rounds played.

 

 

Calculate and display the number of tied games as a percentage of all rounds played.

 

 

Plenary

Review the students’ understanding of the key concepts of the lesson and how successful they have been with their project. 

  • What is a variable?
  • Why do we use them in programs?
  • What is important when we give a variable a name?
  • How many values can a variable hold at any time? 

 

What did you think of this unit?

When you have completed the unit with your class, please take a moment to leave us some feedback with this short form, it'll really help us create more content you love in the future. Thank you!