ハノイの塔のプログラムを改良し、
動く様子を画像でも表示するようにしました。
ボタンを動かすようにしました。
コンソールに表示していた手順は、textviewに表示するようにしています。
動画もアップしようとしましたが、アップロードに時間がかかり過ぎるみたいだったので、今回は省略しました。
《SourceCode》
import ui
import time
step = 1
lnum = 7
cnum = rnum = 0
def moveDisk(num_disk, from_twr, to_twr):
global lnum, cnum, rnum
if num_disk == 1:
if to_twr == "Left":
d1.x = 80 - (num_disk-1) * 10
d1.y = 280 - lnum * 30
lnum += 1 #increment Left disk num.
if to_twr == "Center":
d1.x = 320 - (num_disk-1) * 10
d1.y = 280 - cnum * 30
cnum += 1 #increment Center disk num.
if to_twr == "Right":
d1.x = 560 - (num_disk-1) * 10
d1.y = 280 - rnum * 30
rnum += 1 #increment Right disk num.
if num_disk == 2:
if to_twr == "Left":
d2.x = 80 - (num_disk-1) * 10
d2.y = 280 - lnum * 30
lnum += 1 #increment Left disk num.
if to_twr == "Center":
d2.x = 320 - (num_disk-1) * 10
d2.y = 280 - cnum * 30
cnum += 1 #increment Center disk num.
if to_twr == "Right":
d2.x = 560 - (num_disk-1) * 10
d2.y = 280 - rnum * 30
rnum += 1 #increment Right disk num.
if num_disk == 3:
if to_twr == "Left":
d3.x = 80 - (num_disk-1) * 10
d3.y = 280 - lnum * 30
lnum += 1 #increment Left disk num.
if to_twr == "Center":
d3.x = 320 - (num_disk-1) * 10
d3.y = 280 - cnum * 30
cnum += 1 #increment Center disk num.
if to_twr == "Right":
d3.x = 560 - (num_disk-1) * 10
d3.y = 280 - rnum * 30
rnum += 1 #increment Right disk num.
if num_disk == 4:
if to_twr == "Left":
d4.x = 80 - (num_disk-1) * 10
d4.y = 280 - lnum * 30
lnum += 1 #increment Left disk num.
if to_twr == "Center":
d4.x = 320 - (num_disk-1) * 10
d4.y = 280 - cnum * 30
cnum += 1 #increment Center disk num.
if to_twr == "Right":
d4.x = 560 - (num_disk-1) * 10
d4.y = 280 - rnum * 30
rnum += 1 #increment Right disk num.
if num_disk == 5:
if to_twr == "Left":
d5.x = 80 - (num_disk-1) * 10
d5.y = 280 - lnum * 30
lnum += 1 #increment Left disk num.
if to_twr == "Center":
d5.x = 320 - (num_disk-1) * 10
d5.y = 280 - cnum * 30
cnum += 1 #increment Center disk num.
if to_twr == "Right":
d5.x = 560 - (num_disk-1) * 10
d5.y = 280 - rnum * 30
rnum += 1 #increment Right disk num.
if num_disk == 6:
if to_twr == "Left":
d6.x = 80 - (num_disk-1) * 10
d6.y = 280 - lnum * 30
lnum += 1 #increment Left disk num.
if to_twr == "Center":
d6.x = 320 - (num_disk-1) * 10
d6.y = 280 - cnum * 30
cnum += 1 #increment Center disk num.
if to_twr == "Right":
d6.x = 560 - (num_disk-1) * 10
d6.y = 280 - rnum * 30
rnum += 1 #increment Right disk num.
if num_disk == 7:
if to_twr == "Left":
d7.x = 80 - (num_disk-1) * 10
d7.y = 280 - lnum * 30
lnum += 1 #increment Left disk num.
if to_twr == "Center":
d7.x = 320 - (num_disk-1) * 10
d7.y = 280 - cnum * 30
cnum += 1 #increment Center disk num.
if to_twr == "Right":
d7.x = 560 - (num_disk-1) * 10
d7.y = 280 - rnum * 30
rnum += 1 #increment Right disk num.
if from_twr == "Left":
lnum = lnum -1 #decrement Left disk num.
if from_twr == "Center":
cnum = cnum - 1 #decrement Center disk num.
if from_twr == "Right":
rnum = rnum - 1 #decrement Right disk num.
time.sleep(0.5)
def hanois(num_disk, from_twr, to_twr, tmp_twr):
global step
#Move disk from 'from_twr' to 'to_twr'.
if num_disk == 1: #if 1disk, move it finished.
#print(f"step{step}: No.{num_disk}-disk from {from_twr} to {to_twr}.")
tv.text += (f"step{step}: No.{num_disk}-disk from {from_twr} to {to_twr}."+"\n")
moveDisk(num_disk, from_twr, to_twr)
step = step + 1
return
#Move (N-1)th. disk from Left to Center. Right as temporary tower
hanois(num_disk-1, from_twr, tmp_twr, to_twr)
#Move remaining disk from Left to Right
#print(f"step{step}: No.{num_disk}-disk from {from_twr} to {to_twr}.")
tv.text += (f"step{step}: No.{num_disk}-disk from {from_twr} to {to_twr}."+"\n")
moveDisk(num_disk, from_twr, to_twr)
step = step + 1
#Move (N-1)th. disk from Center to Right. Left as temporary tower
hanois(num_disk-1, tmp_twr, to_twr, from_twr)
v = ui.load_view()
v.present('sheet')
tv = v['textview1']
d1 = v['button1']
d2 = v['button2']
d3 = v['button3']
d4 = v['button4']
d5 = v['button5']
d6 = v['button6']
d7 = v['button7']
hanois(7, "Left", "Right", "Center")
《UI》
参考まで、ボタンの初期座標は、
Disk1:(80,100), Disk2:(70,130), Disk3:(60, 160)。。。Disk7:(20, 280)
大きさは、Disk1が幅60、Disk2が80、と、20ずつ大きくしています。
高さは全て30です。