pxt-calliope/docs/lessons/rock-paper-scissors/challenges.md

134 lines
2.8 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# rock paper scissors challenges
Coding challenges for rock paper scissors.
## Before we get started
2016-04-13 17:27:45 +02:00
Complete the following [guided activity](/lessons/rock-paper-scissors/activity) , your code should look like this:
2016-03-26 00:47:20 +01:00
```blocks
input.onGesture(Gesture.Shake, () => {
let img = Math.random(3)
if (img == 2) {
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
} else if (img == 1) {
basic.showLeds(`
. . . . .
. # # # .
. # # # .
. # # # .
. . . . .
`)
} else {
basic.showLeds(`
. . . # #
# # . # .
. . # . .
# # . # .
. . . # #
`)
}
})
```
### Challenge 1
When the A button is pressed, increment the score by 1. You can select Game drawer then add change score by 1.
```blocks
input.onGesture(Gesture.Shake, () => {
let img = Math.random(2)
if (img == 2) {
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
} else if (img == 1) {
basic.showLeds(`
. . . . .
. # # # .
. # # # .
. # # # .
. . . . .
`)
} else {
basic.showLeds(`
. . . # #
# # . # .
. . # . .
# # . # .
. . . # #
`)
}
})
input.onButtonPressed(Button.A, () => {
game.addScore(1)
})
```
* Click *run* to execute your code in the simulator
### Challenge 2
After incrementing the score, display the total number of wins you have.
```blocks
input.onGesture(Gesture.Shake, () => {
let img = Math.random(2)
if (img == 2) {
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
} else if (img == 1) {
basic.showLeds(`
. . . . .
. # # # .
. # # # .
. # # # .
. . . . .
`)
} else {
basic.showLeds(`
. . . # #
# # . # .
. . # . .
# # . # .
. . . # #
`)
}
})
input.onButtonPressed(Button.A, () => {
game.addScore(1)
basic.showString("WINS:")
basic.showNumber(game.score())
})
```
* Run and compile the code to see if it works as expected.
### Challenge 3
You have successfully tracked and displayed the number of wins on the micro:bit! However, what about losses? Use the Game drawer to change score by -1 when button `B` is pressed.
* Run and compile the code to see if it works as expected.