2019.8.31

micro:bit で学ぶプログラミング～JavaScript プログラム集～
目 次
１．プログラミングの基礎 ................................................................................................1
1.1 micro:bit の基本操作 ...............................................................................................1
1.2 プログラムの基礎（順次，繰り返し） ....................................................................1
1.3 プログラムの基礎（分岐） .......................................................................................1
演習問題 .............................................................................................................................2
２．プログラミングの応用（関数，配列） ......................................................................2
2.1 じゃんけんゲーム .....................................................................................................2
2.2 数あてゲーム ............................................................................................................3
2.3 グラフの作成 ............................................................................................................4
2.4 10 進数から 2 進数への変換 ......................................................................................5
演習問題 .............................................................................................................................5
３．センサによる計測・制御プログラム ..........................................................................5
3.1 micro:bit の各種センサと制御 ................................................................................5
3.2 音センサによる音の制御 ..........................................................................................6
3.3 傾きセンサを使った計測・制御 ...............................................................................6
3.4 地磁気センサを使った計測・制御 ............................................................................6
3.5 光センサを使った計測・制御 ...................................................................................7
演習問題 .............................................................................................................................7
４．無線通信を利用したプログラム .................................................................................7
4.1 無線通信の利用 .........................................................................................................8
4.2 無線通信を利用したじゃんけんゲーム ....................................................................8
4.3 信号機の制御 ............................................................................................................8
4.4 無線通信による信号機の制御 ...................................................................................9演習問題 ........................................................................................................................... 10
５．アルゴリズムとプログラム ...................................................................................... 10
5.1 探索 ......................................................................................................................... 10
5.2 整列 ......................................................................................................................... 11
5.3 ハノイの塔 .............................................................................................................. 12
5.4 自動販売機の状態遷移図 ........................................................................................ 13
演習問題 ........................................................................................................................... 13６．通信とプログラム ..................................................................................................... 14
6.1 通信の基本 .............................................................................................................. 14
6.2 ネットワークにおけるアドレッシング .................................................................. 14
6.3 暗号通信 .................................................................................................................. 15
6.4 エラー検出 .............................................................................................................. 16
演習問題 ........................................................................................................................... 16
７．総合問題 ................................................................................................................... 18
7.1 信号機（スクランブル交差点） ............................................................................. 18
7.2 じゃんけんゲーム（3 人対戦） .............................................................................. 18
7.3 ハノイの塔（複数台による表示） .......................................................................... 19
7.4 通信プログラム（不具合問題） ............................................................................. 202019.8.31【注意事項】「micro:bit で学ぶプログラミング－ブロック型，JavaScript そして Python へ－」の教科書で記載しているファイル名，例えば，rei○○は，このプログラム集では，実際に保存されているファイル名，microbit-rei○○.hex になっています。
micor:bit 1),2) では，ブロックから JavaScript へ自動変換されますが，JavaScript の変数や関数の名称・順序は，自動変換されたプログラムと異なる場合があります。また，ブロックに変換できない箇所は，灰色で JavaScript のプログラムが記載されています。
なお，本書で掲載している JavaScript のプログラムは，2019 年 3 月末段階で動作確認したプログラムであり，今後の仕様変更で関数名などが変わっている場合があるかもしれません。読者の方々が，ブロックの動作確認をする際，JavaScript のプログラムも確認していただけると幸いです。

参考文献
1) micro:bit の公式 Web サイト（日本語）：https://microbit.org/ja/
micro:bit の冒険を始めよう

https://microbit.org/ja/guide/2) ガレス・ハルファクリー著，金井哲夫訳：BBC マイクロビット公式ガイドブック，日経
BP 社(2018.10)．

for (let x = 0; x <= 4; x++) {led.plot(x, x)}

１．プログラミングの基礎
1.1 micro:bit の基本操作【練習 1-3】(保存ファイル名：microbit-ren1-3-1)
for (let x = 0; x <= 4; x++) {led.plot(x, x)}
for (let x = 0; x <= 4; x++) {led.plot(x, 4 - x)}

【例題 1-1】(保存ファイル名：microbit-rei1-1-1)
basic.showLeds(`
.#.#.
#####
#####
.###.
..#..
`)

(保存ファイル名：microbit-ren1-3-2)
for (let x = 0; x <= 4; x++) {led.plot(x, x)
led.plot(x, 4 - x)}

(保存ファイル名：microbit-rei1-2)
basic.forever(function () {basic.showLeds(`
.#.#.
#####
#####
.###.
..#..
`)
basic.pause(500)basic.clearScreen()
basic.pause(500)})

【例題 1-5】(保存ファイル名：microbit-rei1-5)
for (let x = 0; x <= 4; x++) {for (let y = 0; y <= 4; y++) {led.plot(x, y)
basic.pause(100)}}【例題 1-6】(保存ファイル名：microbit-rei1-6)
let x = 0
while (x <= 4) {led.plot(x, 4 - x)x += 1}

1.2 プログラムの基礎（順次，繰り返し）【例題 1-2】(保存ファイル名：microbit-rei1-2)
led.plot(2, 0)
led.plot(2, 1)
led.plot(2, 2)
led.plot(2, 3)
led.plot(2, 4)

1.3 プログラムの基礎（分岐）【例題 1-7】(保存ファイル名：microbit-rei1-7)
let c = 0
basic.forever(function () {c = Math.randomRange(0, 1)
basic.showNumber(c)if (c == 0) {basic.showIcon(IconNames.SmallDiamond)} else {basic.showIcon(IconNames.Square)}})【例題 1-3】(保存ファイル名：microbit-rei1-3)
for (let y = 0; y <= 4; y++) {led.plot(2, y)}【練習 1-1】(保存ファイル名：microbit-ren1-1)
for (let x = 0; x <= 4; x++) {led.plot(x, 2)}【例題 1-4】(保存ファイル名：microbit-rei1-4)
for (let x = 0; x <= 4; x++) {led.plot(x, 4 - x)}

【例題 1-8】(保存ファイル名：microbit-rei1-8)
let c = 0
basic.forever(function () {c = Math.randomRange(0, 2)
basic.showNumber(c)if (c == 0) {【練習 1-2】(保存ファイル名：microbit-ren1-2)
1

(保存ファイル名：microbit-ens1-3)
let c = 0
input.onGesture(Gesture.Shake, function () {c = Math.randomRange(0, 2)
basic.showNumber(c)if (c == 0) {basic.showIcon(IconNames.SmallSquare)} else if (c == 1) {basic.showIcon(IconNames.Scissors)} else {basic.showIcon(IconNames.Square)}})

basic.showIcon(IconNames.SmallSquare)} else if (c == 1) {basic.showIcon(IconNames.Scissors)} else {basic.showIcon(IconNames.Square)}})【練習 1-4】
プログラムなし
演習問題(1)(保存ファイル名：microbit-ens1-1)
for (let x = 0; x <= 4; x++) {fr (let y = 0; y <= 4; y++) {if (x == y) {led.plot(x, y)
basic.pause(100)}}}

