2020.6.30

micro:bit で学ぶプログラミング～Python プログラム集～
目次
１． プログラミングの基礎 ...................................................... 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 進数への変換 ................................................... 4演習問題 ........................................................................ 5
３．センサによる計測・制御プログラム ............................................ 5
3.1 micro:bit の各種センサと制御 ................................................ 5
3.2 音の制御 ................................................................... 5
3.3 傾きセンサを使った計測・制御 ............................................... 6
3.4 地磁気センサを使った計測・制御 ............................................. 6
3.5 光センサを使った計測・制御 ................................................. 7演習問題 ........................................................................ 7４．無線通信を利用したプログラム ................................................ 7
4.1 無線通信の利用 ............................................................. 7
4.2 無線通信を利用したじゃんけんゲーム ......................................... 8
4.3 信号機の制御 ............................................................... 8
4.4 無線通信による信号機の制御 ................................................. 9演習問題 ....................................................................... 10
５．アルゴリズムとプログラム ................................................... 10
5.1 探索 ...................................................................... 10
5.2 整列 ...................................................................... 11
5.3 ハノイの塔 ................................................................ 12
5.4 自動販売機の状態遷移図 .................................................... 12
演習問題 ....................................................................... 13６．通信とプログラム ........................................................... 14
6.1 通信の基本 ................................................................ 14
6.2 ネットワークにおけるアドレッシング ........................................ 14
6.3 暗号通信 .................................................................. 15
6.4 エラー検出 ................................................................ 16
演習問題 ....................................................................... 16
７．総合問題 ................................................................... 16
7.1 信号機（スクランブル交差点） .............................................. 16
7.2 じゃんけんゲーム（3 人対戦） ............................................... 17
7.3 ハノイの塔（複数台による表示） ............................................ 17
7.4 通信プログラム（不具合問題） .............................................. 18

2020.6.30【注意事項】＜JavaScript のプログラム集＞
micor:bit では，ブロックから JavaScript へ自動変換されますが，JavaScript の変数や関数の名称・順序は，自動変換されたプログラムと異なる場合があります。また，「 micro:bit
で学ぶプログラミング－ブロック型，JavaScript そして Python へ－」の教科書で記載しているファイル名，例えば，rei○○は，JavaScript のプログラム集では，実際に保存されているファイル名，microbit-rei○○.hex になっています。
なお，本書で掲載している JavaScript のプログラムは，2019 年 3 月末段階で動作確認したプログラムであり，今後の仕様変更で関数名などが変わっている場合があるかもしれま
せん。
＜Python のプログラム集＞
2020 年リリース（2020 年 6 月 12 日公表）のバージョンでは，micor:bit は，ブロック
か ら Python へ の 自 動 変 換 も 追 加 さ れ ま し た 。 こ の プ ロ グ ラ ム 集 で は ， 自 動 変 換 さ れ た
Python のプログラムを示します。JavaScript と同様，変数や関数の名称・順序は，自動変換されたプログラムと異なる場合があります。
また，Python の関数等の仕様が異なり，5 章，6 章，7 章で自動変換できない例題がいく
つかありますが，その場合は，各例題の箇所に変換できないことを記載しています。
なお，本書で掲載している Python のプログラムは，2020 年 6 月末段階で動作確認したプログラムであり，今後の仕様変更で関数名などが変わっている場合があるかもしれません。
＜MicroPython のプログラム集＞
この教科書で作成している MicroPyhon のプログラム集と，自動変換された Python のプ
ログラム集は，一部プログラムが異なります。MicroPyhon のプログラム集は，JavaScriptのプログラム(2019 年 3 月末)のプログラムを，Python の言語仕様に従った記述をしています。Python のプログラム集では，ブラックボックスになっている個所など，詳細な内容を知りたい方は，MicroPyhon のプログラム集が参考になると思います。
いずれのプログラム集のプログラムも，読者の方々が，ブロックの動作確認をする際，
JavaScript，Python のプログラムも確認していただけると幸いです。
参考文献
1) micro:bit の公式 Web サイト（日本語）：https://microbit.org/ja/
micro:bit の冒険を始めよう

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

１． プログラミングの基礎【練習 1-3】(保存ファイル名：microbit-ren1-3-1)
for x in range(5):led.plot(x, x)
for y in range(5):led.plot(y, 4 - y)

1.1 micro:bit の基本操作【例題 1-1】(保存ファイル名：microbit-rei1-1-1)
basic.show_leds("""
.#.#.
#####
#####
.###.
..#..
""")

(保存ファイル名：microbit-ren1-3-2)
for x in range(5):led.plot(x, x)
led.plot(x, 4 - x)
【例題 1-5】(保存ファイル名：microbit-rei1-5)
for x in range(5):for y in range(5):led.plot(x, y)
basic.pause(100)(保存ファイル名：microbit-rei1-1-2)
def on_forever():
basic.show_leds("""
.#.#.
#####
#####
.###.
..# ..
""")
basic.pause(500)basic.clear_screen()
basic.pause(500)basic.forever(on_forever)【例題 1-6】(保存ファイル名：microbit-rei1-6)
x=0
while x <= 4:
led.plot(x, 4 - x)x += 1
1.3 プログラムの基礎（分岐）1.2 プログラムの基礎（順次，繰り返し）【例題 1-7】(保存ファイル名：microbit-rei1-7)
c=0
def on_forever():
global cc = randint(0, 1)
basic.show_number(c)if c == 0:
basic.show_icon(IconNames.SMALL_DIAM
OND)
else:
basic.show_icon(IconNames.SQUARE)
basic.forever(on_forever)【例題 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】(保存ファイル名：microbit-rei1-3)
for y in range(5):led.plot(2, y)【例題 1-8】(保存ファイル名：microbit-rei1-8)
c=0
def on_forever():
global cc = randint(0, 2)
basic.show_number(c)if c == 0:
basic.show_icon(IconNames.SMALL_DIAM
OND)elif c == 1:
basic.show_icon(IconNames.SCISSORS)
else:
basic.show_icon(IconNames.SQUARE)
basic.forever(on_forever)【練習 1-1】(保存ファイル名：microbit-ren1-1)
for x in range(5):led.plot(x, 2)【例題 1-4】(保存ファイル名：microbit-rei1-4)
for x in range(5):led.plot(x, 4 - x)
【練習 1-2】(保存ファイル名：microbit-ren1-2)
for x in range(5):led.plot(x, x)
1２．プログラミングの応用（関数，配列）【練習 1-4】
プログラムなし

