Introduction: What's the Bit Thinking? Make a Simple Guessing Game With the BBC Microbit!

About: I'm a Mechanical Engineer turned IT Professional and Author. I came into the Information Technology world because someone challenged me to. But at heart, I'm still a grease monkey with no desire to lose touch …

I picked up a couple of BBC Microbits after reading positive reviews about them in several online articles.

In an attempt to familiarize myself with the BIT, I played around with the online Microsoft Blocks Editor for a couple of hours and came up with a simple guessing game.

In this IBLE we will talk about the rules of the game and navigate through the functions assembled in different blocks of code in the Blocks Editor.

Finally, excited audience can navigate to the online simulator and give this simple game a shot.

Let's get started.

Step 1: Before Getting Started...

The Hardware

The BBC Microbit board is easy to familiarize yourself with. For this IBLE, all you'll need to know is that it's a tiny board with 2 push buttons, an array of tiny LEDs , and a micro-USB socket that acts as power supply and a means to load a program on to the board.

The array of tiny LEDs can be programmed to light up in different patterns.


You do not need the physical board for this IBLE! The BBC Microbit site has an online simulator to test the code you develop online.

The Software

This IBLE does not go into detail on getting started with the BBC Microbit and/or the Online Blocks Editor.

Audience are encouraged to explore this BBC Micorbit resource before proceeding to the next steps.

BBC Microbit has been designed to teach programming using a very user-friendly approach and therefore familiarity with another programming language is a nice to have, but not required especially if you're going to use the Microsoft Blocks Editor to learn programming.

Nevertheless, I haven't yet encountered a product that is so easy to get started with than the BBC Microbit, given that I own and play with several other micro-controller programming platforms.

Step 2: Rules of the Guessing Game

The rules of the game are as follows:

  1. On powering up, the BIT thinks of a number and challenges the human player to guess what that number is by printing a message on its LED panel, and waits for an answer
  2. The player makes a guess and pushes the Left Push Button (A) as many times as the answer - Example, if the player guessed 5, then the button is pushed 5 times
  3. To submit the answer to the BIT, the player pushes on the Right Push Button (B) once!
  4. If the players guess is right, then the player scores a point, if not the BIT gets a point
  5. If one player guessed a number outside of the range, the other player scores a point - the BIT displays a large X on its' LED display and proceeds to guess a new number and repeat the challenge;
    Example: If the BIT challenged you to guess a number between 1 and 5, and you guessed 6, or if you guessed ZERO (by simply pushing Button B before pushing Button A), then that round is invalidated
  6. The challenge-response continues until one of the players score the maximum allowable points

Starting a New Game:

To start a new game anytime, simply give the BIT a shake! Or, simply disconnect and reconnect the power.

Step 3: The Blocks Program: Variables

The program uses a few variables named to match their functions listed below. These variables can be created by clicking on the Variables Programming tab in the Blocks Editor

BIT_SCORE - Keeps track of the points scored by the BIT

bitGuessed - This variable holds the random value guessed by the BIT

CHECK - when set to True, the BIT checks if the players guess is equal to the BITs guess

GAME_RANGE - This number limits how high the BIT or a player can guess

Example: If set to 6, either players can guess from 1 to 6

MSG - A string that is changed to display different messages based on the context of the game

PLAYER_SCORE - keeps track of the points scored by the player

playerGuess - The number guessed by the player is recorded in this variable


The very first variable named item is a general and default variable and can be ignored

Step 4: The Functions

As part of the learning process and for better organization, the code was organized into a number of functions that could be called or invoked in the main program. Just as variables, the functions can be created and accessed by clicking on the Advanced > Functions Programming tab.

Here below is a brief description of the functions:

announceWinner - This function prints out the Winning message addressed to the winning player

doChallenge - The function that the BIT calls to guess a number and pose a challenge to the player

showBadGuess - Displays a message with a huge X on the LED panel if either of the players guessed outside the range allowed

showLose - is called if the BIT wins a round of challenge over the player

showMessage - presents a message to challenge the player to guess a number within a set range

showWin - is called to when the player wins the guessing round over the BIT

Step 5: The Program Startup and the Shake Events & Event Handlers

The "on start" and "on shake" are event handlers that are called respectively when the "start" event and "shake" events happen. As seen from the Blocks code, what happens when these two event handlers are called are almost the same as in:

  • The showMessage() function is called to display the message when the game begins
  • There is a brief pause of 650 milli-seconds after the message is displayed
  • The doChallenge() function is called in which the BIT guesses a number and waits for the human player to submit a guess

We will cover the two functions in the next step.

That said, there are fundamental differences between the the "on start" and "on shake" event handlers:

  1. The "on start" event handler runs each time the BIT is started up - as in disconnecting and reconnecting power to the BIT
  2. Certain things that must happen only once when the program starts up are defined in the "on start" and nowhere else in the entire program
  3. One such thing is initializing the GAME_RANGE variable - in this case, it's initialized to 6 which means that the players can guess a number between 1 and 6
  4. As the "on start" is a basic BIT function, it's no wonder that it's accessible from the Basic programming tab of the Blocks Editor
  5. The "on shake" on the other hand is an event handler that is called whenever the user provides an input to the BIT in the form of a quick shake
  6. Therefore, the "on shake" event handler can be found inside the Input programming tab of the Blocks Editor