２．プログラミングの応用（関数，配列）
2.1 じゃんけんゲーム【例題 2-1】(保存ファイル名：microbit-rei2-1)
let a = 0
let b = 0
let c = 0
input.onButtonPressed(Button.A, function () {a = Math.randomRange(0, 2)
basic.showNumber(a)c=ahyouji()})(2)(a)(保存ファイル名：microbit-ens1-2-1)
for (let x = 0; x <= 4; x++) {for (let y = 0; y <= 4; y++) {if (4 - x <= y) {led.plot(x, y)
basic.pause(500)}}}

input.onButtonPressed(Button.B, function () {b = Math.randomRange(0, 2)
basic.showNumber(b)c=bhyouji()})(2)(b)(保存ファイル名：microbit-ens1-2-2)
for (let x = 0; x <= 4; x++) {for (let y = 0; y <= 4; y++) {if (x == y || 4 - x == y) {led.plot(x, y)
basic.pause(500)}}}function hyouji() {if (c == 0) {basic.showIcon(IconNames.SmallDiamond)} else if (c == 1) {basic.showIcon(IconNames.Scissors)} else {basic.showIcon(IconNames.Square)}}(2)(c)(保存ファイル名：microbit-ens1-2-3)
for (let x = 0; x <= 4; x++) {for (let y = 0; y <= 4; y++) {if (x == 2 || y == 2) {led.plot(x, y)
basic.pause(500)}}}【例題 2-2】(保存ファイル名：microbit-rei2-2)
let a = 0
let b = 0
let c = 0
let d = 0
input.onButtonPressed(Button.A, function () {a = Math.randomRange(0, 2)
c=a

(3)
2

}

hyouji()})})

input.onButtonPressed(Button.B, function () {b = Math.randomRange(0, 2)
c=bhyouji()})

function hyouji() {if (c == 0) {basic.showIcon(IconNames.SmallDiamond)} else if (c == 1) {basic.showIcon(IconNames.Scissors)} else {basic.showIcon(IconNames.Square)}
basic.clearScreen()}

input.onButtonPressed(Button.AB, function () {d = (a - b + 3) % 3
if (d == 2) {basic.showString("A")} else if (d == 1) {basic.showString("B")} else {basic.showString("AB")}})2.2 数あてゲーム【例題 2-3】(保存ファイル名：microbit-rei2-3)
let kotae = 0
let kouho = 0
kotae = Math.randomRange(0, 2)
basic.showNumber(kotae)function hyouji() {if (c == 0) {basic.showIcon(IconNames.SmallDiamond)} else if (c == 1) {basic.showIcon(IconNames.Scissors)} else {basic.showIcon(IconNames.Square)}
basic.clearScreen()}

input.onButtonPressed(Button.A, function () {kouho = Math.randomRange(0, 2)
basic.showNumber(kouho)})
input.onButtonPressed(Button.B, function () {if (kouho == kotae) {basic.showIcon(IconNames.Heart)} else {basic.showIcon(IconNames.No)}})【練習 2-1】(保存ファイル名：microbit-ren2-1)
let a = 0
let b = 0
let c = 0
let d = 0

【例題 2-4】(保存ファイル名：microbit-rei2-4)
let kotae = 0
let kouho = 0
kotae = Math.randomRange(0, 4)
basic.showNumber(kotae)kouho = Math.randomRange(0, 4)
basic.showNumber(kouho)
hantei()input.onButtonPressed(Button.A, function () {a = Math.randomRange(0, 2)
c=ahyouji()})
input.onButtonPressed(Button.B, function () {b = Math.randomRange(0, 2)
c=bhyouji()})