2.1 じゃんけんゲーム
演習問題【例題 2-1】(保存ファイル名：microbit-rei2-1)
a=0
b=0
c=0

(1)
(保存ファイル名：microbit-ens1-1)
for x in range(5):for y in range(5):if x == y:led.plot(x, y)
basic.pause(100)def on_button_pressed_a():global a, c
a = randint(0, 2)
basic.show_number(a)c=ahyouji()input.on_button_pressed(Button.A,on_button_pressed_a)(2)(a)(保存ファイル名：microbit-ens1-2-1)
for x in range(5):for y in range(5):if 4 - x <= y:led.plot(x, y)
basic.pause(500)

def on_button_pressed_b():
global b, c
b = randint(0, 2)
basic.show_number(b)c= bhyouji()input.on_button_pressed(Button.B, on_button_pr
essed_b)(2)(b)(保存ファイル名：microbit-ens1-2-2)
for x in range(5):for y in range(5):if x == y or 4 - x == y:led.plot(x, y)
basic.pause(500)

def hyouji():
if c == 0:
basic.show_icon(IconNames.
SMALL_DIAMOND)
elif c == 1:
basic.show_icon(IconNames.SCISSORS)
else:
basic.show_icon(IconNames.SQUARE)(2)(c)(保存ファイル名：microbit-ens1-2-3)
for x in range(5):for y in range(5):if x == 2 or y == 2:
led.plot(x, y)
basic.pause(500)【例題 2-2】(保存ファイル名：microbit-rei2-2)
a=0
b=0
c=0
d=0

(3)
(保存ファイル名：microbit-ens1-3)
c=0
def on_gesture_shake():global cc = randint(0, 2)
basic.show_number(c)if c == 0:
basic.show_icon(IconNames.SMALL_SQU
ARE)elif c == 1:
basic.show_icon(IconNames.SCISSORS)
else:
basic.show_icon(IconNames.SQUARE)
input.on_gesture(Gesture.SHAKE, on_gesture_shake)

def on_button_pressed_a():global a, c
a = randint(0, 2)
c=ahyouji()input.on_button_pressed(Button.A, on_button_pr
essed_a)def on_button_pressed_ab():global dd = (a - b + 3) % 3
if d == 2:
basic.show_string("A")
elif d == 1:2

basic.show_string("B")
else:
basic.show_string("AB")input.on_button_pressed(Button.AB, on_button_pressed_ab)

2.2 数あてゲーム【例題 2-3】(保存ファイル名：microbit-rei2-3)
kouho = 0
kotae = 0
kotae = randint(0, 2)
basic.show_number(kotae)

def on_button_pressed_b():
global b, c
b = randint(0, 2)
c= bhyouji()input.on_button_pressed(Button.B, on_button_pr
essed_b)

def on_button_pressed_a():global kouho
kouho = randint(0, 2)
basic.show_number(kouho)
input.on_button_pressed(Button.A, on_button_pr
essed_a)【練習 2-1】(保存ファイル名：microbit-ren2-1)
a=0
b=0
c=0
d=0

def on_button_pressed_b():
if kouho == kotae:
basic.show_icon(IconNames.HEART)
else:
basic.show_icon(IconNames.NO)
input.on_button_pressed(Button.B, on_button_pr
essed_b)

def on_button_pressed_a():global a, c
a = randint(0, 2)
c=ahyouji()input.on_button_pressed(Button.A, on_button_pr
essed_a)【例題 2-4】(保存ファイル名：microbit-rei2-4)
kouho = 0
kotae = 0
kotae = randint(0, 4)
basic.show_number(kotae)kouho = randint(0, 4)
basic.show_number(kouho)
hantei()def on_button_pressed_ab():global dd = (a - b + 3) % 3
if d == 2:
basic.show_string("A" + "Kati")
elif d == 1:
basic.show_string("B" + "Kati")
else:
basic.show_string("Hikiwake")
input.on_button_pressed(Button.AB, on_button_pressed_ab)

def hantei():
if kouho > kotae:
basic.show_arrow(ArrowNames.SOUTH)
basic.clear_screen()
elif kouho < kotae:
basic.show_arrow(ArrowNames.NORTH)
basic.clear_screen()else:
basic.show_number(kouho)
basic.clear_screen()
basic.show_string("Hit")

def on_button_pressed_b():
global b, c
b = randint(0, 2)
c= bhyouji()input.on_button_pressed(Button.B, on_button_pr
essed_b)

def on_button_pressed_a():global kouho
kouho += 1
hantei()input.on_button_pressed(Button.A, on_button_pr
essed_a)def hyouji():
if c == 0:
basic.show_icon(IconNames.SMALL_DIAM
OND)elif c == 1:
basic.show_icon(IconNames.SCISSORS)
else:
basic.show_icon(IconNames.SQUARE)
basic.clear_screen()

def on_button_pressed_b():
global kouho
kouho += -1
hantei()3

input.on_button_pressed(Button.B, on_button_pr
essed_b)

basic.show_number(c)def sum(a: number, b: number):s=0
s=a+ breturn s【練習 2-2】(保存ファイル名：microbit-ren2-2)
kouho = 0
kotae = 0
kotae = randint(0, 4)
if kouho == kotae:kouho = randint(0, 4)
basic.show_number(kouho)
hantei()【練習 2-3】(保存ファイル名：microbit-ren2-3-1)
a: List[number] = []x=0a[0] = 3a[1] = 2a[2] = 1a[3] = 5a[4] = 4
for z in range(5):basic.show_number(a[z])def hantei():
if kouho > kotae:
basic.show_arrow(ArrowNames.SOUTH)
basic.clear_screen()
elif kouho < kotae:
basic.show_arrow(ArrowNames.NORTH)
basic.clear_screen()else:
basic.show_number(kouho)
basic.clear_screen()
basic.show_string("Hit")(保存ファイル名：microbit-ren2-3-2)
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])}def on_button_pressed_a():global kouho
kouho += 1
hantei()input.on_button_pressed(Button.A, on_button_pr
essed_a)【練習 2-4】(保存ファイル名：microbit-ren2-4)
a: List[number] = []a = [1, 2]
b: List[str] = []b = ["AB", "cd"]def on_button_pressed_b():
global kouho
kouho += -1
hantei()input.on_button_pressed(Button.B, on_button_pr
essed_b)

