Towers of Hanoi: Difference between revisions

m
→‎{{header|REXX}}: added more whitespace to the program and it's output, added another down arrow for pictorial display of moves.
(add swift)
m (→‎{{header|REXX}}: added more whitespace to the program and it's output, added another down arrow for pictorial display of moves.)
Line 2,151:
=={{header|REXX}}==
===simple text moves===
<lang rexx>/*REXX programpgm to showshows the moves to solve the Tower of Hanoi (with 3 disks). */
parse arg zN . /*get possibleoptional specification# oftowers Zfrom C.L.*/
if zN=='' then zN=3 /*Not given? Use default 3 towers*/
move#=0; z=2**N - 1 /*number of ring moves so far. */
call mov 1, 3,z N /*move top ring, then recurse...··· */
say
say 'The minimum number of moves to solve a ' z N "ring Tower of Hanoi is " 2** z-1
exit /*stick a fork in it, we're done.*/
/*─────────────────────────────MOV subroutine───────────────────────────*/
mov: procedure expose move; arg #1, #2, #3
if #3==1 then call dsk #1, #2
else do
call mov #1, 6-#1-#2, #3-1
call mov #1, #2, 1
call mov 6-#1-#2, #2, #3-1
end
return
/*─────────────────────────────DSK subroutine───────────────────────────*/
dsk: #=#+1 /*bump the move counter by one. */
dsk: move=move+1
say 'step' right(move#,length(movesz))": move disk on tower" arg(1) '──►───►' arg(2)
return
/*─────────────────────────────MOV subroutine───────────────────────────*/
mov: procedure expose move# z; parse arg # @1, # @2, # @3
if #@3==1 then call dsk #@1, # @2
else do
call mov #@1, 6-#@1-#@2, #@3-1
call mov #@1, #@2, 1
call mov 6-#@1-#@2, #@2, #@3-1
end
return</lang>
{{out}}when the default input was used:
{{out}}
<pre>
step 1: move disk on tower 1 ──►───► 3
step 2: move disk on tower 1 ──►───► 2
step 3: move disk on tower 3 ──►───► 2
step 4: move disk on tower 1 ──►───► 3
step 5: move disk on tower 2 ──►───► 1
step 6: move disk on tower 2 ──►───► 3
step 7: move disk on tower 1 ──►───► 3
 
The minimum number of moves to solve a 3 ring Tower of Hanoi is 7.
 
The minimum number of moves to solve a 3 ring Tower of Hanoi is 7.
</pre>
{{out}}when the following was entered (to solve with four diskstowers): &nbsp; <tt> 4 </tt>
<pre>
<pre style="height:25ex">
step 1: move disk on tower 1 ──►───► 2
step 2: move disk on tower 1 ──►───► 3
step 3: move disk on tower 2 ──►───► 3
step 4: move disk on tower 1 ──►───► 2
step 5: move disk on tower 3 ──►───► 1
step 6: move disk on tower 3 ──►───► 2
step 7: move disk on tower 1 ──►───► 2
step 8: move disk on tower 1 ──►───► 3
step 9: move disk on tower 2 ──►───► 3
step 10: move disk on tower 2 ──►───► 1
step 11: move disk on tower 3 ──►───► 1
step 12: move disk on tower 2 ──►───► 3
step 13: move disk on tower 1 ──►───► 2
step 14: move disk on tower 1 ──►───► 3
step 15: move disk on tower 2 ──►───► 3
 
The minimum number of moves to solve a 4 ring Tower of Hanoi is 15.
</pre>
 
Line 2,208 ⟶ 2,209:
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). &nbsp; Coloring of the rings is attempted with dithering.
 
In addition, it shows each move in a countdown manner (the last move is movemarked as #1).
 
No attempt is modemade to explain this version of the REXX program 'cause of the complexity and somewhat obtuse features of the REXX language, the smallest of which is support for ASCII and EBCDIC "graphic" characters (glyphs). &nbsp; It may not be obvious from the pictorial display of the moves, but whenever a ring is moved from one tower to another, it is always the top ring that is moved &nbsp; (to the target tower).
<lang rexx>/*REXX programpgm shows pictorial moves to solve Tower of Hanoi (3with N disks). */
parse arg zN .; if zN=='' then zN=3 /*Not given? Use default 3 disks.*/
sw=80; wp=sw%3-1; blanks=centrecenter('',wp) /*define some default variables. */
c.1=sw%3%2
c.2=sw%2-1
c.3=sw-1-c.1-1
move#=0; totmovesz=2**zN-1; movek=totmoves; lentot=length(totmoves)z
@abc='abcdefghijklmnopqrstuvwxyN' /*dithering chars when many disks*/
@abc='abcdefghijklmnopqrstuvwxyz'
ebcdic= 'f0'x==0 /*determine if EBCDIC or ASCII*/
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 2,234 ⟶ 2,235:
box=left(boxen,1); boxchars=boxen || @abc
bararrow=bar || bar || ar
$.=0; $.1=zN; k=zN; kk=k+k
 
do j=1 for zN
@.3.j=blanks; @.2.j=blanks
@.1.j=centrecenter(copies(box,kk),wp)
if zN<=length(boxchars) then @.1.j=translate(@.1.j, ,
translate(@.1.j, substr(boxchars,kk%2,1),box)
kk=kk-2
end /*j*/
 
call showtowers; call mov 1,3,zN
say
say "The minimum number of moves forto solve a " z N 'ring Tower of Hanoi is ' totmoves z
exit
/*─────────────────────────────MOV subroutine───────────────────────────*/
mov: if arg(3)==1 then call rng arg(1) arg(2)
else do
call mov arg(1), 6-arg(1)-arg(2), arg(3)-1
call mov arg(1), arg(2), 1
call mov 6-arg(1)-arg(2),arg(2), arg(3)-1
end
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)
pp=overlay(bar,pp,c.dest+1,c.3-c.dest-1,bar)
pp=overlay(tl,pp,c.dest)
end
if from==2 then do
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,vertsdowns,Bcorners||Tcorners||bar); say overlay(movek,pp,1)
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
call showtowers
return
/*─────────────────────────────SHOWTOWERS subroutine────────────────────*/
showtowers: do j=zN to 1 by -1 for N
_p_=@.1.j @.2.j @.3.j; if _p_\='' then say _p_
end /*j*/
return</lang>
{{out}}when the default input was used:
{{out}}
<pre>
<pre style="height:50ex">
░░
▒▒▒▒
▓▓▓▓▓▓
7 └───────────────────────────────────────────────────┐
Line 2,306 ⟶ 2,307:
▒▒▒▒
▓▓▓▓▓▓ ░░
6 └─────────────────────────┐
▓▓▓▓▓▓ ▒▒▒▒ ░░
5 ┌─────────────────────────┘
Line 2,317 ⟶ 2,318:
░░
▓▓▓▓▓▓ ▒▒▒▒
4 └───────────────────────────────────────────────────┐
Line 2,323 ⟶ 2,324:
░░
▒▒▒▒ ▓▓▓▓▓▓
3 ┌─────────────────────────┘
░░ ▒▒▒▒ ▓▓▓▓▓▓
2 └─────────────────────────┐
Line 2,334 ⟶ 2,335:
▒▒▒▒
░░ ▓▓▓▓▓▓
1 └───────────────────────────────────────────────────┐
Line 2,342 ⟶ 2,343:
▓▓▓▓▓▓
 
The minimum number of moves forto solve a 3 ring Tower of Hanoi is 7
</pre>