function hantei() {if (kouho > kotae) {basic.showArrow(ArrowNames.South)
basic.clearScreen()} else if (kouho < kotae) {basic.showArrow(ArrowNames.North)basic.clearScreen()} else {basic.showNumber(kouho)
basic.clearScreen()
basic.showString("Hit")}

input.onButtonPressed(Button.AB, function () {d = (a - b + 3) % 3
if (d == 2) {basic.showString("A" + "Kati")} else if (d == 1) {basic.showString("B" + "Kati")} else {basic.showString("Hikiwake")3

for (let x = 0; x <= a[y] - 1; x++) {led.plot(x, y)}}
input.onButtonPressed(Button.A, function () {kouho += 1
hantei()})}
【関数の引数と戻り値（コラム）】(保存ファイル名：microbit-c23-kansu)
let c = 0c = sum(3, 5)
basic.showString("3+5=")
basic.showNumber(c)input.onButtonPressed(Button.B, function () {kouho += -1
hantei()})

function sum(a: number, b: number): number {let s = 0s=a+b
return s}【練習 2-2】(保存ファイル名：microbit-ren2-2)
let kotae = 0
let kouho = 0
kotae = Math.randomRange(0, 4)
if (kouho == kotae) {kouho = Math.randomRange(0, 4)}
basic.showNumber(kouho)
hantei()【練習 2-3】(保存ファイル名：microbit-ren2-3-1)
let x = 0
let a: number[] = []a[0] = 3a[1] = 2a[2] = 1a[3] = 5a[4] = 4
for (let z = 0; z <= 4; z++) {basic.showNumber(a[z])}function hantei() {if (kouho > kotae) {basic.showArrow(ArrowNames.South)
basic.clearScreen()} else if (kouho < kotae) {basic.showArrow(ArrowNames.North)basic.clearScreen()} else {basic.showNumber(kouho)
basic.clearScreen()
basic.showString("Hit")}}(保存ファイル名：microbit-ren2-3-2)
let x = 0
let a: number[] = []a[0] = 3, a[1] = 2, a[2] = 1, a[3] = 5, a[4] = 4
for (let z = 0; z <= 4; z++) {basic.showNumber(a[z])}input.onButtonPressed(Button.A, function () {kouho += 1
hantei()})

【練習 2-4】(保存ファイル名：microbit-ren2-4)
let a: number[] = []a = [1, 2]
let b: string[] = []b = ["AB", "cd"]input.onButtonPressed(Button.B, function () {kouho += -1
hantei()})

for (let i = 0; i <= 1; i++) {basic.showNumber(a[i])}basic.clearScreen()for (let j = 0; j <= 1; j++) {basic.showString(b[j])}2.3 グラフの作成【例題 2-5】(保存ファイル名：microbit-rei2-5)
let a: number[] = []a = [3, 2, 1, 5, 4]
for (let i = 0; i <= 4; i++) {basic.showNumber(a[i])}basic.clearScreen()for (let y = 0; y <= 4; y++) {【例題 2-6】(保存ファイル名：microbit-rei2-6)
let x = 0
let y = 0
4

function Graph_H() {for (let y = 0; y <= 4; y++) {for (let x = 0; x <= a[y] - 1; x++) {led.plot(x, y)
basic.pause(500)}}}

let a: number[] = []a = [3, 2, 1, 5, 4]
for (let x = 0; x <= 4; x++) {for (let z = 0; z <= a[x] - 1; z++) {y=4-z
led.plot(x, y)
basic.pause(500)}}function Graph_V() {for (let x = 0; x <= 4; x++) {for (let z = 0; z <= a[x] - 1; z++) {y=4-z
led.plot(x, y)
basic.pause(500)}}}

2.4 10 進数から 2 進数への変換【例題 2-7】(保存ファイル名：microbit-rei2-7)
let xp: number[] = []xp = [0, 0, 0, 0, 0]
for (let k = 0; k <= 31; k++) {DtoB(k)}(2)(保存ファイル名：microbit-ens2-2)
let x = 0
let y = 0
let a: number[] = []sa = [3, 2, 1, 5, 4]

function DtoB(k: number) {for (let j = 4; j >= 0; j--) {xp[j] = k % 2
k = Math.floor(k / 2)}
Plot()}

plot(0)basic.clearScreen()plot(1)function Plot() {for (let i = 4; i >= 0; i--) {if (xp[i] == 1) {let x = ifor (let y = 0; y <= 4; y++) {led.plot(x, y)}}}
basic.pause(1000)
basic.clearScreen()}function plot(g: number) {if (g == 0) {for (let y = 0; y <= 4; y++) {for (let x = 0; x <= a[y] - 1; x++)
led.plot(x, y)
basic.pause(500)}}} else {for (let x = 0; x <= 4; x++) {for (let z = 0; z <= a[x] - 1; z++)y=4-z
led.plot(x, y)
basic.pause(500)
}
}
}
}

演習問題
(1)
(保存ファイル名：microbit-ens2-1)
let x = 0
let y = 0
let a: number[] = []a = [3, 2, 1, 5, 4]plot()

３．センサによる計測・制御プログラム
3.1 micro:bit の各種センサと制御

function plot() {Graph_H()
basic.clearScreen()
Graph_V()}【例題 3-1】(保存ファイル名：microbit-rei3-1)
basic.forever(function () {basic.showNumber(input.lightLevel())
basic.clearScreen()5【例題 3-3】(保存ファイル名：microbit-rei3-3)
input.onButtonPressed(Button.A, function () {music.beginMelody(music.builtInMelody(Melodies.PowerUp), MelodyOptions.Once)})basic.pause(1000)})【練習 3-1】(保存ファイル名：microbit-ren3-1)
basic.forever(function () {basic.showNumber(input.acceleration(Dimension.X))
basic.clearScreen()
basic.pause(1000)})【練習 3-4】(保存ファイル名：microbit-ren3-4)
input.onButtonPressed(Button.A, function () {music.beginMelody(music.builtInMelody(Melodies.Dadadadum), MelodyOptions.Once)})

3.2 音センサによる音の制御

input.onButtonPressed(Button.B, function () {music.beginMelody(music.builtInMelody(Melodies.PowerUp), MelodyOptions.Once)})【例題 3-2】(保存ファイル名：microbit-rei3-2)
basic.forever(function () {music.playTone(262,music.beat(BeatFraction.Whole))
music.playTone(294,music.beat(BeatFraction.Whole))
music.playTone(330,music.beat(BeatFraction.Whole))
music.rest(music.beat(BeatFraction.Whole))})

3.3 傾きセンサを使った計測・制御【例題 3-4】(保存ファイル名：microbit-rei3-4)
basic.forever(function () {if (input.rotation(Rotation.Roll) < -10) {music.ringTone(262)}else if (input.rotation(Rotation.Roll) > 10) {music.ringTone(294)}else {
music.rest(music.beat(BeatFraction.
Sixteenth))}})【練習 3-2】(保存ファイル名：microbit-ren3-2)
basic.forever(function () {music.playTone(262,music.beat(BeatFraction.Whole))
music.playTone(262 * 2,music.beat(BeatFraction.Whole))
music.playTone(262 * 4,music.beat(BeatFraction.Whole))})【練習 3-5】(保存ファイル名：microbit-ren3-5)
basic.forever(function () {music.ringTone(2 *input.rotation(Rotation.Roll) + 262)})

【練習 3-3】(保存ファイル名：microbit-ren3-3)
basic.forever(function () {music.playTone(262,music.beat(BeatFraction.Whole))
music.playTone(262 * 2,music.beat(BeatFraction.Whole))
music.playTone(262 * 3,music.beat(BeatFraction.Whole))
music.playTone(262 * 4,music.beat(BeatFraction.Whole))
music.playTone(262 * 5,music.beat(BeatFraction.Whole))
music.playTone(523,music.beat(BeatFraction.Whole))
music.playTone(523 * 2,music.beat(BeatFraction.Whole))
music.playTone(523 * 3,music.beat(BeatFraction.Whole))})

3.4 地磁気センサを使った計測・制御【例題 3-5】(保存ファイル名：microbit-rei3-5)
let 角度 = 0
basic.forever(function () {角度 = input.compassHeading()
if (角度 < 45) {basic.showString("N")} else if (角度 > 315) {basic.showString("N")} else {basic.clearScreen()
}
})
6

演習問題
【例題 3-6】(保存ファイル名：microbit-rei3-6)
let 角度 = 0
basic.forever(function () {角度 = input.compassHeading()
if (角度 < 45) {basic.showArrow(ArrowNames.North)} else if (角度 < 135) {basic.showArrow(ArrowNames.West)} else if (角度 < 225) {basic.showArrow(ArrowNames.South)} else if (角度 < 315) {basic.showArrow(ArrowNames.East)} else {basic.showArrow(ArrowNames.North)
}
})

(1)
(保存ファイル名：microbit-ens3-1)
let ｘ = 0
let ｙ = 0
let value = 0
let ans = 0
basic.forever(function () {basic.clearScreen()value = input.rotation(Rotation.Roll)calc()
ｘ = ans
value = input.rotation(Rotation.Pitch)calc()
ｙ = ans
led.plot(ｘ, ｙ)})【練習 3-6】(保存ファイル名：microbit-ren3-6)
let 角度 = 0
basic.forever(function () {角度 = input.compassHeading()
if (角度 < 23) {basic.showArrow(ArrowNames.North)} else if (角度 < 68) {basic.showArrow(ArrowNames.NorthWest)} else if (角度 < 113) {basic.showArrow(ArrowNames.West)} else if (角度 < 158) {basic.showArrow(ArrowNames.SouthWest)} else if (角度 < 203) {basic.showArrow(ArrowNames.South)} else if (角度 < 248) {basic.showArrow(ArrowNames.SouthEast)} else if (角度 < 293) {basic.showArrow(ArrowNames.East)} else if (角度 < 338) {basic.showArrow(ArrowNames.NorthEast)} else {basic.showArrow(ArrowNames.North)}})

function calc() {ans = value / 10
ans = Math.round(ans)ans = ans + 2
if (ans > 4) {ans = 4} else if (ans < 0) {ans = 0}}(2)
(保存ファイル名：microbit-ens3-2)
let s = 0
basic.forever(function () {if (s == 0) {basic.clearScreen()} else {basic.showLeds(`
#####
#####
#####
#####
#####
`)
}
})

3.5 光センサを使った計測・制御【例題 3-7】(保存ファイル名：microbit-rei3-7)
basic.forever(function () {if (input.lightLevel() < 1) {pins.digitalWritePin(DigitalPin.P0, 1)} else {pins.digitalWritePin(DigitalPin.P0, 0)}})

input.onButtonPressed(Button.A, function () {if (s == 0) {s=1
} else {s=0}})
４．無線通信を利用したプログラム
7

basic.showIcon(IconNames.Sad)} else {basic.showIcon(IconNames.Confused)}

