Image of micro:bit and dice

micro:bit Tutorial – How to Make a Two-player Dice Game

Description

This project shows how to create a two-player dice game using micro:bits. When the micro:bits get shaken, they generate a random number between 1 and 6. The two micro:bits then signal each other the value of their dice roll and each micro:bit determines if they won, lost, or if it was a draw.

Programming Language

MakeCode Blocks

Difficulty Level

Intermediate

Age Range

11 and up

Materials

This can be done using the Makecode simulator. If you are using micro:bits, it takes two, along with a power source for each:

Programming the micro:bit

Develop the problem statement

Defining the problem in a few short sentences is important because it puts  boundaries on what you’re developing. It helps guide developing the algorithm and the code. Here is the problem statement for this project:

Write code that mimics rolling dice by shaking micro:bits. Allow two to communicate their rolls and, on each die, show the other person’s roll and then show an emoji to signify win lose or draw for each player.

Write the algorithm

The behavior of both dice is identical so there only needs to be one algorithm:

  • To Start:
    • Make sure player 1 and player 2 can communicate their rolls
  • When a micro:bit is shaken
    • Display a number between 1 and 6
    •  Tell the other micro:bit your score
    • If your score is higher
      • Display a smiley face
    • If their score is higher
      • Display a frowny face
    • Else
      • Display a neutral face

Write the pseudocode

The pseudocode is written taking into account the language being used and the need for event handling. It also takes into account the need to connect two micro:bits:

  • On Start
    • Set radio connection between two micro:bits
    • Create variable to store local value (local_val)
  •  On shake
    • Set local variable to random number between 1 and 6
    • Show number
    • Send number to other micro:bit
  • On Number Received
    • If local_val > Number Received
      • Show smiley icon
    • Else if local_val < Number Received
      • Show frowny icon
    • Else
      • Show squiggle icon

Write and test the code

Here is a link to the code I wrote:

Two Player Dice Game

Academic Connections

Programming Concepts

  • Variables
  • Logical Comparison
  • Conditional Logic
  • Communication
  • Event handling

NGSS Concepts

  • MS-PS4-3: Use digital devices to encode and transmit information.
    The shake motion is detected by the accelerometer and interpreted as an input signal to roll the dice.

  • MS-ETS1-1: Define the criteria and constraints of a design problem.
    Students determine what counts as a valid shake (strength, duration) and how to avoid accidental triggers.

  • MS-ETS1-2: Evaluate competing design solutions.

  • HS-PS4-2: Evaluate how digital systems encode and process information.
    Students examine how accelerometer data is processed and translated into a program event.

  • HS-ETS1-2: Design a solution to a complex problem by breaking it down into smaller parts.
    Students separate sensing, filtering, random number generation, and game logic.