Rock! Paper! Scissors! Radio!

Team up with a friend and play Rock, Paper, Scissors - using radio! Each of you picks rock, paper or scissors on your own micro:bit, it sends your choice to the other micro:bit, and then both micro:bits show who won. You’ll need two micro:bits - one each.

Important! Before you get started, make sure your micro:bit is connected to your computer: Connect your micro:bit


📻 Step 1: Pick a radio group

micro:bits can only hear each other if they’re in the same radio group, which is a number between 0 and 255. Agree on a number with your partner - if other pairs nearby are playing too, make sure each pair picks a different number, or your games will get mixed up!

let other = 0
let choice = 0
radio.setGroup(1)
choice = 0
other = 0

Make two variables: choice, to remember what you picked, and other, to remember what the other micro:bit picked. Set them both to 0 when the micro:bit starts - 0 means “nothing picked yet”.


✊✋✌️ Step 2: Pick Rock, Paper or Scissors

Each choice gets its own button, and its own number:

When a button is pressed, we store the number in choice and show the matching picture:

function send_choice () {} // @hide
input.onButtonPressed(Button.A, function () {
    choice = 1
    basic.showLeds(`
        . . . . .
        . # # # .
        . # # # .
        . # # # .
        . . . . .
        `)
    send_choice()
})
function send_choice () {} // @hide
input.onButtonPressed(Button.B, function () {
    choice = 2
    basic.showLeds(`
        # # # # #
        # . . . #
        # . . . #
        # . . . #
        # # # # #
        `)
    send_choice()
})
function send_choice () {} // @hide
input.onButtonPressed(Button.AB, function () {
    choice = 3
    basic.showIcon(IconNames.Scissors)
    send_choice()
})

Did you spot the call send_choice block at the end of each one? That’s a function - a set of blocks with a name, which we can use again and again. All three buttons need to send their choice to the other micro:bit in the same way, so we’ll write those blocks once, in a function, rather than three times. Let’s write it next…


📤 Step 3: Send your choice to the other micro:bit

Click Advanced, then Functions, then Make a Function…, and call it send_choice. Then add these blocks inside it:

function check_choices () {} // @hide
function send_choice () {
    radio.sendValue("other", choice)
    check_choices()
}

This sends a message over the radio with the name “other” and your choice as its value. After that, it calls another function, check_choices, to see whether anyone has won yet. Don’t worry that it doesn’t exist yet - we’ll write it in Step 5.


📥 Step 4: Receive the other micro:bit’s choice

When a message arrives over the radio, we store its value in other, then check whether anyone has won:

function check_choices () {} // @hide
radio.onReceivedValue(function (name, value) {
    other = value
    check_choices()
})

Notice that we call check_choices both when we send our choice and when we receive the other one. That’s because we don’t know which player will press their button first!


🏆 Step 5: Find out who won

Now for the clever bit! Make a function called check_choices, and add these blocks to it:

function check_choices () {
    if (choice == 0 || other == 0) {
        return
    } else if (choice == other) {
        basic.showIcon(IconNames.No)
    } else if (choice == other + 1) {
        basic.showIcon(IconNames.Happy)
    } else if (choice == 1 && other == 3) {
        basic.showIcon(IconNames.Happy)
    } else {
        basic.showIcon(IconNames.Sad)
    }
    choice = 0
    other = 0
}

Here’s how it works:

Finally, we set both variables back to 0, ready for the next round. Download your code onto both micro:bits and challenge your friend!


🤔 Challenges