4.1 無線通信の利用【例題 4-1】(保存ファイル名：microbit-rei4-1)
radio.setGroup(1)input.onButtonPressed(Button.A, function () {radio.sendString("hello")})
radio.onReceivedString(function (a) {basic.showString(a)})})
function hyouji() {if (c == 0) {basic.showIcon(IconNames.SmallDiamond)} else if (c == 1) {basic.showIcon(IconNames.Scissors)} else {basic.showIcon(IconNames.Square)}}【例題 4-2】(保存ファイル名：microbit-rei4-2)
radio.setGroup(1)let a = 0radio.onReceivedNumber(function (b) {if (a == b) {basic.showIcon(IconNames.Heart)} else {basic.showIcon(IconNames.No)}})【練習 4-1】
プログラムなし
radio.setGroup(1)を変更して実施する。
4.3 信号機の制御【例題 4-4】(保存ファイル名：microbit-rei4-4)
basic.forever(function () {pins.digitalWritePin(DigitalPin.P2, 1)
basic.pause(5000)
pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 1)
basic.pause(1000)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 1)
basic.pause(4000)
pins.digitalWritePin(DigitalPin.P0, 0)})

input.onButtonPressed(Button.A, function () {a = Math.randomRange(0, 2)
basic.showNumber(a)})
input.onButtonPressed(Button.B, function () {radio.sendNumber(a)})
4.2 無線通信を利用したじゃんけんゲーム【例題 4-3】(保存ファイル名：microbit-rei4-3)
radio.setGroup(1)let a = 0
let c = 0
let d = 0c=ahyouji()input.onButtonPressed(Button.A, function () {a = Math.randomRange(0, 2)
c=ahyouji()})

【練習 4-2】(保存ファイル名：microbit-ren4-2-1)
basic.forever(function () {pins.digitalWritePin(DigitalPin.P2, 1)
basic.pause(4000)
pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 1)
basic.pause(1000)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 1)
basic.pause(5000)
pins.digitalWritePin(DigitalPin.P0, 0)})

input.onButtonPressed(Button.B, function () {radio.sendNumber(a)})
radio.onReceivedNumber(function (b) {d = (a - b + 3) % 3
if (d == 2) {basic.showIcon(IconNames.Happy)} else if (d == 1) {(保存ファイル名：microbit-ren4-2-2)
basic.forever(function () {pins.digitalWritePin(DigitalPin.P0, 1)
basic.pause(5000)
pins.digitalWritePin(DigitalPin.P0, 0)
pins.digitalWritePin(DigitalPin.P2, 1)
basic.pause(4000)
pins.digitalWritePin(DigitalPin.P2, 0)
8

basic.pause(1000)
pins.digitalWritePin(DigitalPin.P0, 0)

pins.digitalWritePin(DigitalPin.P1, 1)
basic.pause(1000)
pins.digitalWritePin(DigitalPin.P1, 0)})})【練習 4-4】(保存ファイル名：microbit-ren4-4-1)
radio.setGroup(1)pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 0)
basic.forever(function () {pins.digitalWritePin(DigitalPin.P2, 1)
radio.sendNumber(1)basic.pause(4000)
pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 1)
radio.sendNumber(2)basic.pause(1000)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 1)
radio.sendNumber(3)basic.pause(4000)
radio.sendNumber(4)basic.pause(1000)
pins.digitalWritePin(DigitalPin.P0, 0)})

4.4 無線通信による信号機の制御【例題 4-5】(保存ファイル名：microbit-rei4-5)
radio.setGroup(1)basic.forever(function () {pins.digitalWritePin(DigitalPin.P2, 1)
radio.sendNumber(2)basic.pause(4000)
pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 1)
radio.sendNumber(1)basic.pause(1000)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 1)
radio.sendNumber(0)basic.pause(5000)
pins.digitalWritePin(DigitalPin.P0, 0)})【例題 4-6】(保存ファイル名：microbit-rei4-6)
radio.setGroup(1)radio.onReceivedNumber(function(receivedNumber) {pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 0)
if (receivedNumber == 2) {pins.digitalWritePin(DigitalPin.P2, 1)} else if (receivedNumber == 1) {pins.digitalWritePin(DigitalPin.P1, 1)} else if (receivedNumber == 0) {pins.digitalWritePin(DigitalPin.P0, 1)}})(保存ファイル名：microbit-ren4-4-2)
radio.setGroup(1)radio.onReceivedNumber(function(receivedNumber) {pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 0)
if (receivedNumber == 1) {pins.digitalWritePin(DigitalPin.P0, 1)} else if (receivedNumber == 2) {pins.digitalWritePin(DigitalPin.P0, 1)} else if (receivedNumber == 3) {pins.digitalWritePin(DigitalPin.P2, 1)} else if (receivedNumber == 4) {pins.digitalWritePin(DigitalPin.P1, 1)}})【練習 4-3】(保存ファイル名：microbit-ren4-3)
radio.setGroup(1)basic.forever(function () {pins.digitalWritePin(DigitalPin.P2, 1)
radio.sendNumber(1)basic.pause(4000)
pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 1)
radio.sendNumber(2)basic.pause(1000)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 1)
radio.sendNumber(3)basic.pause(4000)
radio.sendNumber(4)【例題 4-7】(保存ファイル名：microbit-rei4-7-1)
radio.setGroup(1)pins.digitalWritePin(DigitalPin.P2, 1)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 0)
radio.onReceivedNumber(function(receivedNumber) {if (receivedNumber == 1) {basic.pause(2000)
pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 1)
9basic.showIcon(IconNames.Square)} else {pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 1)
basic.showIcon(IconNames.No)}

