Anonymous user
Towers of Hanoi: Difference between revisions
m
→{{header|REXX}}: added DO-END labels, compressed sourse somewhat, removed superflous blanks. -- ~~~~
m (→{{header|Clojure}}: whitespace) |
m (→{{header|REXX}}: added DO-END labels, compressed sourse somewhat, removed superflous blanks. -- ~~~~) |
||
Line 1,619:
=={{header|REXX}}==
===
<lang rexx>/*REXX program to show the moves to solve the Tower of Hanoi (3 disks). */
arg z . /*get possible specification of Z*/
if z=='' then z=3 /*Not given? Use default 3 towers*/
move=0 /*number of ring moves so far. */
moves=2**z-1 /*calculate total number of moves*/
call mov 1,3,z /*move top ring, then recurse... */
say
say 'The minimum number of moves to solve a' z "ring Tower of Hanoi is" moves'.'
exit /*stick a fork in it, we're done.*/
/*─────────────────────────────MOV subroutine───────────────────────────*/
mov: if arg(3)==1 then call dsk arg(1),arg(2)
Line 1,639:
/*─────────────────────────────DSK subroutine───────────────────────────*/
dsk: move=move+1
say 'step' right(move,length(moves))": move disk " arg(1) '
return</lang>
{{out}}
<pre>
step 1: move disk 1
step 2: move disk 1
step 3: move disk 3
step 4: move disk 1
step 5: move disk 2
step 6: move disk 2
step 7: move disk 1
The minimum number of moves to solve a 3 ring Tower of Hanoi is 7.
</pre>
<pre style="height:
step 1: move disk 1
step 2: move disk 1
step 3: move disk 2
step 4: move disk 1
step 5: move disk 3
step 6: move disk 3
step 7: move disk 1
step 8: move disk 1
step 9: move disk 2
step 10: move disk 2
step 11: move disk 3
step 12: move disk 2
step 13: move disk 1
step 14: move disk 1
step 15: move disk 2
The minimum number of moves to solve a 4 ring Tower of Hanoi is 15.
</pre>
===pictorial moves===
This version pictorially shows the moves for solving the Town of Hanoi.
Quite a bit of code has been dedicated to showing a picture of the towers with the disks on them, and the movement of the disk (the move). Coloring of the rings is attempted with dithering.
In addition, it shows each move in a countdown manner (the last move is move #1).
No attempt is mode to explain this version of the REXX
<lang rexx>/*REXX program shows pictorial moves to solve Tower of Hanoi (3 disks). */
arg z .; if z=='' then z=3
sw=80; wp=sw%3-1; blanks=centre('',wp)▼
c.1=sw%3%2
c.2=sw%2-1
c.3=sw-1-c.1-1
move=0; totmoves=2**z-1; movek=totmoves; lentot=length(totmoves)
@abc='abcdefghijklmnopqrstuvwxyz'
ebcdic= 'f0'x==0
if ebcdic then do
bar='bf'x;ar='df'x;boxen='db9f9caf'x;tl='ac'x;tr='bf'x;bl='ab'x;br='bb'x;vert='fa'x;down='9a'x
Line 1,702 ⟶ 1,696:
bar='c4'x;ar='10'x;boxen='b0b1b2db'x;tl='da'x;tr='bf'x;bl='c0'x;br='d9'x;vert='b3'x;down='19'x
end
verts=vert || vert▼
downs=down || down▼
▲verts=vert||vert
Tcorners=tl || tr▼
▲downs=down||down
Bcorners=bl || br▼
▲Tcorners=tl||tr
box=left(boxen,1); boxchars=boxen || @abc▼
▲Bcorners=bl||br
bararrow=bar || bar || ar▼
$.=0; $.1=z; k=z; kk=k+k
▲boxchars=boxen||@abc
▲bararrow=bar||bar||ar
▲blanks=centre('',wp)
do j=1 for z
@.3.j=blanks; @.2.j=blanks
@.1.j=centre(copies(box,kk),wp)
if z<=length(boxchars) then @.1.j=,
translate(@.1.j,substr(boxchars,kk%2,1),box)
kk=kk-2
end /*j*/
call showtowers; call mov 1,3,z
say
say "The minimum number of moves for a" z 'ring Tower of Hanoi is' totmoves
Line 1,740 ⟶ 1,725:
return
/*─────────────────────────────RNG subroutine───────────────────────────*/
rng: parse arg from dest; move=move+1; pp=
if from==1 then do
pp=overlay(bl,pp,c.1)
pp=overlay(bar,pp,c.1+1,c.dest-c.1-1,bar)
pp=pp || tr
end
if from==3 then do
pp=overlay(br,pp,c.3)
Line 1,758 ⟶ 1,739:
lpost=min(2,dest)
hpost=max(2,dest)
if dest==1 then do
pp=overlay(tl,pp,c.1)
pp=overlay(bar,pp,c.1+1,c.2-c.1-1,bar)
pp=pp || br
end
if dest==3 then do
pp=overlay(bl,pp,c.2)
pp=overlay(bar,pp,c.2+1,c.3-c.2-1,bar)
pp=pp || tr
end
end
say translate(pp,verts,Bcorners||Tcorners||bar); say overlay(movek,pp,1)▼
▲say translate(pp,verts,Bcorners||Tcorners||bar)
say translate(pp,verts,Tcorners||Bcorners||bar)
say translate(pp,downs,Tcorners||Bcorners||bar)
movek=movek-1
$.from=$.from-1; $.dest=$.dest+1; _f=$.from+1; _t=$.dest
@.dest._t=@.from._f; @.from._f=blanks▼
▲@.dest._t=@.from._f
call showtowers
return
/*─────────────────────────────SHOWTOWERS subroutine────────────────────*/
showtowers: do j=z to 1 by -1
_p=@.1.j @.2.j @.3.j; if _p\='' then say _p
return</lang>
{{out}}
|