2017-09-11 10:49:40 -07:00
# Rock Paper Scissors
2016-05-26 09:15:10 -07:00
2017-09-11 10:49:40 -07:00
## Step 1
2016-05-26 09:15:10 -07:00
2016-11-01 17:44:37 -07:00
We want the @boardname @ to choose rock, paper, or scissors when you shake it.
2017-09-11 10:49:40 -07:00
Place a ``||input:on shake||` ` block so when you shake the @boardname @, it will run part of a program.
2016-05-26 09:15:10 -07:00
```blocks
input.onGesture(Gesture.Shake, () => {
})
```
2017-09-11 10:49:40 -07:00
## Step 2
Add a ``weapon`` variable to store a random number computed with ``||math:pick random||` `.
When you shake the @boardname @, it should pick a random number from `0` to `2`
2016-05-31 17:17:36 -07:00
and store it in the variable `weapon` . (This variable is named `weapon` because
rock, paper, and scissors are the weapons you use to battle your friends!)
2016-05-26 16:18:33 -07:00
2016-05-26 09:15:10 -07:00
```blocks
2017-09-11 10:49:40 -07:00
let weapon = 0;
2016-05-26 09:15:10 -07:00
input.onGesture(Gesture.Shake, () => {
2017-09-11 10:49:40 -07:00
weapon = Math.random(3)
2016-05-26 09:15:10 -07:00
})
```
2017-11-12 18:43:39 -08:00
In a later step, each of the possible numbers (`0` , `1` , or `2` ) is matched to its own picture. The picture is shown on the LEDs when its number is picked.
2016-05-26 16:18:33 -07:00
2017-09-11 10:49:40 -07:00
## Step 3
Place an ``if`` block under the ``||math:pick random||` ` and
check whether ``weapon`` is equal to ``0` `.
2016-05-26 16:18:33 -07:00
2017-09-11 10:49:40 -07:00
```blocks
let weapon = 0;
input.onGesture(Gesture.Shake, () => {
let weapon = Math.random(3)
if (weapon == 0) {
}
})
```
2016-05-26 16:18:33 -07:00
2017-09-11 10:49:40 -07:00
## Step 4
In the ``if`` block, place a ``||basic:show leds||` ` block that shows a
picture of a piece of paper.
2016-05-26 09:15:10 -07:00
```blocks
2017-09-11 10:49:40 -07:00
let weapon = 0;
2016-05-26 09:15:10 -07:00
input.onGesture(Gesture.Shake, () => {
2016-05-26 16:18:33 -07:00
let weapon = Math.random(3)
if (weapon == 0) {
2016-05-26 09:15:10 -07:00
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
}
})
```
2017-09-11 10:49:40 -07:00
## Step 5
2016-05-26 09:15:10 -07:00
2017-09-11 10:49:40 -07:00
Add an ``else if`` block to the ``if`` block and check whether ``weapon` `
is equal to ``1` `.
2016-05-26 09:15:10 -07:00
2017-09-11 10:49:40 -07:00
Click on the gearwheel icon to open up the ``if`` editor; then drag and drop an ``else if`` block in the ``if` ` editor.
2016-05-26 09:15:10 -07:00
```blocks
2017-09-11 10:49:40 -07:00
let weapon = 0;
2016-05-26 09:15:10 -07:00
input.onGesture(Gesture.Shake, () => {
2016-05-26 16:18:33 -07:00
let weapon = Math.random(3)
if (weapon == 0) {
2016-05-26 09:15:10 -07:00
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
2017-09-11 10:49:40 -07:00
} else if (weapon == 1) {
}
})
```
## Step 6
2016-05-26 09:15:10 -07:00
2017-09-11 10:49:40 -07:00
Place a ``||basic:show leds||` ` block under the else if and draw a **rock** image on the screen.
```blocks
let weapon = 0;
input.onGesture(Gesture.Shake, () => {
let weapon = Math.random(3)
if (weapon == 0) {
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
2016-05-26 16:18:33 -07:00
} else if (weapon == 1) {
2016-05-26 09:15:10 -07:00
basic.showLeds(`
. . . . .
. # # # .
. # # # .
. # # # .
. . . . .
`)
}
})
```
2017-09-11 10:49:40 -07:00
## Step 7
2016-05-26 09:15:10 -07:00
2017-09-11 10:49:40 -07:00
Add a ``||basic:show leds||`` block with a picture of scissors to the ``else` ` part.
You don't need to check if `weapon` is `2` because `2` is the only number left out of `0` , `1` , and `2` .
That's why you can use an ``else`` instead of an ``else if` `.
2016-05-26 09:15:10 -07:00
```blocks
2017-09-11 10:49:40 -07:00
let weapon = 0;
2016-05-26 09:15:10 -07:00
input.onGesture(Gesture.Shake, () => {
2016-05-26 16:18:33 -07:00
let weapon = Math.random(3)
if (weapon == 0) {
2016-05-26 09:15:10 -07:00
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
2016-05-26 16:18:33 -07:00
} else if (weapon == 1) {
2016-05-26 09:15:10 -07:00
basic.showLeds(`
. . . . .
. # # # .
. # # # .
. # # # .
. . . . .
`)
} else {
basic.showLeds(`
2016-05-26 15:10:46 -07:00
# # . . #
2016-05-26 09:15:10 -07:00
# # . # .
. . # . .
# # . # .
2016-05-26 15:10:46 -07:00
# # . . #
2016-05-26 09:15:10 -07:00
`)
}
})
```
2017-09-11 10:49:40 -07:00
## Step 8
2016-06-25 17:22:50 -04:00
2017-09-11 10:49:40 -07:00
Your game is ready! Gather your friends and play Rock Paper Scissors!