pins.digitalWritePin(DigitalPin.P0, 0)
basic.pause(2000)
pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 1)
radio.sendNumber(1)basic.pause(4000)
radio.sendNumber(0)pins.digitalWritePin(DigitalPin.P2, 1)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 0)})（2）
プログラムなし}})

５．アルゴリズムとプログラム(保存ファイル名：microbit-rei4-7-2)
radio.setGroup(1)pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 1)

5.1 探索【例題 5-1】(保存ファイル名：microbit-rei5-1)
let b = 0
let a: number[] = []a = [6, 4, 2, 3, 7, 1, 5]

radio.onReceivedNumber(function(receivedNumber) {if (receivedNumber == 1) {pins.digitalWritePin(DigitalPin.P2, 1)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 0)} else {pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 1)}})

input.onButtonPressed(Button.A, function () {b = a[Math.randomRange(0, 6)]
basic.showNumber(b)for (let i = 0; i <= 6; i++) {if (a[i] == b) {basic.showIcon(IconNames.Heart)basic.clearScreen()
basic.showNumber(i + 1)
basic.showString(" BanMe!")
break
}
}
})

input.onButtonPressed(Button.A, function () {radio.sendNumber(1)})

【練習 5-1】(保存ファイル名：microbit-ren5-1)
let b = 0
let sh = 0
let a: number[] = []a = [6, 4, 2, 3, 7, 1, 5]

演習問題
（1）
(保存ファイル名：microbit-ens4-1)
radio.setGroup(1)pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 1)
basic.showIcon(IconNames.No)

input.onButtonPressed(Button.A, function () {b = (b + 1) % 10
basic.showNumber(b)})

input.onButtonPressed(Button.A, function () {radio.sendNumber(1)})

input.onButtonPressed(Button.B, function () {sh = -1
for (let i = 0; i <= 6; i++) {if (a[i] == b) {sh = ibreak}}
if (sh >= 0) {radio.onReceivedNumber(function(receivedNumber) {if (receivedNumber == 1) {pins.digitalWritePin(DigitalPin.P2, 1)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 0)10

i=m+1
basic.clearScreen()
basic.pause(100)} else {basic.showIcon(IconNames.Heart)break}basic.showIcon(IconNames.Heart)basic.clearScreen()
basic.showNumber(sh + 1)
basic.showString(" BanMe!")} else {basic.showIcon(IconNames.No)
}
}

})
})
【例題 5-2】(保存ファイル名：microbit-rei5-2)
let c = ""let tmp = ""let a: string[] = []a = ["red", "green", "blue", "cyan", "magenta","yellow", "white"]【練習 5-2】
プログラムなし【例題 5-4】(保存ファイル名：microbit-rei5-4)
let c = ""let cmp = 0
let i = 0
let j = 0
let m = 0
let a: string[] = []a = ["blue", "cyan", "green", "magenta", "red","white", "yellow"]

input.onButtonPressed(Button.A, function () {c = a[Math.randomRange(0, 6)]
basic.showString(c)for (let i = 0; i <= 6; i++) {tmp = a[i]if (tmp.compare(c) == 0) {basic.showIcon(IconNames.Heart)basic.clearScreen()
basic.showString(c)basic.showNumber(i + 1)
basic.showString("BanMe!")
break
}
}
})

input.onButtonPressed(Button.A, function () {i=0
j=6
c = a[Math.randomRange(0, 6)]
basic.showString(c)while (i <= j) {m = Math.idiv(i + j, 2)
cmp = a[m].compare(c)if (cmp > 0) {basic.showArrow(ArrowNames.West)
j=m-1
basic.pause(100)basic.clearScreen()
basic.pause(100)} else if (cmp < 0) {basic.showArrow(ArrowNames.East)i=m+1
basic.pause(100)basic.clearScreen()
basic.pause(100)} else {basic.showIcon(IconNames.Heart)basic.showNumber(m + 1)
basic.showString("BanMe!")
basic.clearScreen()
break
}
}
})