for i in range(2):basic.show_number(a[i])basic.clear_screen()
for j in range(2):basic.show_string("" + b[j])2.3 グラフの作成【例題 2-6】(保存ファイル名：microbit-rei2-6)
y=0
a = [3, 2, 1, 5, 4]【例題 2-5】(保存ファイル名：microbit-rei2-5)
a = [3, 2, 1, 5, 4]
for i in range(5):basic.show_number(a[i])basic.clear_screen()
for y in range(5):x=0while x <= a[y] - 1:
led.plot(x, y)x += 1for x in range(5):z=0while z <= a[x] - 1:
y=4-z
led.plot(x, y)
basic.pause(500)z += 1
2.4 10 進数から 2 進数への変換【関数の引数と戻り値（コラム）】(保存ファイル名：microbit-c23-kansu)
c=0
c = sum(3, 5)
basic.show_string("3+5=")【例題 2-7】(保存ファイル名：microbit-rei2-7)4

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

xp: List[number] = []xp = [0, 0, 0, 0, 0]
for k in range(32):
DtoB(k)def DtoB(k: number):j=4
while j >= 0:
xp[j] = k % 2
k = Math.floor(k / 2)
j -= 1
Plot()

plot(0)basic.clear_screen()plot(1)
def plot(g: number):global y
if g == 0:
for z in range(5):b=0while b <= a[z] - 1:
led.plot(b, z)
basic.pause(500)b += 1else:
for c in range(5):d=0while d <= a[c] - 1:
y=4- dled.plot(c, y)basic.pause(500)d += 1

def Plot():i=4
while i >= 0:
if xp[i] == 1:x=ifor y in range(5):led.plot(x, y)i -= 1
basic.pause(1000)
basic.clear_screen()
演習問題(1)(保存ファイル名：microbit-ens2-1)
y2 = 0a: List[number] = []a = [3, 2, 1, 5, 4]plot()

３．センサによる計測・制御プログラム
3.1 micro:bit の各種センサと制御【例題 3-1】(保存ファイル名：microbit-rei3-1)
def on_forever():
basic.show_number(input.light_level())
basic.clear_screen()
basic.pause(1000)
basic.forever(on_forever)def plot():
Graph_H()
basic.clear_screen()
Graph_V()
def Graph_V():
global y2
for x in range(5):z=0while z <= a[x] - 1:
y2 = 4 - z
led.plot(x, y2)basic.pause(500)【練習 3-1】(保存ファイル名：microbit-ren3-1)
def on_forever():
basic.show_number(input.acceleration(Dimen
sion.X))
basic.clear_screen()
basic.pause(1000)
basic.forever(on_forever)def Graph_H():
for y in range(5):x2 = 0while x2 <= a[y] - 1:
led.plot(x2, y)basic.pause(500)x2 += 1z += 13.2 音の制御【例題 3-2】(保存ファイル名：microbit-rei3-2)
def on_forever():music.play_tone(262, music.beat(BeatFractio
n.WHOLE))5

music.play_tone(294, music.beat(BeatFractio
n.WHOLE))
music.play_tone(330, music.beat(BeatFractio
n.WHOLE))
music.rest(music.beat(BeatFraction.WHOLE))
basic.forever(on_forever)music.start_melody(music.built_in_melody(Melodies.POWER_UP), MelodyOptions.ONCE)
input.on_button_pressed(Button.B, on_button_pr
essed_b)
3.3 傾きセンサを使った計測・制御【例題 3-4】(保存ファイル名：microbit-rei3-4)
def on_forever():
if input.rotation(Rotation.ROLL) < -10:
music.ring_tone(262)elif input.rotation(Rotation.ROLL) > 10:music.ring_tone(294)else:music.rest(music.beat(BeatFraction.SIXT
EENTH))
basic.forever(on_forever)【練習 3-2】(保存ファイル名：microbit-ren3-2)
def on_forever():music.play_tone(262, music.beat(BeatFractio
n.WHOLE))
music.play_tone(262 * 2, music.beat(BeatFra
ction.WHOLE))
music.play_tone(262 * 4, music.beat(BeatFra
ction.WHOLE))
basic.forever(on_forever)【練習 3-3】(保存ファイル名：microbit-ren3-3)
def on_forever():music.play_tone(262, music.beat(BeatFractio
n.WHOLE))
music.play_tone(262 * 2, music.beat(BeatFra
ction.WHOLE))
music.play_tone(262 * 3, music.beat(BeatFra
ction.WHOLE))
music.play_tone(262 * 4, music.beat(BeatFra
ction.WHOLE))
music.play_tone(262 * 5, music.beat(BeatFra
ction.WHOLE))
music.play_tone(523, music.beat(BeatFractio
n.WHOLE))
music.play_tone(523 * 2, music.beat(BeatFra
ction.WHOLE))
music.play_tone(523 * 3, music.beat(BeatFra
ction.WHOLE))
basic.forever(on_forever)【練習 3-5】(保存ファイル名：microbit-ren3-5)
def on_forever():music.ring_tone(2 * input.rotation(Rotation.R
OLL) + 262)
basic.forever(on_forever)
3.4 地磁気センサを使った計測・制御【例題 3-5】(保存ファイル名：microbit-rei3-5)
角度 = 0def on_forever():
global 角度
角度 = input.compass_heading()
if 角度 < 45:basic.show_string("N")
elif 角度 > 315:basic.show_string("N")
else:basic.clear_screen()
basic.forever(on_forever)【例題 3-3】(保存ファイル名：microbit-rei3-3)
def on_button_pressed_a():music.start_melody(music.built_in_melody(Melodies.POWER_UP), MelodyOptions.ONCE)
input.on_button_pressed(Button.A, on_button_pr
essed_a)【例題 3-6】(保存ファイル名：microbit-rei3-6)
角度 = 0def on_forever():
global 角度
角度 = input.compass_heading()
if 角度 < 45:basic.show_arrow(ArrowNames.NORTH)
elif 角度 < 135:
basic.show_arrow(ArrowNames.WEST)
elif 角度 < 225:
basic.show_arrow(ArrowNames.SOUTH)
elif 角度 < 315:basic.show_arrow(ArrowNames.EAST)else:【練習 3-4】(保存ファイル名：microbit-ren3-4)
def on_button_pressed_a():music.start_melody(music.built_in_melody(Melodies.DADADADUM), MelodyOptions.ONCE)
input.on_button_pressed(Button.A, on_button_pr
essed_a)def on_button_pressed_b():6

