Remove offending docs
This commit is contained in:
parent
6fef590912
commit
3afa302e65
@ -1,211 +1 @@
|
||||
<!-- ## 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/pennies.png)
|
||||
|
||||
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. -->
|
||||
TODO: Fix for microbit master
|
@ -1,87 +1 @@
|
||||
<!-- # micro:coin
|
||||
|
||||
## ~ avatar
|
||||
|
||||
Have you heard about BitCoin and all those new Crypto currencies? Well micro:bit has **micro:coin** now!
|
||||
|
||||
## ~
|
||||
|
||||
## How does a @boardname@ make coins?
|
||||
|
||||
Each @boardname@ contains a **block chain**, a sequence of **blocks**, that is public and can't be modified. Each block represents a **coin**. To mine new coins, the user shakes
|
||||
the @boardname@ and, if they are in luck, their coin added to the chain as a new block!
|
||||
Once the block is added, it is broadcasted to the other @boardname@ (the block chain is public and can't be modified so it's ok to share it). Other @boardname@s receive the block, validate the transaction and update their block chain as needed.
|
||||
|
||||
Pressing ``A`` shows the number of block you added to the chain, that's your score.
|
||||
Pressing ``B`` shows you the length of the chain.
|
||||
|
||||
Happy mining!
|
||||
|
||||
## Coins, blocks, chains
|
||||
|
||||
A _block chain_ is a list of _blocks_ that record transactions of a crypto-currency like BitCoin. A block might contain information like the time it was created (mined) and who mined it. The most important part of the block is it's _hash_. This is a special number made from the information in the last block of the block list combined with the hash number of previous block in the list. The new block contains information for the current transaction and this new hash number. The new block is added to the list of previous blocks. This list is then transmitted to the crypto currency network. It's really hard (like impossible) to tamper or forge a hash which allows the block chain to be transmitted publically.
|
||||
|
||||
## ~ hint
|
||||
|
||||
Build yourself a [@boardname@ wallet](/projects/wallet) to hold your coins!
|
||||
|
||||
## ~
|
||||
|
||||
## Code
|
||||
|
||||
The code uses blocks from [radio-blockchain](https://makecode.microbit.org/pkg/microsoft/pxt-radio-blockchain) package.
|
||||
|
||||
* Click on **Advanced**, then **Add Package**
|
||||
* search for **blockchain** and add **radio-blockchain**
|
||||
|
||||
```blocks
|
||||
// shaking is mining...
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
led.stopAnimation()
|
||||
basic.clearScreen()
|
||||
basic.pause(200) // display a short pause
|
||||
if (Math.randomInt(3) == 0) { // 30% chances to add a transaction
|
||||
// we found a coin!!!
|
||||
blockchain.addBlock(1);
|
||||
basic.showIcon(IconNames.Diamond);
|
||||
} else {
|
||||
// missed!
|
||||
basic.showIcon(IconNames.Asleep);
|
||||
}
|
||||
})
|
||||
|
||||
// show my coins
|
||||
input.onButtonPressed(Button.A, () => {
|
||||
led.stopAnimation()
|
||||
let coins = blockchain.valuesFrom(blockchain.id()).length;
|
||||
basic.showNumber(coins);
|
||||
basic.showString("COINS");
|
||||
})
|
||||
|
||||
// show the block chain size
|
||||
input.onButtonPressed(Button.B, () => {
|
||||
led.stopAnimation()
|
||||
basic.showNumber(blockchain.length());
|
||||
basic.showString("BLOCKS");
|
||||
})
|
||||
|
||||
// instructions on how to play
|
||||
basic.showString("A=COINS B=CHAIN SHAKE=MINE")
|
||||
```
|
||||
|
||||
## How does the blockchain code work?
|
||||
|
||||
The [radio-blockchain](https://makecode.microbit.org/pkg/microsoft/pxt-radio-blockchain) package uses radio to transmit blocks between @boardname@s. You can see how this package works by reading the JavaScript sources at https://github.com/microsoft/pxt-radio-blockchain. To go right to the blockchain code, see this file:
|
||||
|
||||
[main.ts](https://github.com/Microsoft/pxt-radio-blockchain/blob/master/main.ts) - contains the complete JavaScript source for the blockhain. Have a good read!
|
||||
|
||||
## References
|
||||
|
||||
* ["A blockchain in 200 lines of code"](https://medium.com/@lhartikk/a-blockchain-in-200-lines-of-code-963cc1cc0e54). _medium.com_.
|
||||
* ["Let’s Build the Tiniest Blockchain"](https://medium.com/crypto-currently/lets-build-the-tiniest-blockchain-e70965a248b). _medium.com_.
|
||||
* ["How does the blockchain work?"](https://medium.com/@micheledaliessi/how-does-the-blockchain-work-98c8cd01d2ae). _medium.com_.
|
||||
|
||||
```package
|
||||
radio
|
||||
radio-blockchain=github:Microsoft/pxt-radio-blockchain#master
|
||||
``` -->
|
||||
TODO: Fix for microbit master
|
@ -1,51 +1 @@
|
||||
# Pixel
|
||||
|
||||
The pixel function.
|
||||
|
||||
Get the state of a pixel in an [Image](/reference/images/image).
|
||||
|
||||
## JavaScript
|
||||
|
||||
```sig
|
||||
export function pixel(_this: micro_bit.Image, x: number, y: number) : boolean
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
* x - [Number](/types/number); the *x coordinate* or horizontal position of a pixel in an [image](/reference/images/image)
|
||||
* y - [Number](/types/number); the *y coordinate* or vertical position of a pixel in an [image](/reference/images/image)
|
||||
|
||||
## x, y coordinates?
|
||||
|
||||
To figure out the ``x``, ``y`` coordinates, see [LED screen](/device/screen).
|
||||
|
||||
## Returns
|
||||
|
||||
* [Boolean](/blocks/logic/boolean) - `true` for on and `false` for off
|
||||
|
||||
## Example
|
||||
|
||||
This example gets the state of pixel `0, 0` in the `img` variable:
|
||||
|
||||
## ~hide
|
||||
|
||||
```blocks
|
||||
let img = images.createImage(`
|
||||
. . # . . . . . . .
|
||||
. # . # . . . # . .
|
||||
. . # . . . . . . .
|
||||
. # . # . . . # . .
|
||||
. . # . . . . . . .
|
||||
`)
|
||||
```
|
||||
|
||||
## ~
|
||||
|
||||
```typescript-ignore
|
||||
let state = img.pixel(0, 0)
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
[set pixel](/reference/images/set-pixel), [show image](/reference/images/show-image), [image](/reference/images/image), [create image](/reference/images/create-image), [scroll image](/reference/images/scroll-image)
|
||||
|
||||
TODO: Fix for microbit master
|
@ -1,42 +1 @@
|
||||
# Set Pixel
|
||||
|
||||
The set pixel function. #set pixel.
|
||||
|
||||
Set the on/off state of pixel in an [Image](/reference/images/image).
|
||||
|
||||
## JavaScript
|
||||
|
||||
```sig
|
||||
export function setPixel(_this: micro_bit.Image, x: number, y: number, value: boolean)
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
* x - [Number](/types/number); the *x coordinate* or horizontal position of a pixel in an [image](/reference/images/image)
|
||||
* x - [Number](/types/number); the *y coordinate* or vertical position of a pixel in an [image](/reference/images/image)
|
||||
* value -[Boolean](/blocks/logic/boolean); the on/off state of a pixel; `true` for on, `false` for off
|
||||
|
||||
## x, y coordinates?
|
||||
|
||||
To figure out the ``x``, ``y`` coordinates, see [LED screen](/device/screen).
|
||||
|
||||
## Example
|
||||
|
||||
The following example creates an image and stores it in the `img` variable. The `set pixel` function sets the centre pixel off, before `img` is shown using `show image`.
|
||||
|
||||
```blocks
|
||||
let img = images.createImage(`
|
||||
. . # . .
|
||||
. # . # .
|
||||
. . # . .
|
||||
. # . # .
|
||||
. . # . .
|
||||
`)
|
||||
img.setPixel(2, 2, false)
|
||||
img.showImage(0)
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
[pixel](/reference/images/pixel), [show image](/reference/images/show-image), [image](/reference/images/image), [create image](/reference/images/create-image), [scroll image](/reference/images/scroll-image)
|
||||
|
||||
TODO: Fix for microbit master
|
Loading…
Reference in New Issue
Block a user