Migrate docs from the other repo
This commit is contained in:
171
docs/lessons/die-roll/activity.md
Normal file
171
docs/lessons/die-roll/activity.md
Normal file
@@ -0,0 +1,171 @@
|
||||
# die roll activity
|
||||
|
||||
Create a die on the micro:bit
|
||||
|
||||
### ~avatar avatar
|
||||
|
||||
### @video td/videos/die-roll-0
|
||||
|
||||
Welcome! This tutorial will help you create a die. Let's get started!
|
||||
|
||||
### ~
|
||||
|
||||
Let's create a condition for when the micro:bit is shaken.
|
||||
|
||||
|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
|
||||
})
|
||||
```
|
||||
|
||||
We need to show a random value from 1 to 6 on our die. So let's make a local variable called **roll**.
|
||||
|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let roll = Math.random(6)
|
||||
})
|
||||
```
|
||||
|
||||
We need a condition for if **roll** is 5. We will show a `6` if **roll** is 5 because **roll** has a range from 0 to 5. We can use `show LEDs` to display the side of a die that shows 6.
|
||||
|
||||
|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let roll = Math.random(6);
|
||||
if (roll == 5) {
|
||||
basic.showLeds(`
|
||||
. # . # .
|
||||
. . . . .
|
||||
. # . # .
|
||||
. . . . .
|
||||
. # . # .`);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
Let's use an `else if` condition for if **roll** is 4. If **roll** is 4 we can show 5 dots on the die.
|
||||
|
||||
|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake, ()=> {
|
||||
let roll = Math.random(6);
|
||||
if (roll == 5) {
|
||||
basic.showLeds(`
|
||||
. # . # .
|
||||
. . . . .
|
||||
. # . # .
|
||||
. . . . .
|
||||
. # . # .`);
|
||||
}
|
||||
else if (roll == 4) {
|
||||
basic.showLeds(`
|
||||
. . . . .
|
||||
. # . # .
|
||||
. . # . .
|
||||
. # . # .
|
||||
. . . . .`);
|
||||
}
|
||||
});
|
||||
|
||||
```
|
||||
|
||||
|
||||
Now we need to repeat the same steps for if **roll** is 3. If **roll** is 3 we will show `4` on the die.
|
||||
|
||||
|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let roll = Math.random(6);
|
||||
if (roll == 5) {
|
||||
basic.showLeds(`
|
||||
. # . # .
|
||||
. . . . .
|
||||
. # . # .
|
||||
. . . . .
|
||||
. # . # .`);
|
||||
}
|
||||
else if (roll == 4) {
|
||||
basic.showLeds(`
|
||||
. . . . .
|
||||
. # . # .
|
||||
. . # . .
|
||||
. # . # .
|
||||
. . . . .`);
|
||||
}
|
||||
else if (roll == 3) {
|
||||
basic.showLeds(`
|
||||
. . . . .
|
||||
. # . # .
|
||||
. . . . .
|
||||
. # . # .
|
||||
. . . . .`);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
Let's also repeat these steps to show the 3, 2, and 1 on the die. We are almost done with our die!
|
||||
|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let roll = Math.random(6);
|
||||
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(`
|
||||
. . . . .
|
||||
. . . . .
|
||||
. . # . .
|
||||
. . . . .
|
||||
. . . . .`);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
### ~avatar avatar
|
||||
|
||||
Excellent, you're ready to continue with the [challenges](/microbit/lessons/die-roll/challenges)!
|
||||
|
||||
### ~
|
||||
|
183
docs/lessons/die-roll/challenges.md
Normal file
183
docs/lessons/die-roll/challenges.md
Normal file
@@ -0,0 +1,183 @@
|
||||
# die roll challenges
|
||||
|
||||
Create a die on the micro:bit.
|
||||
|
||||
## Before we get started
|
||||
|
||||
Complete the following [guided tutorial](/microbit/lessons/die-roll/activity), your code should look like this:
|
||||
|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let roll = Math.random(6);
|
||||
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(`
|
||||
. . . . .
|
||||
. . . . .
|
||||
. . # . .
|
||||
. . . . .
|
||||
. . . . .`);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### Challenge 1
|
||||
|
||||
Modify the line of code with `pick random` so that only number 1-4 can appear on the die.
|
||||
|
||||
|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let roll = Math.random(4);
|
||||
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(`
|
||||
. . . . .
|
||||
. . . . .
|
||||
. . # . .
|
||||
. . . . .
|
||||
. . . . .`);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### Challenge 2
|
||||
|
||||
Let's make a trick die! Modify the line of code with `pick random` so that only numbers 3-6 can appear on the die. Also note that we need to ensure `roll = 0` when only 1 dot is shown on the BBC micro:bit.
|
||||
|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let roll = Math.random(4) + 2;
|
||||
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(`
|
||||
. . . . .
|
||||
. . . . .
|
||||
. . # . .
|
||||
. . . . .
|
||||
. . . . .`);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### Challenge 3
|
||||
|
||||
Add a couple more conditions so that the BBC micro:bit randomly chooses a number between 1 and 8.
|
||||
|
Reference in New Issue
Block a user