【例題 5-3】(保存ファイル名：microbit-rei5-3)
let b = 0
let i = 0
let j = 0
let m = 0
let a: number[] = []a = [1, 2, 3, 4, 5, 6, 7]
input.onButtonPressed(Button.A, function () {i=0
j=6
b = a[Math.randomRange(0, 6)]
basic.showNumber(b)while (i <= j) {m = Math.idiv(i + j, 2)
if (a[m] > b) {basic.showArrow(ArrowNames.West)basic.pause(100)j=m-1
basic.clearScreen()
basic.pause(100)} else if (a[m] < b) {basic.showArrow(ArrowNames.East)basic.pause(100)5.2 整列【例題 5-5】(保存ファイル名：microbit-rei5-5)
11let a: number[] = []a = [3, 2, 1, 5, 4]
for (let j = 4; j > 0; j--) {for (let k = 0;k < j; k++) {if (a[k] > a[k + 1]) {let tmp = a[k]a[k] = a[k + 1]
a[k + 1] = tmp}}}
for (let l = 0; l <= 4; l++) {basic.showNumber(a[l])}}【練習 5-3】(保存ファイル名：microbit-ren5-3)
let a: number[] = []a = [3, 2, 1, 5, 4]
plot2()for (let i = 4; i > 0; i--) {for (let j = 0; j < i; j++) {if (a[j] > a[j + 1]) {let tmp = a[j]a[j] = a[j + 1]
a[j + 1] = tmp}}}
basic.clearScreen()plot2()

input.onButtonPressed(Button.B, () => {n=n+1
basic.showNumber(n)basic.showString(" Mai")
hanoi(n, 1, 3)
basic.showString(" End")})5.3 ハノイの塔【例題 5-6】(保存ファイル名：microbit-rei5-6)
let n = 0
input.onButtonPressed(Button.A, () => {n=1
basic.showNumber(n)basic.showString(" Mai")
hanoi(n, 1, 3)
basic.showString(" End")})function hanoi(n: number, a: number, b: number){
if (n > 1) {hanoi(n - 1, a, 6 - a - b)}basic.showIcon(IconNames.Heart)basic.pause(1000)
basic.showNumber(n)basic.pause(500)basic.showString(String.fromCharCode(64 + a))
basic.pause(500)basic.showArrow(ArrowNames.South)
basic.pause(500)basic.showString(String.fromCharCode(64 + b))
basic.pause(500)basic.clearScreen()if (n > 1) {hanoi(n - 1, 6 - a - b, b)}}function plot2() {for (let x = 0; x <= 4; x++) {for (let y = 4; y >= 5 - a[x]; y--) {led.plot(x, y)
basic.pause(100)}basic.pause(500)}}【練習 5-4】(保存ファイル名：microbit-ren5-4)
let a: string[] = []a = ["gr", "ye", "bl", "re"]for (let i = 3; i > 0; i--) {for (let j = 0; j < i; j++) {let cmp = a[j].compare(a[j + 1])
if (cmp > 0) {let tmp = a[j]a[j] = a[j + 1]
a[j + 1] = tmp}}}
for (let k = 0; k <= 3; k++) {basic.showString(a[k])【練習 5-5】
プログラムなし【再帰呼び出し（コラム）】(保存ファイル名：microbit-c53-fact)
let c = 0c = fact(5)basic.showNumber(c)function fact(n: number): number {if (n != 0) {return n * fact(n - 1)}
return 1}
12input.onButtonPressed(Button.B, function () {if (s == s0) {s = s1} else if (s == s1) {s = s2} else if (s == s2) {syohin()s = s0basic.pause(1000)
basic.clearScreen()} else {basic.showIcon(IconNames.No)}
basic.showNumber(s)})

【練習 5-6】
プログラムなし
5.4 自動販売機の状態遷移図【例題 5-7】(保存ファイル名：microbit-rei5-7)
let s0 = 0
let s1 = 0
let s = 0
s1 = 1
input.onButtonPressed(Button.A, function () {if (s == s0) {s = s1} else if (s == s1) {syohin()s = s0basic.clearScreen()} else {basic.showIcon(IconNames.No)}
basic.showNumber(s)})

function syohin() {basic.showIcon(IconNames.Target)
basic.pause(1000)}
function otsuri() {basic.showIcon(IconNames.SmallDiamond)basic.pause(1000)}

function syohin() {basic.showIcon(IconNames.Target)
basic.pause(1000)}

input.onButtonPressed(Button.AB, function () {basic.showIcon(IconNames.No)})【例題 5-8】(保存ファイル名：microbit-rei5-8)
let s0 = 0
let s1 = 0
let s2 = 0
let s = 0
s1 = 1s2 = 2演習問題(1)(保存ファイル名：microbit-ens5-1)
let a: number[] = []a = [3, 2, 1, 5, 4]
for (let i = 4; i > 0; i--) {let k = 0
for (let j = 1; j <= i; j++) {if (a[j] > a[k]) {k=j}}
let tmp = 0
tmp = a[k]a[k] = a[i]a[i] = tmp}
for (let l = 0; l <= 4; l++) {basic.showNumber(a[l])}input.onButtonPressed(Button.A, function () {if (s == s0) {s = s2} else if (s == s1) {syohin()s = s0basic.pause(1000)
basic.clearScreen()} else if (s == s2) {syohin()otsuri()s = s0basic.pause(1000)
basic.clearScreen()} else {basic.showIcon(IconNames.No)}
basic.showNumber(s)})(2)(a)(保存ファイル名：microbit-ens5-2-1)
let s0 = 0
let s1 = 0
let s2 = 0
13syohin()} else {basic.showIcon(IconNames.No)}
basic.showNumber(s)let s = 0
s1 = 1s2 = 2
input.onButtonPressed(Button.A, function () {if (s == s0) {s = s1} else if (s == s1) {s = s2} else if (s == s2) {syohin()s = s0basic.clearScreen()} else {basic.showIcon(IconNames.No)}
basic.showNumber(s)})})
function otsuri() {basic.showIcon(IconNames.SmallDiamond)basic.pause(1000)
basic.clearScreen()}function syohin() {basic.showIcon(IconNames.Target)
basic.pause(1000)
basic.clearScreen()}
input.onButtonPressed(Button.AB, function () {basic.showIcon(IconNames.No)})

function syohin() {basic.showIcon(IconNames.Target)
basic.pause(1000)}

６．通信とプログラム(2)(b)(保存ファイル名：microbit-ens5-2-1)
let s0 = 0
let s1 = 0
let s2 = 0
let s3 = 0
let s4 = 0
let s = 0
s1 = 1s2 = 2s3 = 3s4 = 4

6.1 通信の基本【例題 6-1】(保存ファイル名：microbit-rei6-1)
radio.setGroup(1)input.onButtonPressed(Button.B, function () {radio.sendString("hello")})
radio.onReceivedString(function(receivedString) {basic.showString(receivedString)})

input.onButtonPressed(Button.A, function () {if (s >= s0 && s <= s2) {s += 2} else if (s == s3) {s = s0syohin()} else if (s == s4) {s = s0syohin()otsuri()} else {basic.showIcon(IconNames.No)}
basic.showNumber(s)})

【練習 6-1】(保存ファイル名：microbit-ren6-1)
radio.setGroup(1)let a = 0a=0
input.onButtonPressed(Button.B, function () {if (a == 0) {radio.sendString("hello")} else {radio.sendString("world")}
a += 1})
radio.onReceivedString(function(receivedString) {basic.showString(receivedString)})

input.onButtonPressed(Button.B, function () {if (s >= s0 && s <= s3) {s += 1} else if (s == s4) {s = s06.2 ネットワークにおけるアドレッシング
14})【例題 6-2】(保存ファイル名：microbit-rei6-2)
radio.setGroup(1)let myAddress = ""let yourAddress = ""let message = ""let yad = 0
yourAddress = "6"
myAddress = "5"6.3 暗号通信【例題 6-3】(保存ファイル名：microbit-rei6-3)
let msg = ""let msg2 = ""let msg3 = ""let shift = 0
shift = 5

input.onButtonPressed(Button.A, function () {yad = (yad + 1) % 9
yourAddress = String.fromCharCode(yad + 48)
basic.showString(yourAddress)})
input.onButtonPressed(Button.B, function () {message = "" + yourAddress + myAddress +"hello"radio.sendString(message)})

input.onButtonPressed(Button.A, function () {msg = "dream"
msg2 = ""for (let i = 0; i <= msg.length - 1; i++) {msg2 = "" + msg2 +String.fromCharCode(msg[i].
charCodeAt(0) + shift)}radio.sendString("" + msg2)})

radio.onReceivedString(function(receivedString) {if (myAddress == receivedString[0]) {basic.showString(receivedString.substr(2, receivedString.length - 2))}})radio.onReceivedString(function(receivedString) {for (let j = 0; j <= receivedString.length - 1;j++) {
msg3 = "" + msg3 +String.fromCharCode(receivedString[j].charCodeAt(0) - shift)}
basic.showString("" + msg3)})【練習 6-2】(保存ファイル名：microbit-ren6-2)
radio.setGroup(1)let yourAddress = ""let myAddress = ""let message = ""let yad = 0
myAddress = "2"
yourAddress = "1"【練習 6-3】
プログラムなし【例題 6-4】(保存ファイル名：microbit-rei6-4)
let D = 0
let E = 0
let A = 0
let N = 0
let x = 0
let y = 0
y=0
N = 55E=3
D=7
A = 17

radio.onReceivedString(function(receivedString) {if (myAddress == receivedString[0] ||receivedString[0] == "9") {basic.showString(receivedString.
substr(2, receivedString.length - 2))}})
input.onButtonPressed(Button.A, function () {yad = (yad + 1) % 10
yourAddress = String.fromCharCode(yad + 48)
basic.showString(yourAddress)})

radio.onReceivedNumber(function(receivedNumber) {y = receivedNumber ** D % N
basic.showNumber(y)})

input.onButtonPressed(Button.B, function () {message = "" + yourAddress + myAddress +"hello"radio.sendString(message)

input.onButtonPressed(Button.A, function () {15

let m = n
for (let i = 0; i < 5; i++) {if (m % 2 == 1) c++
m = m >> 1}
if (c % 2 != 0) {n=n+1}
return n

x = A ** E % N
basic.showNumber(A)radio.sendNumber(x)})
6.4 エラー検出【例題 6-5】(保存ファイル名：microbit-rei6-5)
radio.setGroup(1)let data3 = 0
radio.onReceivedNumber(function(receivedNumber) {if (receivedNumber >= 0) {if (check_parity(receivedNumber)) {basic.showIcon(IconNames.
SmallDiamond)
Plotx(4, receivedNumber)radio.sendNumber(-1)} else {basic.showLeds(`
.....
.#.#.
..#..
.#.#.
.....
`)
Plotx(4, receivedNumber)basic.pause(2000)
radio.sendNumber(-100)}} else if (receivedNumber < -1) {basic.clearScreen()
let z = set_parity(data3)
Plotx(0, z)
radio.sendNumber(z)}})}
function check_parity(n: number): boolean {let d = 0
for (let j = 0; j < 5; j++) {if (n % 2 == 1) d++n = n >> 1}
if (d % 2 == 0)｛
return true｝else{
return false}}
function Rev(x: number, c: number) {let a = 1a = a << (c - 1)
if ((a & x) == 0) {return a | x} else {return (a ^ 31) & x}}
function Plotx(k: number, n: number) {for (let l = 4; l >= 0; l--) {if (n % 2 == 1) {led.plot(l, k)}n = n >> 1}}

input.onButtonPressed(Button.A, function () {basic.clearScreen()
let data2 = set_data()
let x = set_parity(data2)
Plotx(0, x)
radio.sendNumber(x)})

function set_data(): number {let data = 0
for (let n = 0; n < 4; n++) {data = data + Math.randomRange(0, 1)
data = data << 1}
return data}

input.onButtonPressed(Button.B, function () {basic.clearScreen()
data3 = set_data()
let y = set_parity(data3)
y = Rev(y, Math.randomRange(0, 5))
Plotx(0, y)radio.sendNumber(y)
})

演習問題
(1)
(保存ファイル名：microbit-ens6-1)

function set_parity(n: number): number {let c = 0

let wait_for_ack = 0
16

let x = set_parity(data2)
Plotx(0, x)
radio.sendNumber(x)radio.setGroup(1)input.onButtonPressed(Button.B, function () {wait_for_ack = 1
radio.sendString("hello")})})
input.onButtonPressed(Button.B, function () {basic.clearScreen()
data3 = set_data()
let y = set_parity(data3)
y = Rev(y, Math.randomRange(0, 5))
Plotx(0, y)radio.sendNumber(y)})

radio.onReceivedString(function(receivedString) {if (wait_for_ack == 0) {basic.showString(receivedString)radio.sendString("world")} else {wait_for_ack = 0
basic.showString(receivedString)}})

function set_parity(n: number): number {let c = 0
let m = n
for (let i = 0; i < 5; i++) {if (m % 2 == 1) c++
m = m >> 1}
if (c % 2 == 0) {n=n+1}
return n}(2)
プログラムなし(3)(保存ファイル名：microbit-ens6-3)
radio.setGroup(1)let data3 = 0
radio.onReceivedNumber(function(receivedNumber) {if (receivedNumber >= 0) {if (check_parity(receivedNumber)) {basic.showIcon(IconNames.
SmallDiamond)
Plotx(4, receivedNumber)radio.sendNumber(-1)} else {basic.showLeds(`
.....
.#.#.
..#..
.#.#.
.....
`)
Plotx(4, receivedNumber)basic.pause(2000)
radio.sendNumber(-100)}} else if (receivedNumber < -1) {basic.clearScreen()
let z = set_parity(data3)
Plotx(0, z)
radio.sendNumber(z)}})

function check_parity(n: number): boolean {let d = 0
for (let j = 0; j < 5; j++) {if (n % 2 == 1) d++n = n >> 1}
if (d % 2 != 0){
return true
} else {return false}}
function Rev(x: number, c: number) {let a = 1a = a << (c - 1)
if ((a & x) == 0) {return a | x} else {return (a ^ 31) & x}}
function Plotx(k: number, n: number) {for (let l = 4; l >= 0; l--) {if (n % 2 == 1) {led.plot(l, k)}n = n >> 1}}

input.onButtonPressed(Button.A, function () {basic.clearScreen()
let data2 = set_data()
17

}
})

function set_data(): number {let data = 0
for (let n = 0; n < 4; n++) {data = data + Math.randomRange(0, 1)
data = data << 1}
return data}

(保存ファイル名：microbit-hat7-1-3)
radio.setGroup(1)radio.onReceivedNumber(function(receivedNumber) {pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 0)
if (receivedNumber == 1) {pins.digitalWritePin(DigitalPin.P0, 1)} else if (receivedNumber == 5) {pins.digitalWritePin(DigitalPin.P2, 1)} else if (receivedNumber == 6) {for (let i = 0; i < 5; i++) {pins.digitalWritePin(DigitalPin.P2, 1)
basic.pause(100)pins.digitalWritePin(DigitalPin.P2, 0)
basic.pause(100)}} else if (receivedNumber == 7) {pins.digitalWritePin(DigitalPin.P0, 1)} else {pins.digitalWritePin(DigitalPin.P0, 1)}})