Step 6: The Startup Message and Challenge Functions

The showMessage() Startup Message function is a simple function that clears the BITs LED palette with a clear screen command and displays a scrolling message challenging the player to guess a number.

As seen from the showMessage function block, a couple of fixed pieces of string are appended together to the GAME_RANGE variable to display a simple message that says:

Guess 0 - 6!

The doChallenge() function is called at the beginning of each challenge round, and therefore does the following as seen from the doChallenge function block:

  1. Initializes the playerGuess variable to zero before accepting a new input from the human player
  2. Initializes the CHECK variable to false - meaning that the round is about to begin and it isn't time yet to compare the numbers guessed by both players
  3. Next, and most significant, the BIT picks a random number within the range defined by GAME_RANGE and records it in the bitGuessed variable
  4. Finally, the LED palette is lit up with a question mark symbol prompting the human player for an input in response to the BITs challenge!


If the GAME_RANGE variable value is changed as discussed in the previous step, your game may look and act a bit different.

The join function is a string function and can be accessible from the Text programming tab of the Blocks Editor

Step 7: The Button a Click Event - Respond to the BITs Challenge!

Once the large question mark appears on the BITs LED screen, responding to the challenge for the human player is to simply do the following:

  1. Guess what number the BIT might be thinking between 0 and 6
  2. Press the Button A as many times as your answer is - Example if your answer is 3, then press Button A 3x times

Obviously, pressing the button is a user input and therefore the "on button" event handler is accessible via the Input programming tab of the Blocks editor. Each time, the user presses Button A, this event handler does the following:

  • Increment the value of the playerGuess variable by 1 as long as it's within the GAME_RANGE limit (6 in this case)
  • If the player submits 7 or higher, the guess is invalidated and the playerGuess is reset back to Zero, essentially invalidating the players guess

The next action is to submit the response to the challenge, which we will walk through in the next step.

Step 8: The Button B Click Event - Submit Response to the BITs Challenge!

The human player submits the answer to the BIT by pressing Button B once. A number of steps happen in the Button B "on click" event handler as soon as the BIT receives the players answer as indicated by the code markers in the image:

  1. The CHECK flag is set to True - a signal for the BIT to compare the its guess with that of the players
  2. The scoring proceeds only if both players have guessed numbers greater than Zero and within the GAME_RANGE limit (of 6) and if not, the showBadGuess() function is called - the good guess player wins a point, and the bad guess player loses one!
  3. If the players guess matches that of the BITs, the showWin() function is called - the player wins this round!
  4. If not, the showLose() function is called - the BIT wins this round!
  5. A check is performed at the end of each round by the announceWinner() function to see if one of the two players has won by scoring 3 points - if so, the game is ended and the final winner is announced and all scores & messages are reset
  6. The BIT restarts the game by issuing a challenge by calling the doChallenge() function and awaits for the player to respond and the process repeats

We will discuss the new functions in the next steps.

Wait! How do you guess ZERO?

  • If you look at the random() Math function, you can see that it's initial value is set at Zero and cannot be changed.
  • The BIT is therefore free to guess a Zero and challenge the player as a result of this limitation
  • Similarly, the human player can submit a Zero guess by simply clicking Button B without clicking Button A first!
  • If this is allowed then the human player does not have to make any effort to play and that makes no sense!
  • Therefore, this limitation has been compensated for by Bullet #2. discussed above

Step 9: The Win/Lose and Rest of the Functions

All these functions are called by the Button B "on click" event handler as seen in the previous step.

These functions are pretty straight forward and by this time you should be getting familiar with the intuitive and narrative nature of the code created by a combination of aptly named variables and the visual attributes of the Blocks Editor.

  1. The showWin() function displays a message that indicates that the player has won this round of challenge and increments the PLAYER_SCORE variable by 1
  2. Similarly, the showLose() function displays the the player made a bad guess and awards a point to the BIT by incrementing the BIT_SCORE variable by 1
  3. The showBadGuess() function displays a large X on the screen indicating that one of the players made a bad guess (of either 0 or greater than 6) - the bad player in this case loses a point, and the other scores a point!

And lastly, the announceWinner() function performs the task of announcing who the grand winner is after 3 rounds of challenge/response followed by:

  • Resetting the PLAYER_SCORE and BIT_SCORE score variables
  • Pausing briefly, and starting a new game by calling the showMessage() startup function discussed in the previous step (6) of this IBLE

Finally, on to the simulator and the code!

Step 10: The Simulator and Code

The code is running in the online simulator located here

  • The simulator is located on the bottom left of the page
  • Alternately, the Download button can be used to download the code in case you have a BBC Microbit handy
  • As mentioned at the beginning of the IBLE, you do not need a BBC Microbit to build the code in the Blocks Editor or to simulate and test your code, although that does not work for curious individuals such as myself!

A Note on the Edit Button

  • If you've observed, the code is in the public domain and there is a way to edit it by using the Edit button on the top-right
  • However, I would sincerely request that you make a copy of this code and change it to suit your needs so that the original version is preserved in the interest of this IBLE

Thanks! And happy coding!!! :-)