2016-05-27 00:24:10 +02:00
# dice roll challenges
2016-03-26 00:47:20 +01:00
2016-11-02 01:44:37 +01:00
Create a dice on the @boardname @.
2016-03-26 00:47:20 +01:00
## Before we get started
2016-05-27 00:24:10 +02:00
Complete the following [guided tutorial ](/lessons/dice-roll/activity ), your code should look like this:
2016-03-26 00:47:20 +01:00
```blocks
input.onGesture(Gesture.Shake, () => {
2018-04-21 19:52:09 +02:00
let roll = Math.randomInt(6);
2016-03-26 00:47:20 +01:00
if (roll == 5) {
basic.showLeds(`
. # . # .
. . . . .
. # . # .
. . . . .
. # . # .`);
}
else if (roll == 4) {
basic.showLeds(`
. . . . .
. # . # .
. . # . .
. # . # .
. . . . .`);
}
else if (roll == 3) {
basic.showLeds(`
. . . . .
. # . # .
. . . . .
. # . # .
. . . . .`);
}
else if (roll == 2) {
basic.showLeds(`
# . . . .
. . . . .
. . # . .
. . . . .
. . . . #`);
}
else if (roll == 1) {
basic.showLeds(`
. . . . .
. # . . .
. . . . .
. . . # .
. . . . .`);
}
else {
basic.showLeds(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .`);
}
});
```
2017-09-07 22:42:08 +02:00
## Challenge 1
2016-03-26 00:47:20 +01:00
2016-05-27 00:24:10 +02:00
Modify the line of code with `pick random` so that only number 1-4 can appear on the dice.
2016-03-26 00:47:20 +01:00
```blocks
input.onGesture(Gesture.Shake, () => {
2018-04-21 19:52:09 +02:00
let roll = Math.randomInt(4);
2016-03-26 00:47:20 +01:00
if (roll == 5) {
basic.showLeds(`
. # . # .
. . . . .
. # . # .
. . . . .
. # . # .`);
}
else if (roll == 4) {
basic.showLeds(`
. . . . .
. # . # .
. . # . .
. # . # .
. . . . .`);
}
else if (roll == 3) {
basic.showLeds(`
. . . . .
. # . # .
. . . . .
. # . # .
. . . . .`);
}
else if (roll == 2) {
basic.showLeds(`
# . . . .
. . . . .
. . # . .
. . . . .
. . . . #`);
}
else if (roll == 1) {
basic.showLeds(`
. . . . .
. # . . .
. . . . .
. . . # .
. . . . .`);
}
else {
basic.showLeds(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .`);
}
});
```
2017-09-07 22:42:08 +02:00
## Challenge 2
2016-03-26 00:47:20 +01:00
2016-11-01 18:42:42 +01:00
Let's make a trick dice! Modify the line of code with `pick random` so that only numbers 3-6 can appear on the dice. Also note that we need to ensure `roll = 0` when only 1 dot is shown on the @boardname @.
2016-03-26 00:47:20 +01:00
```blocks
input.onGesture(Gesture.Shake, () => {
2018-04-21 19:52:09 +02:00
let roll = Math.randomInt(4) + 2;
2016-03-26 00:47:20 +01:00
if (roll == 5) {
basic.showLeds(`
. # . # .
. . . . .
. # . # .
. . . . .
. # . # .`);
}
else if (roll == 4) {
basic.showLeds(`
. . . . .
. # . # .
. . # . .
. # . # .
. . . . .`);
}
else if (roll == 3) {
basic.showLeds(`
. . . . .
. # . # .
. . . . .
. # . # .
. . . . .`);
}
else if (roll == 2) {
basic.showLeds(`
# . . . .
. . . . .
. . # . .
. . . . .
. . . . #`);
}
else if (roll == 1) {
basic.showLeds(`
. . . . .
. # . . .
. . . . .
. . . # .
. . . . .`);
}
else {
basic.showLeds(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .`);
}
});
```
2017-09-07 22:42:08 +02:00
## Challenge 3
2016-03-26 00:47:20 +01:00
2016-11-01 18:42:42 +01:00
Add a couple more conditions so that the @boardname @ randomly chooses a number between 1 and 8.
2016-03-26 00:47:20 +01:00