basic.show_arrow(ArrowNames.NORTH)
basic.forever(on_forever)calc()
ｘ = ans
value = input.rotation(Rotation.PITCH)
calc()
ｙ = ans
led.plot(ｘ, ｙ)
basic.forever(on_forever)【練習 3-6】(保存ファイル名：microbit-ren3-6)
角度 = 0def on_forever():
global 角度
角度 = input.compass_heading()
if 角度 < 23:basic.show_arrow(ArrowNames.NORTH)
elif 角度 < 68:
basic.show_arrow(ArrowNames.NORTH_W
EST)elif 角度 < 113:
basic.show_arrow(ArrowNames.WEST)
elif 角度 < 158:
basic.show_arrow(ArrowNames.SOUTH_W
EST)elif 角度 < 203:
basic.show_arrow(ArrowNames.SOUTH)
elif 角度 < 248:
basic.show_arrow(ArrowNames.SOUTH_E
AST)
elif 角度 < 293:
basic.show_arrow(ArrowNames.EAST)
elif 角度 < 338:
basic.show_arrow(ArrowNames.NORTH_E
AST)
else:
basic.show_arrow(ArrowNames.NORTH)
basic.forever(on_forever)def calc():
global ans
ans = value / 10
ans = Math.round(ans)ans = ans + 2
if ans > 4:
ans = 4elif ans < 0:ans = 0（2）
(保存ファイル名：microbit-ens3-2)
s=0
def on_button_pressed_a():global s
if s == 0:
s=1
else:
s=0
input.on_button_pressed(Button.A, on_button_pr
essed_a)def on_forever():
if s == 0:
basic.clear_screen()else:
basic.show_leds("""
#####
#####
#####
#####
#####
""")
basic.forever(on_forever)

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

４．無線通信を利用したプログラム

演習問題

4.1 無線通信の利用（1）(保存ファイル名：microbit-ens3-1)
ans = 0value = 0
ｘ=0
ｙ=0

【例題 4-1】(保存ファイル名：microbitrei4-1)
radio.set_group(1)def on_button_pressed_a():radio.send_string("hello")
input.on_button_pressed(Button.A, on_button_pr
essed_a)def on_forever():
global value, ｘ, ｙ
basic.clear_screen()value = input.rotation(Rotation.ROLL)

def on_received_string_deprecated(a):basic.show_string("" + (a))
7radio.on_received_string_deprecated(on_received_string_deprecated)

def on_button_pressed_b():
radio.send_number(a)input.on_button_pressed(Button.B, on_button_pr
essed_b)4.2 無線通信を利用したじゃんけんゲーム

def hyouji():
if c == 0:
basic.show_icon(IconNames.SMALL_DIAM
OND)elif c == 1:
basic.show_icon(IconNames.SCISSORS)
else:
basic.show_icon(IconNames.SQUARE)【例題 4-2】(保存ファイル名：microbit-rei4-2)
radio.set_group(1)a=0
def on_received_number_deprecated(b):if a == b:basic.show_icon(IconNames.HEART)
else:
basic.show_icon(IconNames.NO)
radio.on_received_number_deprecated(on_receiv
ed_number_deprecated)【練習 4-1】
プログラムなし
radio.set_group(1)を変更して実施する。

def on_button_pressed_a():global aa = randint(0, 2)
basic.show_number(a)input.on_button_pressed(Button.A, on_button_pr
essed_a)

4.3 信号機の制御【例題 4-4】(保存ファイル名：microbit-rei4-4)
def on_forever():pins.digital_write_pin(DigitalPin.P2, 1)
basic.pause(5000)
pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P1, 1)
basic.pause(1000)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 1)
basic.pause(4000)
pins.digital_write_pin(DigitalPin.P0, 0)
basic.forever(on_forever)def on_button_pressed_b():
radio.send_number(a)input.on_button_pressed(Button.B, on_button_pr
essed_b)【例題 4-3】(保存ファイル名：microbit-rei4-3)
radio.set_group(1)
a=0
c=0
d=0
c=a
hyouji()【練習 4-2】(保存ファイル名：microbit-ren4-2-1)
def on_forever():pins.digital_write_pin(DigitalPin.P2, 1)
basic.pause(4000)
pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P1, 1)
basic.pause(1000)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 1)
basic.pause(5000)
pins.digital_write_pin(DigitalPin.P0, 0)
basic.forever(on_forever)def on_received_number_deprecated(b):global dd = (a - b + 3) % 3
if d == 2:
basic.show_icon(IconNames.HAPPY)
elif d == 1:
basic.show_icon(IconNames.SAD)
else:
basic.show_icon(IconNames.CONFUSED)
radio.on_received_number_deprecated(on_receiv
ed_number_deprecated)(保存ファイル名：microbit-ren4-2-2)
def on_forever():pins.digital_write_pin(DigitalPin.P0, 1)
basic.pause(5000)
pins.digital_write_pin(DigitalPin.P0, 0)
pins.digital_write_pin(DigitalPin.P2, 1)
basic.pause(4000)
pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P1, 1)

def on_button_pressed_a():global a, c
a = randint(0, 2)
c=ahyouji()input.on_button_pressed(Button.A, on_button_pr
essed_a)8basic.pause(1000)
pins.digital_write_pin(DigitalPin.P1, 0)
basic.forever(on_forever)radio.send_number(2)basic.pause(1000)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 1)
radio.send_number(3)basic.pause(4000)
radio.send_number(4)basic.pause(1000)
pins.digital_write_pin(DigitalPin.P0, 0)
basic.forever(on_forever)