７．総合問題7.1

信号機（スクランブル交差点）(保存ファイル名：microbit-hat7-1-1)
radio.setGroup(1)basic.forever(function () {pins.digitalWritePin(DigitalPin.P2, 1)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 0)
radio.sendNumber(1)basic.pause(4000)
pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 1)
radio.sendNumber(2)basic.pause(1000)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 1)
radio.sendNumber(3)basic.pause(4000)
radio.sendNumber(4)basic.pause(1000)
radio.sendNumber(5)basic.pause(3000)
radio.sendNumber(6)basic.pause(1000)
radio.sendNumber(7)basic.pause(1000)})

7.2 じゃんけんゲーム（3 人対戦）
(保存ファイル名：microbit-hat7-2)
radio.setGroup(1)let a = 0
let b = 0
let c = 0
let count = 0
let d: number[] = []d = [0, 0]
radio.onReceivedNumber(function(receivedNumber) {d[count] = receivedNumber
count += 1
if (count >= 2) {b = d[0]c = d[1]if (a == b && a == c && b == c || a != b &&a != c && b != c) {basic.showIcon(IconNames.Confused)} else if ((a + 1) % 3 == b || (a + 1) % 3 == c) {basic.showIcon(IconNames.Happy)} else {basic.showIcon(IconNames.Sad)}basic.pause(500)count = 0}})

