This is an assignment for students to come up with a micro:bit program that uses Boolean variables, Boolean operators, and possibly the random function.
## Input
Remind the students of all the different inputs available to them through the micro:bit.
When you shake the micro:bit, it reports the current temperature in degrees Fahrenheit. Button B measures the light level and if it is above 70 degrees AND very bright, it will display a sun icon. If it is above 70 degrees and less bright, it will display a cloudy symbol. If it is dark, it will display a nighttime icon.
if (128 > input.lightLevel() && 0 <input.lightLevel()&&input.temperature()> 22) {
basic.showAnimation(`
. # . # .
# . . . #
. . . . .
. . . . .
. . . . .
`, 500)
basic.showAnimation(`
. # # # .
# . . . #
. . . . .
. . . . .
. . . . .
`, 500)
basic.showAnimation(`
. # # # .
# . . . #
. # . # .
. . . . .
. . . . .
`, 500)
basic.showAnimation(`
. # # # .
# . . . #
. # # # .
. . . . .
. . . . .
`, 500)
basic.showAnimation(`
. . . . .
. # . # .
. . . . .
. # # # .
# . . . #
`, 500)
} else {
basic.showAnimation(`
# . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`, 500)
basic.showAnimation(`
. # . . .
# # . . .
. . . . .
. . . . .
. . . . .
`, 500)
basic.showAnimation(`
. . # . .
. . # . .
# # # . .
. . . . .
. . . . .
`, 500)
basic.showAnimation(`
. . # # .
. . # . .
# # # . .
# . . # .
. . . . .
`, 500)
basic.showAnimation(`
. . # # #
. . # . .
# # # . .
# . . # .
# . . . #
`, 500)
basic.showAnimation(`
. . . . .
. # . # .
. . . . .
# . . . #
. # # # .
`, 500)
}
})
input.onButtonPressed(Button.A, () => {
if (input.temperature() <22&&input.temperature()> 6) {
basic.showAnimation(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
`, 500)
basic.showAnimation(`
. . . . .
. . # . .
. # # # .
. . # . .
. . . . .
`, 500)
basic.showAnimation(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`, 500)
basic.showAnimation(`
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
`, 500)
} else {
basic.showAnimation(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
`, 500)
basic.showAnimation(`
. . . . .
. . . . .
. # # # .
. . . . .
. . . . .
`, 500)
basic.showAnimation(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`, 500)
basic.showAnimation(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`, 500)
}
})
input.onGesture(Gesture.Shake, () => {
basic.clearScreen()
basic.showNumber(input.temperature() * 2 + 32)
})
```
Button A displays an animation to tell you whether or not you should use sunscreen (on sunny or cloudy days but not at night or indoors.)
Make a holder that can hold the micro:bit and a bottle of sunscreen.
This example uses boolean operations because both light level AND temperature must be high in order to trigger the sun icon:
```block
if (128 > input.lightLevel() && 0 <input.lightLevel()&&input.temperature()> 22) {}
```
### Two-player game
Create a game in which two players take turns on the same micro:bit. You can use a boolean variable called PlayerATurn to keep track of whose turn it is.
**Board Game:** Use boolean variables and random values as part of a board game (or improve your Board Game from the Variables lesson). Make the board and pieces and a holder for the micro:bit. Try modding a current board game.
![Two player game project](/static/courses/csintro/booleans/two-player-game.png)
Board Game with Arrows
#### Board game arrow code
```blocks
let player1Turn = false
let spin = 0
let delay = 0
input.onGesture(Gesture.Shake, () => {
if (player1Turn == true && Math.random(4) <3){
basic.clearScreen()
delay = 0
while (delay <500){
basic.showLeds(`
. . # . .
. # # . .
# # # # .
. # # . .
. . # . .
`)
delay += 50
basic.pause(delay)
basic.showLeds(`
. . # . .
. # # # .
# # # # #
. . # . .
. . . . .
`)
delay += 50
basic.pause(delay)
basic.showLeds(`
. . # . .
. . # # .
. # # # #
. . # # .
. . # . .
`)
delay += 50
basic.pause(delay)
basic.showLeds(`
. . . . .
. . # . .
# # # # #
. # # # .
. . # . .
`)
basic.pause(delay)
delay += 50
player1Turn = false
}
} else if (player1Turn) {
basic.showString("Crash!")
player1Turn = false
} else if (Math.random(4) <3){
basic.clearScreen()
delay = 0
while (delay <500){
basic.showLeds(`
. . # . .
. # # . #
# # # # #
. # # . #
. . # . .
`)
delay += 50
basic.pause(delay)
basic.showLeds(`
. . # . .
. # # # .
# # # # #
. . # . .
. # # # .
`)
delay += 50
basic.pause(delay)
basic.showLeds(`
. . # . .
# . # # .
# # # # #
# . # # .
. . # . .
`)
delay += 50
basic.pause(delay)
basic.showLeds(`
. # # # .
. . # . .
# # # # #
. # # # .
. . # . .
`)
basic.pause(delay)
delay += 50
player1Turn = true
}
} else {
basic.showString("Crash!")
player1Turn = true
}
})
basic.forever(() => {
})
delay = 0
spin = 0
player1Turn = true
```
This is an example of a board game in which the micro:bit displays an arrow pointing in a random direction. The paper legend indicates different actions the player must take.
Here is a portion of the board game's code. A boolean variable is used to determine whose turn it is. If player1Turn is false, then it's player 2's turn. A random number is generated to show the arrow seventy-five percent of the time (for values of 0, 1, or 2).