4.4 無線通信による信号機の制御【例題 4-5】(保存ファイル名：microbit-rei4-5)
radio.set_group(1)def on_forever():pins.digital_write_pin(DigitalPin.P2, 1)
radio.send_number(2)basic.pause(4000)
pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P1, 1)
radio.send_number(1)basic.pause(1000)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 1)
radio.send_number(0)basic.pause(5000)
pins.digital_write_pin(DigitalPin.P0, 0)
basic.forever(on_forever)(保存ファイル名：microbit-ren4-4-2)
radio.set_group(1)def on_received_number_deprecated(receivedNu
mber):
pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 0)
if receivedNumber == 1:
pins.digital_write_pin(DigitalPin.P0, 1)
elif receivedNumber == 2:
pins.digital_write_pin(DigitalPin.P0, 1)
elif receivedNumber == 3:
pins.digital_write_pin(DigitalPin.P2, 1)
elif receivedNumber == 4:
pins.digital_write_pin(DigitalPin.P1, 1)
radio.on_received_number_deprecated(on_receiv
ed_number_deprecated)【例題 4-6】(保存ファイル名：microbit-rei4-6)
radio.set_group(1)def on_received_number_deprecated(receivedNu
mber):
pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 0)
if receivedNumber == 2:
pins.digital_write_pin(DigitalPin.P2, 1)
elif receivedNumber == 1:
pins.digital_write_pin(DigitalPin.P1, 1)
elif receivedNumber == 0:
pins.digital_write_pin(DigitalPin.P0, 1)
radio.on_received_number_deprecated(on_receiv
ed_number_deprecated)【例題 4-7】(保存ファイル名：microbit-rei4-7-1)（車道用）
radio.set_group(1)pins.digital_write_pin(DigitalPin.P2, 1)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 0)
def on_received_number_deprecated(receivedNu
mber):
if receivedNumber == 1:
basic.pause(2000)
pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P1, 1)
pins.digital_write_pin(DigitalPin.P0, 0)
basic.pause(2000)
pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 1)
radio.send_number(1)basic.pause(4000)
radio.send_number(0)pins.digital_write_pin(DigitalPin.P2, 1)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 0)
radio.on_received_number_deprecated(on_receiv
ed_number_deprecated)【練習 4-3】
プログラムなし【練習 4-4】(保存ファイル名：microbit-ren4-4-1)
radio.set_group(1)pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 0)
def on_forever():pins.digital_write_pin(DigitalPin.P2, 1)
radio.send_number(1)basic.pause(4000)
pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P1, 1)
9

(保存ファイル名：microbit-rei4-7-2)（歩行者用）
radio.set_group(1)pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 1)

a: List[number] = []a = [6, 4, 2, 3, 7, 1, 5]
def on_button_pressed_a():global bb = (b + 1) % 10
basic.show_number(b)input.on_button_pressed(Button.A, on_button_pr
essed_a)def on_received_number_deprecated(receivedNu
mber):
if receivedNumber == 1:
pins.digital_write_pin(DigitalPin.P2, 1)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 0)
else:
pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 1)
radio.on_received_number_deprecated(on_receiv
ed_number_deprecated)

def on_button_pressed_b():
global sh
sh = -1
for i in range(7):if a[i] == b:
sh = ibreak
if sh >= 0:
basic.show_icon(IconNames.HEART)
basic.clear_screen()
basic.show_number(sh + 1)
basic.show_string(" BanMe!")
else:
basic.show_icon(IconNames.NO)
input.on_button_pressed(Button.B, on_button_pr
essed_b)

def on_button_pressed_a():radio.send_number(1)input.on_button_pressed(Button.A, on_button_pr
essed_a)
演習問題【例題 5-2】(保存ファイル名：microbit-rei5-2)
tmp = ""
c = ""
a: List[str] = []a = ["red", "green", "blue", "cyan", "magenta", "yellow", "white"]

プログラムなし
５．アルゴリズムとプログラム
5.1 探索【例題 5-1】(保存ファイル名：microbit-rei5-1)
b=0
a: List[number] = []a = [6, 4, 2, 3, 7, 1, 5]

def on_button_pressed_a():global c, tmp
c = a[randint(0, 6)]
basic.show_string(c)for i in range(7):tmp = a[i]if tmp.compare(c) == 0:
basic.show_icon(IconNames.HEART)
basic.clear_screen()
basic.show_string(c)basic.show_number(i + 1)
basic.show_string("BanMe!")
break
input.on_button_pressed(Button.A, on_button_pr
essed_a)def on_button_pressed_a():global bb = a[randint(0, 6)]
basic.show_number(b)for i in range(7):if a[i] == b:basic.show_icon(IconNames.HEART)
basic.clear_screen()
basic.show_number(i + 1)
basic.show_string(" BanMe!")
break
input.on_button_pressed(Button.A, on_button_pr
essed_a)【例題 5-3】(保存ファイル名：microbit-rei5-3)
b=0
i=0
j=0
m=0
a: List[number] = []【練習 5-1】(保存ファイル名：microbit-ren5-1)
sh = 0b=010

a = [1, 2, 3, 4, 5, 6, 7]

basic.show_arrow(ArrowNames.EAST)
i= m+1
basic.pause(100)basic.clear_screen()
basic.pause(100)else:basic.show_icon(IconNames.HEART)
basic.show_number(m + 1)
basic.show_string("BanMe!")
basic.clear_screen()
break
input.on_button_pressed(Button.A, on_button_pr
essed_a)def on_button_pressed_a():global i, j, b, m
i=0
j=6
b = a[randint(0, 6)]
basic.show_number(b)while i <= j:m = Math.idiv(i + j, 2)
if a[m] > b:basic.show_arrow(ArrowNames.WEST)
basic.pause(100)j=m-1
basic.clear_screen()
basic.pause(100)elif a[m] < b:basic.show_arrow(ArrowNames.EAST)
basic.pause(100)i= m+1
basic.clear_screen()
basic.pause(100)else:basic.show_icon(IconNames.HEART)
break
input.on_button_pressed(Button.A, on_button_pr
essed_a)5.2 整列【例題 5-5】(保存ファイル名：microbit-rei5-5)
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-2】
プログラムなし【例題 5-4】(保存ファイル名：microbit-rei5-4)
cmp = 0m=0c = ""
i=0
j=0
a: List[str] = []a = ["blue", "cyan", "green", "magenta", "red", "white", "yellow"]【練習 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()