(保存ファイル名：microbit-hat7-1-2)
radio.setGroup(1)radio.onReceivedNumber(function(receivedNumber) {pins.digitalWritePin(DigitalPin.P2, 0)
pins.digitalWritePin(DigitalPin.P1, 0)
pins.digitalWritePin(DigitalPin.P0, 0)
if (receivedNumber == 1) {pins.digitalWritePin(DigitalPin.P0, 1)} else if (receivedNumber == 3) {pins.digitalWritePin(DigitalPin.P2, 1)} else if (receivedNumber == 4) {pins.digitalWritePin(DigitalPin.P1, 1)} else if (receivedNumber == 5) {pins.digitalWritePin(DigitalPin.P0, 1)} else {pins.digitalWritePin(DigitalPin.P0, 1)
18

hanoi(n - 1, a, 6 - a - b)}basic.showIcon(IconNames.Heart)basic.pause(1000)
basic.showNumber(n)basic.pause(500)basic.showString(String.fromCharCode (64 + a))
basic.pause(500)basic.showArrow(ArrowNames.South)
basic.pause(500)basic.showString(String.fromCharCode(64 + b))
basic.pause(500)basic.clearScreen()
send_pop(a)send_push(b, n)
if (n > 1) {hanoi(n - 1, 6 - a - b, b)}

input.onButtonPressed(Button.A, function () {a = Math.randomRange(0, 2)
c=ahyouji2()})
input.onButtonPressed(Button.AB, function () {control.reset()})
input.onButtonPressed(Button.B, function () {radio.sendNumber(a)})
function hyouji2() {if (c == 0) {basic.showIcon(IconNames.SmallDiamond)} else if (c == 1) {basic.showIcon(IconNames.Scissors)} else {basic.showIcon(IconNames.Square)}}}(保存ファイル名：microbit-hat7-3-2)
radio.setGroup(1)let cmd = 0
let o = 0
let data = 0
let bo2: game.LedSprite[] = []let bar: number[] = []bar = [0, 0, 0, 0, 0]
let board1: game.LedSprite[] = []let board3: game.LedSprite[] = []let board5: game.LedSprite[] = []mynum = 4

7.3 ハノイの塔（複数台による表示）(保存ファイル名：microbit-hat7-3-1)
radio.setGroup(1)let n = 0
input.onButtonPressed(Button.A, () => {n=3
basic.showNumber(n)basic.showString(" Mai")
hanoi(n, 1, 3)
basic.showString(" End")})

input.onButtonPressed(Button.A, function () {mynum = (mynum + 1) % 4
basic.showNumber(mynum)})
input.onButtonPressed(Button.B, function () {radio.sendNumber(100 + 20)})

input.onButtonPressed(Button.B, () => {n=n+1
basic.showNumber(n)basic.showString(" Mai")
hanoi(n, 1, 3)
basic.showString(" End")})

input.onButtonPressed(Button.AB, function () {basic.clearScreen()if (mynum == 1) {push(3)push(2)push(1)}})function send_pop(bar: number) {let msg = (bar * 100) + 20
radio.sendNumber(msg)}basic.forever(function () {led.plot(mynum - 1, 0)})

function send_push(bar: number, n: number) {let msg = (bar * 100) + 10 + n
radio.sendNumber(msg)}radio.onReceivedNumber(function(receivedNumber) {data = receivedNumber

function hanoi(n: number, a: number, b: number) {if (n > 1) {19

}else if (bar[i + 1] != 0) {bar[i] = n
break}if (Math.floor(data / 100) == mynum) {cmd = Math.floor(data % 100 / 10)
n = Math.floor(data % 10)
if (cmd == 1) {push(n)} else if (cmd == 2) {pop2()}}}}}
function up(b: game.LedSprite[]) {for (let l = 0; l < b.length; l++) {b[l].change(LedSpriteProperty.Y, -1)}}})
function pop2() {for (let j = 0; j <= 5 - 1; j++) {if (bar[j] != 0) {o = bar[j]if (o == 1) {bo2 = board1} else if (o == 2) {bo2 = board3} else if (o == 3) {bo2 = board5}
bar[j] = 0
for (let k = 0; k <= bo2.length - 1; k++) {bo2[k].delete()
}
break
}
}
}

function down(b: game.LedSprite[]) {basic.pause(500)for (let m = 0; m < b.length; m++) {b[m].change(LedSpriteProperty.Y, 1)}}
7.4 通信プログラム（不具合問題）(保存ファイル名：microbit-hat7-4)
radio.setGroup(1)let sstring = ""input.onButtonPressed(Button.B, function () {sstring = "s" + "hello"
radio.sendString(sstring)})

function push(n: number) {let bo: game.LedSprite[] = []if (n == 1) {board1 = [game.createSprite(2, 0)]bo = board1}
else if (n == 2) {board3 = [game.createSprite(1, 0),
game.createSprite(2, 0),
game.createSprite(3, 0)]bo = board3}
else if (n == 3) {board5 = [game.createSprite(0, 0),
game.createSprite(1, 0),
game.createSprite(2, 0),
game.createSprite(3, 0),
game.createSprite(4, 0)]bo = board5}for (let i = 0; i < 5; i++) {if (i != 0 && bar[i] == 0) {down(bo)if (i == 4) {bar[i] = n

radio.onReceivedString(function(receivedString) {if (receivedString[0] == "s") {basic.showString(receivedString.
substr(1, receivedString.length - 1))
sstring = "a" + "world"
radio.sendString(sstring)} else {basic.showString(receivedString.
substr(1, receivedString.length - 1))

20