2017-09-11 10:49:40 -07:00
# Coin Flipper
2016-06-23 23:52:24 -07:00
2018-10-10 15:38:51 -07:00
## Introduction @unplugged
2018-07-08 21:08:03 -07:00
Let's create a coin flipping program to simulate a real coin toss. We'll use icon images to represent a ``heads`` or ``tails` ` result.
2018-10-10 15:38:51 -07:00
![Simulating coin toss ](/static/mb/projects/coin-flipper/coin-flipper.gif )
2017-09-11 10:49:40 -07:00
## Step 1
2018-08-17 07:58:45 -07:00
Get an ``||input:on button A pressed||`` block from the ``||input:Input||` ` drawer in the toolbox. We'll put our coin flipping code in here.
2016-06-23 23:52:24 -07:00
```blocks
2017-09-11 10:49:40 -07:00
input.onButtonPressed(Button.A, () => {
})
2016-06-23 23:52:24 -07:00
```
2017-09-11 10:49:40 -07:00
## Step 2
2016-06-23 23:52:24 -07:00
2018-07-08 21:08:03 -07:00
Grab an ``||logic:if else||`` block and set it inside ``||input:on button A pressed||``. Put a ``||Math:pick random true or false||`` into the ``||logic:if||` ` as its condition.
2016-06-23 23:52:24 -07:00
2018-07-08 21:08:03 -07:00
The ``||Math:pick random true or false||`` returns a random ``true`` or ``false`` value which we use to determine a ``heads`` or ``tails` ` result for a coin toss.
2016-06-23 23:52:24 -07:00
2017-09-11 10:49:40 -07:00
```blocks
input.onButtonPressed(Button.A, () => {
if (Math.randomBoolean()) {
} else {
}
2018-07-08 21:08:03 -07:00
})
2017-09-11 10:49:40 -07:00
```
2016-06-23 23:52:24 -07:00
2017-09-11 10:49:40 -07:00
## Step 3
2016-06-23 23:52:24 -07:00
2018-07-08 21:08:03 -07:00
Now, put a ``||basic:show icon||`` block inside both the ``||logic:if||`` and the ``||logic:else||``. Pick images to mean ``heads`` and ``tails` `.
2016-06-23 23:52:24 -07:00
```blocks
input.onButtonPressed(Button.A, () => {
2017-09-11 10:49:40 -07:00
if (Math.randomBoolean()) {
basic.showIcon(IconNames.Skull)
} else {
basic.showIcon(IconNames.Square)
}
2018-07-08 21:08:03 -07:00
})
2016-06-23 23:52:24 -07:00
```
2017-09-11 10:49:40 -07:00
## Step 4
2018-09-14 14:57:17 -07:00
Press button **A** in the simulator to try the coin toss code.
2017-09-11 10:49:40 -07:00
## Step 5
2016-06-23 23:52:24 -07:00
2018-08-17 07:58:45 -07:00
You can animate the coin toss to add the feeling of suspense. Place different ``||basic:show icon||`` blocks before the ``||logic:if||` ` to show that the coin is flipping.
2016-06-23 23:52:24 -07:00
```blocks
2017-09-11 10:49:40 -07:00
input.onButtonPressed(Button.A, () => {
basic.showIcon(IconNames.Diamond)
basic.showIcon(IconNames.SmallDiamond)
basic.showIcon(IconNames.Diamond)
basic.showIcon(IconNames.SmallDiamond)
2016-06-23 23:52:24 -07:00
if (Math.randomBoolean()) {
2017-09-11 10:49:40 -07:00
basic.showIcon(IconNames.Skull)
2016-06-23 23:52:24 -07:00
} else {
2017-09-11 10:49:40 -07:00
basic.showIcon(IconNames.Square)
2016-06-23 23:52:24 -07:00
}
2017-09-11 10:49:40 -07:00
})
2016-06-23 23:52:24 -07:00
```
2017-09-11 10:49:40 -07:00
## Step 6
2018-09-14 14:57:17 -07:00
If you have a @boardname @, connect it to USB and click ``|Download|` ` to transfer your code.
2018-08-17 07:58:45 -07:00
## Step 7
Press button **A** for a flip. Test your luck and guess ``heads`` or ``tails` ` before the toss is over!