Post Reply 
(42) Tower of Hanoi graphical version
07-07-2021, 10:54 PM
Post: #1
(42) Tower of Hanoi graphical version
I didn’t see a program that actually displays the Tower of Hanoi discs, so I wrote one for the 42s for 5 discs. To run this Tower of Hanoi program, XEQ TH
When the program pauses, type a 2 digit number (between 1 and 3) with the first digit (tens place) being the from pile and the second digit (ones place) being the to pile. Then, press R/S. Then repeat, until the puzzle is solved.

There are no checks to make sure the moves are valid or if the tower has been completely moved.

Registers 00 to 02 store pile information. The first pile, in register 00, is 97531. To move a disc from one pile to another, we:

Divide by 10 for the from pile.
Add the fraction part to the to pile.
Multiply the to pile by 10.
Store the integer part of the from pile to the from pile.

TH1 processes the move command. Register 4 is the from pile and register 5 is the to pile.
TH5 displays the 3 piles. Register 13 holds the pile number to display.
TH3 sets things up to display a pile.
TH4 is the loop to setup to display each disc. Register 10 is the X coordinate of the disc. Register 11 is the Y coordinate of the disc. Register 7 is ISG counter.
TH2 actually displays the disc. Using the AGRAPH command, I found that the program runs at a usable speed on a HP 42s (compared to using the PIXEL command).

Code:

00 { 246-Byte Prgm }
01▸LBL "TH"
02 CLLCD
03 CLA
04 7
05 XTOA
06 XTOA
07 XTOA
08 97531
09 STO 00
10 0
11 STO 01
12 STO 02
13 XEQ "TH5"
14 STOP
15▸LBL "TH1"
16 CLLCD
17 STO 06
18 10
19 MOD
20 1
21 -
22 STO 05
23 RCL 06
24 10
25 ÷
26 IP
27 1
28 -
29 STO 04
30 10
31 STO÷ IND 04
32 RCL IND 04
33 FP
34 STO+ IND 05
35 10
36 STO× IND 05
37 RCL IND 04
38 IP
39 STO IND 04
40 XEQ "TH5"
41 STOP
42 GTO "TH1"
43▸LBL "TH5"
44 0
45 STO 13
46 XEQ "TH3"
47 1
48 STO 13
49 XEQ "TH3"
50 2
51 STO 13
52 XEQ "TH3"
53 RTN
54▸LBL "TH2"
55 RCL 11
56 RCL 10
57 AGRAPH
58 3
59 STO+ 10
60 ISG 07
61 GTO "TH2"
62 RTN
63▸LBL "TH3"
64 RCL IND 13
65 STO 15
66 RCL IND 13
67 X=0?
68 RTN
69 LOG
70 1
71 +
72 IP
73 STO 14
74▸LBL "TH4"
75 16
76 RCL 14
77 3
78 ×
79 -
80 STO 11
81 RCL 15
82 10
83 MOD
84 STO 16
85 3
86 ×
87 2
88 ÷
89 IP
90 35
91 X<>Y
92 -
93 RCL 13
94 30
95 ×
96 +
97 STO 10
98 STO 17
99 RCL 16
100 1
101 -
102 1ᴇ3
103 ÷
104 STO 07
105 STO 18
106 XEQ "TH2"
107 10
108 STO÷ 15
109 RCL 15
110 IP
111 STO 15
112 DSE 14
113 GTO "TH4"
114 RTN
115 END


Attached File(s)
.zip  ToH.raw.zip (Size: 309 bytes / Downloads: 5)
Find all posts by this user
Quote this message in a reply
07-09-2021, 12:27 PM
Post: #2
RE: (42) Tower of Hanoi graphical version
For the 'eclectic' minded: The Tower of Hanoi – Myths and Maths 2e, Hinz-Klavžar-Petr, Birkhäuser, ISBN 978-3-319-73779-9 (e-book), 458 pgs.

[attachment=9630]

BEST!
SlideRule
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)