Add 'Booleans' lesson to csintro. (#433)
This commit is contained in:
parent
245612e8f3
commit
90d0d9a1ed
@ -96,6 +96,12 @@
|
||||
* [Activity](/courses/csintro/coordinates/activity)
|
||||
* [Project](/courses/csintro/coordinates/project)
|
||||
* [Standards](/courses/csintro/coordinates/standards)
|
||||
* [Booleans](/courses/csintro/booleans)
|
||||
* [Overview](/courses/csintro/booleans/overview)
|
||||
* [Unplugged](/courses/csintro/booleans/unplugged)
|
||||
* [Activity](/courses/csintro/booleans/activity)
|
||||
* [Project](/courses/csintro/booleans/project)
|
||||
* [Standards](/courses/csintro/booleans/standards)
|
||||
|
||||
## #reference
|
||||
|
||||
|
@ -38,7 +38,7 @@ UNDER CONSTRUCTION: We are still migrating the CSIntro content to this format...
|
||||
5. [Iteration](/courses/csintro/iteration)
|
||||
6. [Review/Mini-Project](/courses/csintro/miniproject)
|
||||
7. [Coordinate Grid System](/courses/csintro/coordinates)
|
||||
8. Booleans
|
||||
8. [Booleans](/courses/csintro/booleans)
|
||||
9. Music and Arrays
|
||||
10. Bits, Bytes, and Binary
|
||||
11. Radio
|
||||
|
32
docs/courses/csintro/booleans.md
Normal file
32
docs/courses/csintro/booleans.md
Normal file
@ -0,0 +1,32 @@
|
||||
# Booleans
|
||||
|
||||
This lesson introduces the use of the boolean data type to control the flow of a program, keep track of state, and to include or exclude certain conditions.
|
||||
|
||||
![Picture and quote of Shakespeare](/static/courses/csintro/booleans/shakespeare.png)
|
||||
Shakespeare knew Booleans (quote from Hamlet)
|
||||
|
||||
## Lesson Objectives
|
||||
Students will...
|
||||
* Understand what booleans and boolean operators are, and why and when to use them in a program.
|
||||
* Learn how to create a boolean, set the boolean to an initial value, and change the value of the boolean within a micro:bit program.
|
||||
* Learn how to use the random true or false block.
|
||||
* Apply the above knowledge and skills to create a unique program that uses booleans and boolean operators as an integral part of the program.
|
||||
|
||||
## Lesson Plan Structure
|
||||
* Introduction: Booleans in daily life
|
||||
* Unplugged Activity: Two Heads are Better Than One
|
||||
* Micro:bit Activity: Double Coin Flipper
|
||||
* Project: Boolean
|
||||
* Assessment: Rubric
|
||||
* Standards: Listed
|
||||
|
||||
## Lesson plan
|
||||
|
||||
1. [**Overview**: Booleans](/courses/csintro/booleans/overview)
|
||||
2. [**Unplugged**: Two heads are better than one](/courses/csintro/booleans/unplugged)
|
||||
3. [**Activity**: Double coin flipper](/courses/csintro/booleans/activity)
|
||||
4. [**Project**: Boolean](/courses/csintro/booleans/project)
|
||||
|
||||
## Related standards
|
||||
|
||||
[Targeted CSTA standards](/courses/csintro/booleans/standards)
|
209
docs/courses/csintro/booleans/activity.md
Normal file
209
docs/courses/csintro/booleans/activity.md
Normal file
@ -0,0 +1,209 @@
|
||||
# Activity: Double coin flipper
|
||||
|
||||
Guide the students to create a program using Boolean variables and operators.
|
||||
We’ll use our pseudocode from the previous activity to code a double coin flipper program.
|
||||
|
||||
For the first step, let’s create our variables.
|
||||
Make a variable for each of the following:
|
||||
* `CoinAHeads`
|
||||
* `CoinBHeads`
|
||||
* `PlayerAScore`
|
||||
* `PlayerBScore`
|
||||
|
||||
Now we need to initialize the variable values.
|
||||
Put a 'set' variable block for each of these 4 variables inside the 'on start' block.
|
||||
|
||||
The initial value of a variable is the value the variable will hold each time the program starts.
|
||||
By default:
|
||||
* a string variable is initialized to an empty string `""`
|
||||
* a number variable is initialized to `0`
|
||||
* a Boolean is initialized to `false`
|
||||
|
||||
Initialize the number variables to zero and the Boolean variables to `false`.
|
||||
|
||||
You can find the false blocks under the Logic menu.
|
||||
|
||||
![Logic menu](/static/courses/csintro/booleans/logic-menu.png)
|
||||
|
||||
```blocks
|
||||
let CoinAHeads = false
|
||||
let CoinBHeads = false
|
||||
let PlayerAScore = 0
|
||||
let PlayerBScore = 0
|
||||
basic.showLeds(`
|
||||
. # . . .
|
||||
# # # . .
|
||||
. # . # .
|
||||
. . # # #
|
||||
. . . # .
|
||||
`)
|
||||
```
|
||||
|
||||
Notice that we also added an image for the start screen, so the user knows the program has started and is ready. Does the image look like two coins?
|
||||
|
||||
## Random coin flips
|
||||
|
||||
When the player shakes the micro:bit, we will code the micro:bit to give each of our Boolean variables a random true/false value.
|
||||
|
||||
* From the Input Toolbox drawer, drag an 'on shake' block to the coding workspace
|
||||
* From the Variables Toolbox drawer, drag 2 'set' variable blocks to the coding workspace
|
||||
* Drag the 2 'set' blocks into the 'on shake' block
|
||||
* Change the default `item` to `CoinAHeads` and `CoinBHeads`
|
||||
* From the Math Toolbox drawer, drag 2 'pick random true or false' blocks to the coding workspace
|
||||
* Hover over this 'pick random' block and note that its pop-up description mentions coin flipping!
|
||||
|
||||
![Pick Math random boolean](/static/courses/csintro/booleans/math-random-boolean.png)
|
||||
|
||||
* Attach these 'pick random' blocks to the 'set' variable blocks in the 'on shake' block
|
||||
|
||||
```blocks
|
||||
let CoinBHeads = false
|
||||
let CoinAHeads = false
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
CoinAHeads = Math.randomBoolean()
|
||||
CoinBHeads = Math.randomBoolean()
|
||||
})
|
||||
```
|
||||
|
||||
Now that the virtual CoinA and CoinB have been virtually flipped, we need to compare the outcomes to see if they are the same or different.
|
||||
|
||||
* From the Logic Toolbox drawer, drag an 'if...then...else' block to the coding workspace
|
||||
* Drag the 'if...then...else' block into the 'on shake' block under the 'set' variable blocks
|
||||
|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
CoinAHeads = Math.randomBoolean()
|
||||
CoinBHeads = Math.randomBoolean()
|
||||
if (true) {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Now our logic block is ready for the next steps of our pseudocode.
|
||||
1. Compare the current values of Coin A and Coin B.
|
||||
2. If the current true/false values of Coin A and Coin B are the same, add a point to Player A’s score.
|
||||
3. Otherwise, if the current true/false values of Coin A and Coin B are different, add a point to Player B’s score.
|
||||
|
||||
Because we were able to visualize our blocks as we wrote our pseudocode, we already know what blocks we will use and also know that we have simplified our code as much as possible!
|
||||
|
||||
* We can now simply add this to our current code
|
||||
* And provide user feedback by adding some visuals
|
||||
|
||||
```block
|
||||
let PlayerBScore = 0
|
||||
let PlayerAScore = 0
|
||||
let CoinBHeads = false
|
||||
let CoinAHeads = false
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
CoinAHeads = Math.randomBoolean()
|
||||
CoinBHeads = Math.randomBoolean()
|
||||
if (CoinAHeads == CoinBHeads) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. # . # .
|
||||
. # # # .
|
||||
. # . # .
|
||||
. # . # .
|
||||
`)
|
||||
basic.pause(100)
|
||||
PlayerAScore += 1
|
||||
} else {
|
||||
basic.showLeds(`
|
||||
. # # . .
|
||||
. # . # .
|
||||
. # # . .
|
||||
. # . # .
|
||||
. # # . .
|
||||
`)
|
||||
basic.pause(100)
|
||||
PlayerBScore += 1
|
||||
}
|
||||
basic.showLeds(`
|
||||
. # . . .
|
||||
# # # . .
|
||||
. # . # .
|
||||
. . # # #
|
||||
. . . # .
|
||||
`)
|
||||
})
|
||||
```
|
||||
|
||||
To finish our program, we’ll display the players’ current scores on button A pressed.
|
||||
|
||||
Here is the complete program for our Double Coin Flipper.
|
||||
|
||||
```blocks
|
||||
let PlayerBScore = 0
|
||||
let PlayerAScore = 0
|
||||
let CoinBHeads = false
|
||||
let CoinAHeads = false
|
||||
input.onButtonPressed(Button.A, () => {
|
||||
CoinAHeads = Math.randomBoolean()
|
||||
CoinBHeads = Math.randomBoolean()
|
||||
PlayerAScore = 0
|
||||
PlayerBScore = 0
|
||||
basic.showLeds(`
|
||||
. # . . .
|
||||
# # # . .
|
||||
. # . # .
|
||||
. . # # #
|
||||
. . . # .
|
||||
`)
|
||||
})
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
CoinAHeads = true
|
||||
CoinBHeads = true
|
||||
if (CoinAHeads == CoinBHeads) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. # . # .
|
||||
. # # # .
|
||||
. # . # .
|
||||
. # . # .
|
||||
`)
|
||||
basic.pause(100)
|
||||
PlayerAScore += 1
|
||||
} else {
|
||||
basic.showLeds(`
|
||||
. # # . .
|
||||
. # . # .
|
||||
. # # . .
|
||||
. # . # .
|
||||
. # # . .
|
||||
`)
|
||||
basic.pause(100)
|
||||
PlayerBScore += 1
|
||||
}
|
||||
basic.showLeds(`
|
||||
. # . . .
|
||||
# # # . .
|
||||
. # . # .
|
||||
. . # # #
|
||||
. . . # .
|
||||
`)
|
||||
})
|
||||
```
|
||||
|
||||
Try it out!
|
||||
Have the students play a few more rounds of the Double Coin Flip using their new Micro:bit Double Coin Flipper!
|
||||
|
||||
## Boolean operator NOT in a Loop
|
||||
|
||||
```block
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
while (!(input.buttonIsPressed(Button.A))) {
|
||||
for (let i = 0; i < 2; i++) {
|
||||
music.playTone(262, music.beat(BeatFraction.Half))
|
||||
music.playTone(523, music.beat(BeatFraction.Half))
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Do you remember this code from our micro:bit Alarm?
|
||||
Can you read this code and tell what it does?
|
||||
|
||||
_If the micro:bit is shaken, the micro:bit will play two tones twice and keep repeating this action until button A is pressed. So, after shaking, as long as ‘is button A pressed?’ is false, the two tone alarm will continue to repeat._
|
73
docs/courses/csintro/booleans/overview.md
Normal file
73
docs/courses/csintro/booleans/overview.md
Normal file
@ -0,0 +1,73 @@
|
||||
# Introduction
|
||||
|
||||
There are several different data types used in computer programming. We have already used two of these types:
|
||||
* [String](/types/string) (for text)
|
||||
* [Integer](/types/number) (for numbers)
|
||||
|
||||
Boolean is another type of data. A boolean data type has only two values: true or false.
|
||||
In true binary fashion, these two values can be represented by the numbers 1 = true, and 0 = false.
|
||||
|
||||
Booleans are useful in programming for decision-making, often deciding when certain functions and parts of programs should start or stop running and are also used in database searches.
|
||||
|
||||
Ask the students to think of things in daily life that have only two values or states. The status is always one value or the other value.
|
||||
|
||||
Examples of Booleans in daily life
|
||||
* Lights: On or Off
|
||||
* Time: AM or PM
|
||||
* You!: Asleep or Awake
|
||||
* Weather: Raining or Not Raining
|
||||
* Math: Equal to or Not Equal to
|
||||
* Game: Truth or Dare
|
||||
* Soda: Coke or Pepsi
|
||||
* At the store: Paper or Plastic? Cash or Credit? Chip or Swipe?
|
||||
|
||||
Note:
|
||||
Arguments can be made that some of these can have more than two values.
|
||||
For example: At the store, you may have brought your own reusable bags or pay by check.
|
||||
|
||||
Let the students discuss these to help them hone in on which examples best represent Booleans.
|
||||
|
||||
A student might argue that a dimmer switch on a light or the brightness value on the micro:bit LEDs allow the lights to be in a state between on and off. One could respond that you can classify ‘on’ as the state where any electricity at all is running through the bulb (on) versus no electricity at all (off).
|
||||
|
||||
In programming, if you have worked with conditionals or loops, you have already worked with this type of logic:
|
||||
* If a certain condition is true, do this, otherwise (if condition is false), do something else.
|
||||
* While a certain condition is true, do this
|
||||
|
||||
Boolean Operators: AND, OR, and NOT
|
||||
To make working with Booleans useful for solving more complex decisions and searches, we can connect two or more Booleans into one decision statement. To do this, we use what are known as Boolean operators. The three most common and the ones we will use with the micro:bit are And, Or, and Not.
|
||||
|
||||
These operators can be used in conditionals and loops, like so:
|
||||
* If condition A is true AND condition B is true
|
||||
* If condition A is true OR condition B is true
|
||||
* While event A has NOT happened
|
||||
|
||||
Let’s look at how each of these work.
|
||||
|
||||
## AND
|
||||
(Condition A AND Condition B)
|
||||
For this expression to evaluate as true, both conditions in the expression need to be true.
|
||||
So, if both Condition A AND Condition B are true, the expression will evaluate as or return true.
|
||||
|
||||
## OR
|
||||
(Condition A OR Condition B)
|
||||
For this expression to evaluate as true, only one of the conditions in the expression needs to be true.
|
||||
If Condition A is true, the expression will return true regardless of whether Condition B is true or false.
|
||||
If Condition B is true, the expression will return true regardless of whether Condition A is true or false.
|
||||
|
||||
## NOT
|
||||
NOT can be used when checking that a condition is false (or not true).
|
||||
For example:
|
||||
* (NOT Condition A and Condition B) evaluates as true only if Condition A is false and Condition B is true.
|
||||
* (Condition A and NOT Condition B) evaluates as true only if Condition A is true and Condition B is false.
|
||||
* (NOT Condition A and NOT Condition B) evaluates as true only if both Condition A and Condition B are true.
|
||||
NOT is also useful when using a loop. For example, you can use a NOT to check
|
||||
While button A is NOT pressed, continue to run this code…
|
||||
|
||||
Note: ‘False’ can be thought of as equivalent to ‘NOT true’.
|
||||
|
||||
## Sidebar material
|
||||
![George Boole](/static/courses/csintro/booleans/george-boole.jpg)
|
||||
Image credit: Wikimedia Commons
|
||||
|
||||
George Boole (2 November 1815 – 8 December 1864) was an English mathematician, educator, philosopher and **logician**. He worked in the fields of differential equations and algebraic **logic**, and is best known as the author of The Laws of Thought (1854) which contains **Boolean** algebra.
|
||||
|
337
docs/courses/csintro/booleans/project.md
Normal file
337
docs/courses/csintro/booleans/project.md
Normal file
@ -0,0 +1,337 @@
|
||||
# Project: Boolean
|
||||
|
||||
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.
|
||||
|
||||
![micro:bit input list](/static/courses/csintro/variables/input-list.png)
|
||||
|
||||
## Project Ideas
|
||||
|
||||
### Sunscreen Monitor
|
||||
|
||||
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.
|
||||
|
||||
[**micro:bit Sunscreen Monitor**](https://youtu.be/VmD-dcZZQFc)
|
||||
https://youtu.be/VmD-dcZZQFc
|
||||
|
||||
#### Sunscreen code
|
||||
|
||||
```blocks
|
||||
input.onButtonPressed(Button.B, () => {
|
||||
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).
|
||||
```block
|
||||
if (player1Turn == true && Math.random(4) < 3) {}
|
||||
```
|
||||
|
||||
## Assessment
|
||||
|
||||
**Competency scores**: 4, 3, 2, 1
|
||||
|
||||
### Boolean
|
||||
|
||||
**4 =** More than 2 Boolean variables are implemented in a meaningful way.<br/>
|
||||
**3 =** At least 2 Boolean variable is implemented in a meaningful way.<br/>
|
||||
**2 =** At least 1 Boolean variable is implemented in a meaningful way.<br/>
|
||||
**1 =** No Boolean variables are implemented.
|
||||
|
||||
### Micro:bit program
|
||||
|
||||
**4 =** micro:bit program:<br/>
|
||||
`*` Uses Boolean variables in a way that is integral to the program<br/>
|
||||
`*` Uses a random function in a way that is integral to the program<br/>
|
||||
`*` Compiles and runs as intended<br/>
|
||||
`*` Meaningful comments in code<br/>
|
||||
**3 =** micro:bit program lacks 1 of the required element.<br/>
|
||||
**2 =** micro:bit program lacks 2 of the required elements.<br/>
|
||||
**1 =** micro:bit program lacks 3 or more of the required elements.
|
||||
|
||||
### Collaboration reflection
|
||||
|
||||
**4 =** Reflection piece includes:<br/>
|
||||
`*` Brainstorming ideas<br/>
|
||||
`*` Construction<br/>
|
||||
`*` Programming<br/>
|
||||
`*` Beta testing<br/>
|
||||
**3 =** Reflection piece lacks 1 of the required elements.<br/>
|
||||
**2 =** Reflection piece lacks 2 of the required elements.<br/>
|
||||
**1 =** Reflection piece lacks 3 of the required elements.
|
10
docs/courses/csintro/booleans/standards.md
Normal file
10
docs/courses/csintro/booleans/standards.md
Normal file
@ -0,0 +1,10 @@
|
||||
# Standards
|
||||
|
||||
## CSTA K-12 Computer Science Standards
|
||||
|
||||
* CL.L2-03 Collaborate with peers, experts, and others using collaborative practices such as pair programming, working in project teams, and participating in group active learning activities
|
||||
* CT.L1:6-01 Understand and use the basic steps in algorithmic problem-solving
|
||||
* CT.L1:6-02 Develop a simple understanding of an algorithm using computer-free exercises
|
||||
* CPP.L1:6-05 Construct a program as a set of step-by-step instructions to be acted out
|
||||
* 2-A-5-7 Create variables that represent different types of data and manipulate their values.
|
||||
|
211
docs/courses/csintro/booleans/unplugged.md
Normal file
211
docs/courses/csintro/booleans/unplugged.md
Normal file
@ -0,0 +1,211 @@
|
||||
## Unplugged: Two Heads are Better Than One
|
||||
|
||||
Materials: A penny for each student, paper and pencils
|
||||
|
||||
Most students have used a penny to decide something. Ask for some examples.
|
||||
Who goes first in a game, to break a tie, to decide which activity to do...
|
||||
|
||||
A simple penny is the most common binary decision making tool ever!
|
||||
When you flip a coin to decide something there are only two possible outcomes, heads or tails.
|
||||
When you flip a coin the outcome is random.
|
||||
|
||||
What’s a common issue with coin tosses? Students may bring up issues of trust and fairness. Who gets to flip the coin? Who gets to ‘call’ it? What if it’s a ‘faulty’ coin?
|
||||
|
||||
Here’s a solution... The double coin toss.
|
||||
|
||||
![Two pennies showing heads and tails](/static/courses/csintro/booleans/two-coins.jpg)
|
||||
|
||||
In a double coin toss, both people have a coin and they flip the coins at the same time.
|
||||
|
||||
Working in pairs, have the students make a table or list of the possible outcomes if each student flipped a coin at the same time.
|
||||
|
||||
Example:
|
||||
```
|
||||
Coin A Coin B
|
||||
==============
|
||||
Heads Heads
|
||||
Heads Tails
|
||||
Tails Heads
|
||||
Tails Tails
|
||||
```
|
||||
|
||||
There are 4 possible outcomes.
|
||||
* For 2 outcomes, the result is the same for both coins, both heads or both tails.
|
||||
* For the other 2 outcomes, the result for each coin is different, heads/tails and tails/heads.
|
||||
|
||||
So, if 2 coins are flipped, the chance that the outcomes will be the same (HH/TT) is equal to the chance that the outcomes will be different (HT/TH). Both outcomes, coins the same/coins are different have a 2 in 4 or 50% chance of occurring.
|
||||
|
||||
Therefore, if Person A wins each time the outcomes are the same and Person B wins each time the outcomes are different, both have an equal chance of winning each double coin flip. With this system, no one person’s outcome, heads or tails, guarantees a win. If Person A’s coin flips to heads, she would win if Person B also flipped heads, but lose if Person B flipped tails. Students will usually see that this is a fair system.
|
||||
|
||||
Let the students experiment with this.
|
||||
Have students flip their coins together, keeping track of the outcomes, perhaps by adding another column to their table.
|
||||
|
||||
Example:
|
||||
```
|
||||
Coin A Coin B Totals
|
||||
========================
|
||||
Heads Heads
|
||||
Heads Tails
|
||||
Tails Heads
|
||||
Tails Tails
|
||||
```
|
||||
|
||||
Just for fun, have them play to a certain total number of rounds.
|
||||
|
||||
So, what does this have to do Boolean variables and operators?
|
||||
Think about how you would code a program a double coin flipper.
|
||||
How would you represent each of the 4 different possible double coin flip outcomes?
|
||||
|
||||
Let’s pseudocode it!
|
||||
|
||||
We can create a Boolean variable to represent whether an outcome is heads or tails.
|
||||
We can make
|
||||
* Heads = True
|
||||
* Tails = False
|
||||
|
||||
Note: Tails = False can also be thought of as Tails = not true.
|
||||
|
||||
Have the students copy their Heads/Tails table of possible outcomes, but label the columns "Coin A Heads" and "Coin B Heads" and replace each entry of ‘Heads’ with ‘True’ and ‘Tails’ with ‘False’. In the study of logic, this is known as a truth table.
|
||||
|
||||
We’ll use it to help us pseudocode our program, by adding a third column describing the results of each outcome.
|
||||
|
||||
```
|
||||
Coin A Coin B Results
|
||||
Heads Heads
|
||||
==================================================================================
|
||||
True True If Coin A is true AND Coin B is true, add one to Player A score
|
||||
True False If Coin A is true AND Coin B is false, add one to Player B score
|
||||
False True If Coin A is false AND Coin B is true, add one to Player B score
|
||||
False False If Coin A is false AND Coin B is false, add one to Player A score
|
||||
```
|
||||
|
||||
Can we make this code more efficient? Can we combine any of these lines?
|
||||
Try using an OR to combine both conditions in which Player A scores a point.
|
||||
Do the same for both conditions in which Player B scores a point.
|
||||
|
||||
Give the students a chance to work this out on their own.
|
||||
|
||||
Combining the conditions in which each player wins, gives us:
|
||||
* If (Coin A is true AND Coin B is true) OR (Coin A is false AND Coin B is false), add one to Player A score.
|
||||
* If (Coin A is true AND Coin B is false) OR (Coin A is false AND Coin B is true), add one to Player B score.
|
||||
|
||||
Note: Just as you do for math expressions with multiple operators, use parentheses to make it clear how the conditions and statements are grouped together.
|
||||
|
||||
The students are by now familiar with the MakeCode blocks. As they think through their algorithms, they may even have started to visualize the blocks they might use. Visualizing the blocks as they pseudocode can help them with the logical steps of their program. It can also help them to visualize and recognize the big picture of their code as well as the details.
|
||||
|
||||
Using blocks to start coding these two conditionals as currently written, might look like this:
|
||||
```block
|
||||
let CoinAHeads = false
|
||||
let CoinBHeads = false
|
||||
if ((CoinAHeads == true && CoinBHeads == true) || (CoinAHeads == false && CoinBHeads == false)) {}
|
||||
```
|
||||
Then add one to Player A score.
|
||||
|
||||
```block
|
||||
let CoinAHeads = false
|
||||
let CoinBHeads = false
|
||||
if ((CoinAHeads == true && CoinBHeads == false) || (CoinAHeads == false && CoinBHeads == true)) {}
|
||||
```
|
||||
Then add one to Player B score.
|
||||
|
||||
Though this code will work as we want it to, it’s a lot of code. It is good practice to keep your code as simply as possible. Let’s see how we can do just that.
|
||||
|
||||
## Booleans and simplifying code
|
||||
|
||||
A boolean can have only one of two values: True or False. Conditionals like 'if...then' check whether a condition is true. Notice that the default condition for the 'if...then' blocks is true. In other words, the 'if...then' blocks will check to see whether whatever condition you place there is true.
|
||||
```block
|
||||
basic.forever(() => { if (true) { } })
|
||||
```
|
||||
|
||||
This means that ‘If `CoinAHeads`’ is the same as ‘If `CoinAHeads` = true’. We can use this to simplify our code.
|
||||
|
||||
Instead of:
|
||||
|
||||
```block
|
||||
let CoinAHeads = false
|
||||
let CoinBHeads = false
|
||||
if (CoinAHeads == true && CoinBHeads == true) {}
|
||||
```
|
||||
|
||||
we can code:
|
||||
|
||||
```block
|
||||
let CoinAHeads = false
|
||||
let CoinBHeads = false
|
||||
if (CoinAHeads && CoinBHeads) {}
|
||||
```
|
||||
|
||||
Also, since ‘If not `CoinAHeads`’ is the same as ‘If `CoinAHeads` = False’,
|
||||
instead of:
|
||||
|
||||
```block
|
||||
let CoinAHeads = false
|
||||
if (CoinAHeads == false) {}
|
||||
```
|
||||
we can code:
|
||||
```block
|
||||
let CoinAHeads = false
|
||||
if (!CoinAHeads) {}
|
||||
```
|
||||
|
||||
So now we have:
|
||||
|
||||
```block
|
||||
let CoinAHeads = false
|
||||
let CoinBHeads = false
|
||||
basic.forever(() => {
|
||||
if ((CoinAHeads && CoinBHeads) || (!CoinAHeads && !CoinBHeads)) {}
|
||||
})
|
||||
```
|
||||
Can we simplify it even more?
|
||||
For this particular program, since we are checking to see if the conditions `CoinAHeads` and `CoinBHeads` are the same, whether both true or both false, we can use a logic equals block to simplify our code to:
|
||||
|
||||
```block
|
||||
let CoinAHeads = false
|
||||
let CoinBHeads = false
|
||||
if (CoinAHeads == CoinBHeads) {}
|
||||
```
|
||||
|
||||
Then add one to Player A score.
|
||||
|
||||
What about our other big block of code for the conditions for a Player B win?
|
||||
We could simplify that to:
|
||||
```block
|
||||
let CoinAHeads = false
|
||||
let CoinBHeads = false
|
||||
if (CoinAHeads != CoinBHeads) {}
|
||||
```
|
||||
Then add one to Player B score.
|
||||
|
||||
We don’t need to do this! Since the only other option to being equal is to be not equal, we can simply do this:
|
||||
|
||||
```block
|
||||
let CoinAHeads = false
|
||||
let CoinBHeads = false
|
||||
let PlayerAScore = 0
|
||||
let PlayerBScore = 0
|
||||
basic.forever(() => {
|
||||
if (CoinAHeads != CoinBHeads) {
|
||||
PlayerAScore += 1
|
||||
} else {
|
||||
PlayerBScore += 1
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Random Functions
|
||||
|
||||
We use a coin flip to decide things because the result is random, meaning the result happens without any conscious decision or direction. We use dice and game spinners for the same reason. The results are not predetermined or directed in any way.
|
||||
|
||||
So, how do we get a random flip in code? Most computer programming languages have a built in function that will select a random number given a range of values. Microsoft MakeCode has a block for this. And it also has a block for getting a random true or false value.
|
||||
|
||||
We will call on this built in function to get a random true or false value for each flip of a coin in the next [Activity](/courses/csintro/booleans/activity).
|
||||
|
||||
Our basic pseudocode for our Double Coin Flipper could look like this:
|
||||
|
||||
1. Use the random function to get a true/false value for Coin A.
|
||||
2. Use the random function to get a true/false value for Coin A.
|
||||
3. Compare the current values of Coin A and Coin B.
|
||||
4. If the current true/false values of Coin A and Coin B are the same, add a point to Player A’s score.
|
||||
5. Otherwise, the current true/false values of Coin A and Coin B must be different, so add a point to Player B’s score.
|
||||
6. When players are done with their double coin flipping, show the final scores for each player.
|
BIN
docs/static/courses/csintro/booleans/george-boole.jpg
vendored
Normal file
BIN
docs/static/courses/csintro/booleans/george-boole.jpg
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
BIN
docs/static/courses/csintro/booleans/logic-menu.png
vendored
Normal file
BIN
docs/static/courses/csintro/booleans/logic-menu.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 52 KiB |
BIN
docs/static/courses/csintro/booleans/math-random-boolean.png
vendored
Normal file
BIN
docs/static/courses/csintro/booleans/math-random-boolean.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 51 KiB |
BIN
docs/static/courses/csintro/booleans/shakespeare.png
vendored
Normal file
BIN
docs/static/courses/csintro/booleans/shakespeare.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 104 KiB |
BIN
docs/static/courses/csintro/booleans/two-coins.jpg
vendored
Normal file
BIN
docs/static/courses/csintro/booleans/two-coins.jpg
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.1 KiB |
BIN
docs/static/courses/csintro/booleans/two-player-game.png
vendored
Normal file
BIN
docs/static/courses/csintro/booleans/two-player-game.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 271 KiB |
Loading…
Reference in New Issue
Block a user