def on_button_pressed_a():global i, j, c, m, cmp
i=0
j=6
c = a[randint(0, 6)]
basic.show_string(c)while i <= j:m = Math.idiv(i + j, 2)
cmp = a[m].compare(c)if cmp > 0:
basic.show_arrow(ArrowNames.WEST)
j=m-1
basic.pause(100)basic.clear_screen()
basic.pause(100)elif cmp < 0: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)}11basic.pause(500)basic.pause(500)basic.show_string(String.from_char_code(64+ a))basic.pause(500)basic.show_arrow(ArrowNames.SOUTH)
basic.pause(500)basic.show_string(String.from_char_code(64+ b))basic.pause(500)basic.clear_screen()
if n > 1:
hanoi(n - 1, 6 - a - b, b)}}【練習 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)
c=0
c = fact(5)basic.show_number(c)def fact(n: number):
if n != 0:
return n * fact(n - 1)
return 1

5.3 ハノイの塔【例題 5-6】
関 数 （太 字 黄 色 塗 りの箇 所 ）の仕 様 により，エラーがでて自動変換できない。

【練習 5-6】
プログラムなし(保存ファイル名：microbit-rei5-6)
n=0

5.4 自動販売機の状態遷移図【例題 5-7】(保存ファイル名：microbit-rei5-7)
s=0
s1 = 1

def on_button_pressed_a():global nn=1
basic.show_number(n)basic.show_string(" Mai")
hanoi(n, 1, 3)
basic.show_string(" End")
input.on_button_pressed(Button.A, on_button_pr
essed_a)def on_button_pressed_a():global ss0 = 0if s == s0:
s = s1elif s == s1:
syohin()s = s0basic.clear_screen()else:
basic.show_icon(IconNames.NO)
basic.show_number(s)input.on_button_pressed(Button.A, on_button_pr
essed_a)def on_button_pressed_b():
global nn=n+1
basic.show_number(n)basic.show_string(" Mai")
hanoi(n, 1, 3)
basic.show_string(" End")
input.on_button_pressed(Button.B, on_button_pr
essed_b)

def syohin():basic.show_icon(IconNames.TARGET)
basic.pause(1000)

def hanoi(n: number, a: number, b: number):
if n > 1:
hanoi(n - 1, a, 6 - a - b)basic.show_icon(IconNames.HEART)
basic.pause(1000)
basic.show_number(n)【例題 5-8】(保存ファイル名：microbit-rei5-8)s=012

s0 = 0s1 = 1s2 = 2(1)(保存ファイル名：microbit-ens5-1)
a: List[number] = []a = [3, 2, 1, 5, 4]
i=4while i > 0:
k=0
j=1
while j <= i:if a[j] > a[k]:k=jj += 1
tmp = 0tmp = a[k]a[k] = a[i]a[i] = tmp
i -= 1for l in range(5):basic.show_number(a[l])def otsuri():basic.show_icon(IconNames.SMALL_DIAMON
D)
basic.pause(1000)
def on_button_pressed_a():global s
if s == s0:
s = s2elif s == s1:
syohin()s = s0basic.pause(1000)
basic.clear_screen()
elif s == s2:
syohin()otsuri()s = s0basic.pause(1000)
basic.clear_screen()else:
basic.show_icon(IconNames.NO)
basic.show_number(s)input.on_button_pressed(Button.A, on_button_pr
essed_a)(2)(a)(保存ファイル名：microbit-ens5-2-1)
s=0
s1 = 1s2 = 2def on_button_pressed_a():global ss0 = 0if s == s0:
s = s1elif s == s1:
s = s2elif s == s2:
syohin()s = s0basic.clear_screen()else:
basic.show_icon(IconNames.NO)
basic.show_number(s)input.on_button_pressed(Button.A, on_button_pr
essed_a)def on_button_pressed_ab():basic.show_icon(IconNames.NO)
input.on_button_pressed(Button.AB, on_button_pressed_ab)
def on_button_pressed_b():
global s
if s == s0:
s = s1elif s == s1:
s = s2elif s == s2:
syohin()s = s0basic.pause(1000)
basic.clear_screen()else:
basic.show_icon(IconNames.NO)
basic.show_number(s)input.on_button_pressed(Button.B, on_button_pr
essed_b)

def syohin():basic.show_icon(IconNames.TARGET)
basic.pause(1000)

def syohin():basic.show_icon(IconNames.TARGET)
basic.pause(1000)(2)(b)(保存ファイル名：microbit-ens5-2-2)s=0
s0 = 0s1 = 1s2 = 2s3 = 3s4 = 4

演習問題

def otsuri():13

basic.show_icon(IconNames.SMALL_DIAMON
D)

def on_received_string_deprecated(receivedString):
basic.show_string("" + (receivedString))
radio.on_received_string_deprecated(on_received_string_deprecated)basic.pause(1000)
basic.clear_screen()
def on_button_pressed_a():global s
if s >= s0 and s <= s2:
s += 2elif s == s3:
s = s0syohin()elif s == s4:
s = s0syohin()otsuri()else:
basic.show_icon(IconNames.NO)
basic.show_number(s)input.on_button_pressed(Button.A, on_button_pr
essed_a)【練習 6-1】(保存ファイル名：microbit-ren6-1)
radio.set_group(1)a=0
def on_button_pressed_b():
global a
if a == 0:
radio.send_string("hello")
else:radio.send_string("world")
a += 1
input.on_button_pressed(Button.B, on_button_pr
essed_b)

def on_button_pressed_ab():basic.show_icon(IconNames.NO)
input.on_button_pressed(Button.AB, on_button_pressed_ab)

def on_received_string_deprecated(receivedString):
basic.show_string("" + (receivedString))
radio.on_received_string_deprecated(on_received_string_deprecated)

def on_button_pressed_b():
global s
if s >= s0 and s <= s3:
s += 1elif s == s4:
s = s0syohin()else:
basic.show_icon(IconNames.NO)
basic.show_number(s)input.on_button_pressed(Button.B, on_button_pr
essed_b)

6.2 ネットワークにおけるアドレッシング【例題 6-2】関数（太字黄 色塗りの箇 所）の仕様によりエラーがでて自動変換できない。
(保存ファイル名：microbit-rei6-2)
message = ""yad = 0
myAddress = ""yourAddress = ""radio.set_group(1)yourAddress = "2"
myAddress = "1"

