BBC micro:bit – Higher or Lower Game

For this challenge you will design and write a program to play against the BBC micro:bit.

The micro:bit will display a random number between 0 and 100. It will then ask the end-user whether they believe the next number will be higher or lower. The program will then generate the next number. If the user guessed right (e.g. the next number is higher or lower than the previous one) then the user scores one point. The game stops when the user guesses it wrong.

micro-bit-higher-or-lower-5

Learning Objectives

By completing this challenge you are going to use selection (IF statements) and comparison operators such as >= or <= to compare numbers. You will also use variables to store the value of random numbers, and to keep and increment a score as the game progresses.

Solution


Try to implement the game by yourself on the BBC micro:bit website:

micro-bit-logo

If you are stuck you can always use our solution below:

Step 1: Initialising the game
For our game we will need three variables:

  • number: the current number, randomly selected (between 0 and 100) being displayed on the LED screen,
  • nextNumber: the next number, also randomly selected between 0 and 100, but kept secret, the user will decide if they beieve it will be higher or lower than the current number,
  • score: the score will start at 0 and increment by 1 everytime the user has a good guess.

micro-bit-higher-or-lower-1

Step 2: Button A: Lower
When the user presses button A, they believe the next number will be lower than the current number. We can check if they are right or not.

If they are correct, we will display a smiley face, add 1 to their score and the nextNumber will become the current number whereas a new nextNumber is generated for the game to continue.

If they are wrong though, we will display a sad face as well as the value of the next number followed y the user score.micro-bit-higher-or-lower-2

Step 3: Button B: Higher
Step 3 is very similar to step 2 but this time we are checking whether the next number is higher than the current number.micro-bit-higher-or-lower-3

Extension Task:


That’s it you have a fully working game. We can tweak it slightly though. For instance how could we code the micro:bit so that when the user presses both the A and B buttons at the same time, the game restarts from the beginning – a new set of numbers is generated and the score is reset to zero?

Tagged with: