 Are you looking for an easy project to code your BBC microbit? Rock, paper, scissors! You got me this time, microbit. Stick around for another tutorial about microbit basics series and we'll use Maco to program this game together. Coming up! Hello world, it's The Surfing Scratcher here, teacher, surfer, programmer and lover of learning, bringing you the goodness of learning through code. On this channel we code projects together using video tutorials, so if you're new around here, consider hitting that subscribe button. Be sure to check out the show notes and links in the description below as I've listed a bunch of unplugged activities and resources that can help you along in your learning journey. Alright, let's get stuck into it. Here we are in Microsoft Maco, let's go ahead and give our project a name. Let's create the three shapes that we need for our rock, paper and scissors game. You can go ahead and do that in the base and get our show leads, but there's also another way that we can do this. If we go down to the advanced tab here, we can click down and there's also an images category. Let's go inside that images category and grab a create image. So we can go ahead and draw our images on this block right here. Let's go draw a rock. Yeah, that rocks. I'm going to right click and duplicate. If you click and drag these squares here, they will turn on off, so you don't have to click them individually. Let's go ahead and create our paper and duplicate. Let's go ahead and create our pair of scissors. Cool. So we've got our three images here. Now we need to set them to variable. So let's go ahead and do that. So let's make a variable and let's call one rock image and then I'm going to set rock image on start to our rock. So now rock image is equal to this image. Alright, let's go ahead and create our paper image. Let's set paper image to our piece of paper. Now let's rinse and repeat for our scissors. Alright, I'm just going to zoom out so we can see everything that's going on here. So let's get rid of our four ever block. So when we start this program, we are setting our first three images. Okay, so what we're going to go ahead and do now is set up our shake. So we know that there are three options. So why don't we on shake pick a random number between one, two and three, between one and three. So let's go do that. Jump into our math. Pick a random number in one and three. Cool. So that picks a random number, but we need to assign that to something. So what we'll need to do now is just create another variable and let's call it my hand. And this refers to our hand if we were actually playing this game with our hand. We can set the value of my hand to that random number between one and three. Let's just see how that's going. Go over to the basic category, show number, we're going to show my hand. We go over to our simulated micro bit here. We can press this shake button to simulate a shake. We see every time we press it, we're getting a number here between one and three. One, two, let's do it until we get three. There's our number three. Cool. Let's get rid of that block. Now what we now want to do is display the corresponding LED. So if it is a one, let's display the rock, two, let's display the paper image and a three, let's display the scissors image. Here we need some logic. Let's grab a new statement. And we want to create a statement that is true. So for our rock to be true, we want my hand to be equal to one. It could be any number, but because we've got this order here, let's just make one be the rock. Let's go back over to our logic and let's get a comparison here. We want to compare what the current value of my hand is. My hand is equal to one. Then we want to show the rock. So we need to scroll back down into our images. We're going to show the image, rock image. Cool. So the last thing we need to is connect that if statement back into our own shake. We press our shake and we get a number one. Our rock will appear. Perfect. Let's go ahead and set up the last two results. So I'm just going to duplicate that. If my hand is equal to two, let's make it the paper image and duplicate one more time. It's equal to three. I'm going to do the scissors image. Cool. You could use if and else statements here, but I kind of like just using explicit if statements for the exact numbers. I'm going to press our shake button here. We've got a rock. Press that again. We've got some scissors. And again, we've got some scissors and there's our piece of paper. All right. I get a bit nervous when we just use numbers free willy-nilly like this. So I'm going to go ahead and change these images here so that they belong to a variable. I'll show what I mean. So let's go over to our variables and I'm going to make a new variable. I'm going to call this rock index. I'm going to set the rock index just like when we set the rock image. I'm going to set the rock index to one. The reason I'm setting it to one is because over here, we have said that when my hand is equal to one, we want to show the rock image. So if I set this rock index to one, well, when my hand is equal to this rock index, then we'll show the rock image. Let's go and just do that now. I'm going to go grab that rock index out and place it in there. So when I read this to me, this is a little bit more human friendly. When my hand is equal to the rock index, which is just the number one, then let's show the rock image. I'm going to go ahead and create variables for the other two scenarios. Now the word index is just another name for an integer that we're using sort of programming a number. Let's go ahead and duplicate our variables here so we can cover all scenarios. Paper index number two, scissor index number three. Cool. This will do the exact same thing. So we press the shake, we get the rock, get in the paper and we'll keep pressing it until we get some scissors. So there were some scissors in there. Awesome. Okay, we might want to set up our rock paper scissors game so we can play against another micro bit. We want to do some more work in our on shake method here. So I recommend that we take the logic that we've just created and place it into a new function. So we're going to refactor this piece of code. Refactoring just means like reorganizing it just to create a little bit more order. Again, so it's more human friendly and more readable. So I want to head over to functions here. I'm going to make a function. A good way to name your function is to use a verb, we'll use pick and then a noun. So pick shape. Okay, scroll over. Here is our function. You'll see it's a little bit like a hack block here. So nothing can go to the top of it. Then we're going to put all of that code that we've just created over here and stick that inside of our function. And the last thing we want to do is head back over to our functions and we want to call it. So all I've done essentially is just group all of that code that we just did. And I've given it a name and I've said when we call it, we're going to pick a shape. And that's exactly what this function does. So if I go ahead and press the shake button, the exact same thing will happen. There's our rock, there's our scissors. And if I press it often enough, our paper will come back up as well. There it is. The last thing we want to do is right click and format our code. It's time for a Cody question. And we're going to do things a little differently this week. You're going to play rock, paper, scissors against the micro bit and use the comment section below to tell me how you went. Are you ready? Rock, paper, scissors. Hey, thanks for checking out this tutorial. Like, subscribe, ring that bell if you're new around here and be sure to check out some of my other content which is on the screen right now. If you can't get enough then, head on over to my Patreon page where you can join a membership tier for access to exclusive content. Link in the description below. But until then, I'm off to go find a way. I'll catch you in the next one.