def syohin():basic.show_icon(IconNames.TARGET)
basic.pause(1000)
basic.clear_screen()

def on_button_pressed_a():global yad, yourAddress
yad = (yad + 1) % 9
yourAddress = String.from_char_code(yad +48)
basic.show_string(yourAddress)
input.on_button_pressed(Button.A, on_button_pr
essed_a)

６．通信とプログラム
6.1 通信の基本【例題 6-1】(保存ファイル名：microbit-rei6-1)
radio.set_group(1)def on_button_pressed_b():
radio.send_string("hello")
input.on_button_pressed(Button.B, on_button_pr
essed_b)

def on_button_pressed_b():
global message
message = "" + yourAddress + myAddress +"hello"radio.send_string(message)
14

input.on_button_pressed(Button.B, on_button_pr
essed_b)

msg3 = ""msg2 = ""shift = 0
msg = ""
shift = 5

def on_received_string_deprecated(receivedString):
if myAddress == receivedString[0]:basic.show_string(receivedString.substr(2, len(receivedString) - 2))radio.on_received_string_deprecated(on_received_string_deprecated)

def on_button_pressed_a():global msg, msg2
msg = "dream"
msg2 = ""i=0while i <= len(msg) - 1:
msg2 = "" + msg2 + String.from_char_code(msg[i].char_code_at(0) + shift)i += 1
radio.send_string("" + msg2)
input.on_button_pressed(Button.A, on_button_pr
essed_a)【練習 6-2】関数（太字黄 色塗りの箇 所）の仕様によりエラーがでて自動変換できない。
(保存ファイル名：microbit-ren6-2)
message = ""yad = 0
yourAddress = ""myAddress = ""radio.set_group(1)myAddress = "2"
yourAddress = "1"

def on_received_string(receivedString):
global msg3
j=0
while j <= len(receivedString) - 1:
msg3 = "" + msg3 + String.from_char_code(receivedString[j].char_code_at(0) - shift)j += 1
basic.show_string("" + msg3)
radio.on_received_string(on_received_string)def on_button_pressed_a():global yad, yourAddress
yad = (yad + 1) % 10
yourAddress = String.from_char_code(yad +48)
basic.show_string("" + (yourAddress))
input.on_button_pressed(Button.A, on_button_pr
essed_a)【練習 6-3】
プログラムなし

def on_button_pressed_b():
global message
message = "" + yourAddress + myAddress +"hello"radio.send_string("" + (message))
input.on_button_pressed(Button.B, on_button_pr
essed_b)【例題 6-4】(保存ファイル名：microbit-rei6-4)
x=0
A=0
D=0
E=0
N=0
y=0
y=0
N = 55E=3
D=7
A = 17

def on_received_string_deprecated(receivedString):
if myAddress == receivedString[0] or receiv
edString[0] == "9":basic.show_string(receivedString.substr(2, len(receivedString) - 2))radio.on_received_string_deprecated(on_received_string_deprecated)

def on_received_number_deprecated(receivedNu
mber):
global yy = receivedNumber ** D % N
basic.show_number(y)radio.on_received_number_deprecated(on_receiv
ed_number_deprecated)

6.3 暗号通信【例題 6-3】関数（太字黄 色塗りの箇 所）の仕様によりエラーがでて自動変換できない。

def on_button_pressed_a():global x
x = A ** E % N(保存ファイル名：microbit-rei6-3)
15

basic.show_number(A)radio.send_number(x)input.on_button_pressed(Button.A, on_button_pr
essed_a)radio.send_number(1)basic.pause(4000)
pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P1, 1)
radio.send_number(2)basic.pause(1000)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 1)
radio.send_number(3)basic.pause(4000)
radio.send_number(4)basic.pause(1000)
radio.send_number(5)basic.pause(3000)
radio.send_number(6)basic.pause(1000)
radio.send_number(7)basic.pause(1000)
basic.forever(on_forever)

6.4 エラー検出【例題 6-5】
自働変換できない。検証中。
(保存ファイル名：microbit-rei6-5)

演習問題
(1)
(保存ファイル名：microbit-ens6-1)
wait_for_ack = 0
radio.set_group(1)def on_button_pressed_b():
global wait_for_ack
wait_for_ack = 1
radio.send_string("hello")
input.on_button_pressed(Button.B, on_button_pr
essed_b)(保存ファイル名：microbit-hat7-1-2)
radio.set_group(1)def on_received_number_deprecated(receivedNu
mber):
pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 0)
if receivedNumber == 1:
pins.digital_write_pin(DigitalPin.P0, 1)
elif receivedNumber == 3:
pins.digital_write_pin(DigitalPin.P2, 1)
elif receivedNumber == 4:
pins.digital_write_pin(DigitalPin.P1, 1)
elif receivedNumber == 5:
pins.digital_write_pin(DigitalPin.P0, 1)
else:
pins.digital_write_pin(DigitalPin.P0, 1)
radio.on_received_number_deprecated(on_receiv
ed_number_deprecated)

def on_received_string_deprecated(receivedString):
global wait_for_ack
if wait_for_ack == 0:
basic.show_string("" + (receivedString))
radio.send_string("world")
else:
wait_for_ack = 0
basic.show_string("" + (receivedString))
radio.on_received_string_deprecated(on_received_string_deprecated)(2)プログラムなし(保存ファイル名：microbit-hat7-1-3)
radio.set_group(1)def on_received_number_deprecated(receivedNu
mber):
pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 0)
if receivedNumber == 1:
pins.digital_write_pin(DigitalPin.P0, 1)
elif receivedNumber == 5:
pins.digital_write_pin(DigitalPin.P2, 1)
elif receivedNumber == 6:
for index in range(5):pins.digital_write_pin(DigitalPin.P2, 1)
basic.pause(100)pins.digital_write_pin(DigitalPin.P2, 0)
basic.pause(100)elif receivedNumber == 7:(3)
自働変換できない。検証中。
(保存ファイル名：microbit-ens6-3)

７．総合問題7.1 信号機（スクランブル交差点）(保存ファイル名：microbit-hat7-1-1)
radio.set_group(1)def on_forever():pins.digital_write_pin(DigitalPin.P2, 1)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P0, 0)
16

