When two people play rock, paper scissors, the players shake their fists, then choose a random choice between rock, paper or scissors. Then depending on the players choices, There is someone who wins (the other loses), or the game is a tie.
How could we code a computer to play against a human? Should they choose the same choice over and over every game? Should they loop the choices in a pattern?
Or could there be a way to make them un-predictable?
To begin our game, we shake our fists. Our micro-bit will start it’s code in the same way. In Input you will find the “On [Shake]” start block. This block uses the microbit’s accelerometer to detect if the microbit is shaking, and then run code when it is.
Next we will need the computer to make an, unpredictable random choice between 3 options, and save it for later. Here we will need to make a variable. So select Variable, make a variable and name it something useful like “Choice”, this is where the microbit will choose it’s move for our game.
Then drag out the “set [choice] to ( )” block, and place it inside the “on [shake] block”.
Next we want to set our choice to something random.
In Math drag out the “pick random ( ) to ( )” block and place it inside the circular slot on the right of the “set [choice] to ( )” block. Set the range from 1 to 3 (So we have the options, 1, 2, 3).
We want our microbit to make a decision. To make choices in code, we need to ask the question:
“What IF the choice is 1” THEN the Microbit selects Rock.
In code we write this as:
IF choice = 1 THEN
Show LED’s Rock
We will need two blocks from logic. The “IF <> then” block and the comparison “( ) = ( )” block. Place these under the set choice block.
You will need to fit the “( ) = ( )” block into the hexagonal hole in the “IF < > then” block.
Then go to Variables and take the rounded “choice” variable block and place it into the left side of the “=” comparison block, on the other side type 1. Then go to basic and drag out the Show LED’s block, and then draw a picture of a rock.
Do the same for the other 2 options (Scissors and Paper), and place those beneath the IF block.
After you have done this we want our game to reset after some time. So go to Basic and add a pause block after that last IF block, and then add a show LED’s block also from Basic, and set it to no LED’s.
This will work. However, it isn’t the best code we can have. We make 3 comparisons one after the other. Choice = 1 then Choice = 2 then Choice = 3.
Wouldn’t it be easier to our microbit to not have to think about so many comparisons?
Here is where the IF / ELSE block will become useful.
The difference between the IF and the IF/ELSE block is that the IF only does something when the “condition” is true, like IF “I clean my room” Then I get to go out with friends. Nothing happens if the condition is false (When I don’t clean my room, nothing happens).
The IF / ELSE block would have two options. IF “I clean my room” THEN I get to go out with friends. or ELSE I will be grounded.
We can use these IF / Else block to make branching choices.
For our case.
IF my choice is 1, then my micro-bit chooses Rock. ELSE my choice must be 2 or 3. Then I should check IF my choice is 2 then it is Paper ELSE choose scissors (If it isn’t 1 or 2 then it must be 3 no need to check it).
Try modifying your code to use two IF / ELSE Block instead. Take out your comparisons, and replace the IF blocks with two IF / Else blocks. Your second IF / ELSE block should be inside the bottom half of the first IF / ELSE block (“Nested” inside).