pins.digital_write_pin(DigitalPin.P0, 1)
else:
pins.digital_write_pin(DigitalPin.P0, 1)
radio.on_received_number_deprecated(on_receiv
ed_number_deprecated)

elif c == 1:
basic.show_icon(IconNames.SCISSORS)
else:
basic.show_icon(IconNames.SQUARE)

7.2 じゃんけんゲーム（3 人対戦）

7.3 ハノイの塔（複数台による表示）(保存ファイル名：microbit-hat7-2)
radio.set_group(1)
a=0
b=0
c=0
count = 0
d: List[number] = []d = [0, 0]関数（太字黄色塗りの箇所）等の仕様によりエラーがでて自動変換できない。
(保存ファイル名：microbit-hat7-3-1)
radio.set_group(1)def on_button_pressed_a():global nn=3
basic.show_number(n)basic.show_string(" Mai")
hanoi(n, 1, 3)
basic.show_string(" End")
input.on_button_pressed(Button.A, on_button_pr
essed_a)def on_received_number(receivedNumber):global count, b, cd[count] = receivedNumber
count += 1
if count >= 2:b = d[0]c = d[1]if a == b and a == c and b == c or a != b
and a != c and b != c:basic.show_icon(IconNames.CONFUSE
D)
elif (a + 1) % 3 == b or (a + 1) % 3 == c:basic.show_icon(IconNames.HAPPY)
else:
basic.show_icon(IconNames.SAD)
basic.pause(500)count = 0radio.on_received_number(on_received_number)def on_button_pressed_b():
global nn=n+1
basic.show_number(n)basic.show_string(" Mai")
hanoi(n, 1, 3)
basic.show_string(" End")
input.on_button_pressed(Button.B, on_button_pr
essed_b)n=0
def send_pop(bar: number):
msg = (bar * 100) + 20
radio.send_number(msg)
def send_push(bar: number, n: number):
msg2 = (bar * 100) + 10 + n
radio.send_number(msg2)
def hanoi(n: number, a: number, b: number):
if n > 1:
hanoi(n - 1, a, 6 - a - b)basic.show_icon(IconNames.HEART)
basic.pause(1000)
basic.show_number(n)basic.pause(500)basic.show_string(String.from_char_code(64+ a))basic.pause(500)basic.show_arrow(ArrowNames.SOUTH)
basic.pause(500)basic.show_string(String.from_char_code(64+ b))basic.pause(500)basic.clear_screen()
send_pop(a)send_push(b, n)

def on_button_pressed_a():global a, c
a = Math.random_range(0, 2)
c=ahyouji2()
input.on_button_pressed(Button.A, on_button_pr
essed_a)def on_button_pressed_ab():control.reset()
input.on_button_pressed(Button.AB, on_button_pressed_ab)
def on_button_pressed_b():
radio.send_number(a)input.on_button_pressed(Button.B, on_button_pr
essed_b)
def hyouji2():
if c == 0:
basic.show_icon(IconNames.SMALL_DIAM
OND)17

if n > 1:
hanoi(n - 1, 6 - a - b, b)

while j <= 5 - 1:
if bar[j] != 0:o = bar[j]if o == 1:
bo2 = board1
elif o == 2:
bo2 = board3
elif o == 3:
bo2 = board5
bar[j] = 0
k=0while k <= len(bo2) - 1:
bo2[k].delete()k += 1break
j += 1
def push(n: number):global board1, board3, board5
bo: List[game.LedSprite] = []if n == 1:
board1 = [game.create_sprite(2, 0)]bo = board1
elif n == 2:board3 = [game.create_sprite(1, 0),
game.create_sprite(2, 0),
game.create_sprite(3, 0)]bo = board3
elif n == 3:
board5 = [game.create_sprite(0, 0),
game.create_sprite(1, 0),
game.create_sprite(2, 0),
game.create_sprite(3, 0),
game.create_sprite(4, 0)]bo = board5
for i in range(5):if i != 0 and bar[i] == 0:
down(bo)if i == 4:
bar[i] = nelif bar[i + 1] != 0:
bar[i] = n
break
def up(b: List[game.LedSprite]):for l in range(len(b)):
b[l].change(LedSpriteProperty.Y, -1)
def down(b: List[game.LedSprite]):basic.pause(500)for m in range(len(b)):
b[m].change(LedSpriteProperty.Y, 1)(保存ファイル名：microbit-hat7-3-2)
bo2: List[game.LedSprite] = []n=0mynum = 0
bar: List[number] = []cmd = 0o=0data = 0
board1: List[game.LedSprite] = []board3: List[game.LedSprite] = []board5: List[game.LedSprite] = []bar = [0, 0, 0, 0, 0]
mynum = 4
radio.set_group(1)def on_button_pressed_a():global mynum
mynum = (mynum + 1) % 4
basic.show_number(mynum)
input.on_button_pressed(Button.A, on_button_pr
essed_a)def on_button_pressed_b():
radio.send_number(100 + 20)
input.on_button_pressed(Button.B, on_button_pr
essed_b)
def on_button_pressed_ab():basic.clear_screen()
if mynum == 1:
push(3)push(2)push(1)
input.on_button_pressed(Button.AB, on_button_pressed_ab)
def on_forever():
led.plot(mynum - 1, 0)
basic.forever(on_forever)def on_received_number(receivedNumber):global data, cmd, ndata = receivedNumber
if Math.floor(data / 100) == mynum:
cmd = Math.floor(data % 100 / 10)
n = Math.floor(data % 10)
if cmd == 1:
push(n)elif cmd == 2:
pop2()
radio.on_received_number(on_received_number)

7.4 通信プログラム（不具合問題）(保存ファイル名：microbit-hat7-4)
sstring = ""radio.set_group(1)def on_button_pressed_b():
global sstring
sstring = "s" + "hello"

def pop2():
global o, bo2
j=018

radio.send_string("" + (sstring))
input.on_button_pressed(Button.B, on_button_pr
essed_b)
def on_received_string_deprecated(receivedString):
global sstring
if receivedString[0] == "s":basic.show_string(receivedString.substr(1, len(receivedString) - 1))
sstring = "a" + "world"
radio.send_string("" + (sstring))
else:
basic.show_string(receivedString.substr(1, len(receivedString) - 1))radio.on_received_string_deprecated(on_received_string_deprecated)

19