Towers of Hanoi: Difference between revisions

Content added Content deleted
(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: Line 2,151:
=={{header|REXX}}==
=={{header|REXX}}==
===simple text moves===
===simple text moves===
<lang rexx>/*REXX program to show the moves to solve the Tower of Hanoi (3 disks). */
<lang rexx>/*REXX pgm shows the moves to solve the Tower of Hanoi (with 3 disks). */
arg z . /*get possible specification of Z*/
parse arg N . /*get optional # towers from C.L.*/
if z=='' then z=3 /*Not given? Use default 3 towers*/
if N=='' then N=3 /*Not given? Use default 3 towers*/
move=0 /*number of ring moves so far. */
#=0; z=2**N - 1 /*number of ring moves so far. */
call mov 1,3,z /*move top ring, then recurse... */
call mov 1, 3, N /*move top ring, then recurse··· */
say
say
say 'The minimum number of moves to solve a' z "ring Tower of Hanoi is" 2**z-1
say 'The minimum number of moves to solve a ' N " Tower of Hanoi is " z
exit /*stick a fork in it, we're done.*/
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 subroutine───────────────────────────*/
dsk: #=#+1 /*bump the move counter by one. */
dsk: move=move+1
say 'step' right(move,length(moves))": move disk " arg(1) '──►' arg(2)
say 'step' right(#,length(z))": move disk on tower" arg(1) '───►' arg(2)
return
/*─────────────────────────────MOV subroutine───────────────────────────*/
mov: procedure expose # 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>
return</lang>
{{out}}when the default input was used:
{{out}}
<pre>
<pre>
step 1: move disk 1 ──► 3
step 1: move disk on tower 1 ───► 3
step 2: move disk 1 ──► 2
step 2: move disk on tower 1 ───► 2
step 3: move disk 3 ──► 2
step 3: move disk on tower 3 ───► 2
step 4: move disk 1 ──► 3
step 4: move disk on tower 1 ───► 3
step 5: move disk 2 ──► 1
step 5: move disk on tower 2 ───► 1
step 6: move disk 2 ──► 3
step 6: move disk on tower 2 ───► 3
step 7: move disk 1 ──► 3
step 7: move disk on tower 1 ───► 3

The minimum number of moves to solve a 3 Tower of Hanoi is 7


The minimum number of moves to solve a 3 ring Tower of Hanoi is 7.
</pre>
</pre>
{{out}}when the following was entered (to solve with four disks): <tt>4</tt>
{{out}}when the following was entered (to solve with four towers): &nbsp; <tt> 4 </tt>
<pre>
<pre style="height:25ex">
step 1: move disk 1 ──► 2
step 1: move disk on tower 1 ───► 2
step 2: move disk 1 ──► 3
step 2: move disk on tower 1 ───► 3
step 3: move disk 2 ──► 3
step 3: move disk on tower 2 ───► 3
step 4: move disk 1 ──► 2
step 4: move disk on tower 1 ───► 2
step 5: move disk 3 ──► 1
step 5: move disk on tower 3 ───► 1
step 6: move disk 3 ──► 2
step 6: move disk on tower 3 ───► 2
step 7: move disk 1 ──► 2
step 7: move disk on tower 1 ───► 2
step 8: move disk 1 ──► 3
step 8: move disk on tower 1 ───► 3
step 9: move disk 2 ──► 3
step 9: move disk on tower 2 ───► 3
step 10: move disk 2 ──► 1
step 10: move disk on tower 2 ───► 1
step 11: move disk 3 ──► 1
step 11: move disk on tower 3 ───► 1
step 12: move disk 2 ──► 3
step 12: move disk on tower 2 ───► 3
step 13: move disk 1 ──► 2
step 13: move disk on tower 1 ───► 2
step 14: move disk 1 ──► 3
step 14: move disk on tower 1 ───► 3
step 15: move disk 2 ──► 3
step 15: move disk on tower 2 ───► 3


The minimum number of moves to solve a 4 ring Tower of Hanoi is 15.
The minimum number of moves to solve a 4 Tower of Hanoi is 15
</pre>
</pre>


Line 2,208: Line 2,209:
This version pictorially shows the moves for solving the Town of Hanoi.
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.
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 move #1).
In addition, it shows each move in a countdown manner (the last move is marked as #1).


No attempt is mode 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. It may not be obvious, but whenever a ring is moved from one tower to another, it is always the top ring that is moved.
No attempt is made 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 program shows pictorial moves to solve Tower of Hanoi (3 disks). */
<lang rexx>/*REXX pgm shows pictorial moves to solve Tower of Hanoi (with N disks).*/
arg z .; if z=='' then z=3
parse arg N .; if N=='' then N=3 /*Not given? Use default 3 disks.*/
sw=80; wp=sw%3-1; blanks=centre('',wp)
sw=80; wp=sw%3-1; blanks=center('',wp) /*define some default variables. */
c.1=sw%3%2
c.1=sw%3%2
c.2=sw%2-1
c.2=sw%2-1
c.3=sw-1-c.1-1
c.3=sw-1-c.1-1
move=0; totmoves=2**z-1; movek=totmoves; lentot=length(totmoves)
#=0; z=2**N-1; movek=z
@abc='abcdefghijklmnopqrstuvwxyN' /*dithering chars when many disks*/
@abc='abcdefghijklmnopqrstuvwxyz'
ebcdic= 'f0'x==0
ebcdic= 'f0'x==0 /*determine if EBCDIC or ASCII*/
if ebcdic then do
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
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: Line 2,235:
box=left(boxen,1); boxchars=boxen || @abc
box=left(boxen,1); boxchars=boxen || @abc
bararrow=bar || bar || ar
bararrow=bar || bar || ar
$.=0; $.1=z; k=z; kk=k+k
$.=0; $.1=N; k=N; kk=k+k


do j=1 for z
do j=1 for N
@.3.j=blanks; @.2.j=blanks
@.3.j=blanks; @.2.j=blanks
@.1.j=centre(copies(box,kk),wp)
@.1.j=center(copies(box,kk),wp)
if z<=length(boxchars) then @.1.j=,
if N<=length(boxchars) then @.1.j=translate(@.1.j, ,
translate(@.1.j,substr(boxchars,kk%2,1),box)
substr(boxchars,kk%2,1),box)
kk=kk-2
kk=kk-2
end /*j*/
end /*j*/


call showtowers; call mov 1,3,z
call showtowers; call mov 1,3,N
say
say
say "The minimum number of moves for a" z 'ring Tower of Hanoi is' totmoves
say "The minimum number of moves to solve a " N ' Tower of Hanoi is ' z
exit
exit
/*─────────────────────────────MOV subroutine───────────────────────────*/
/*─────────────────────────────MOV subroutine───────────────────────────*/
mov: if arg(3)==1 then call rng arg(1) arg(2)
mov: if arg(3)==1 then call rng arg(1) arg(2)
else do
else do
call mov arg(1),6-arg(1)-arg(2),arg(3)-1
call mov arg(1), 6-arg(1)-arg(2), arg(3)-1
call mov arg(1),arg(2),1
call mov arg(1), arg(2), 1
call mov 6-arg(1)-arg(2),arg(2),arg(3)-1
call mov 6-arg(1)-arg(2),arg(2), arg(3)-1
end
end
return
return
/*─────────────────────────────RNG subroutine───────────────────────────*/
/*─────────────────────────────RNG subroutine───────────────────────────*/
rng: parse arg from dest; move=move+1; pp=
rng: parse arg from dest; #=#+1; pp=
if from==1 then do
if from==1 then do
pp=overlay(bl,pp,c.1)
pp=overlay(bl,pp,c.1)
pp=overlay(bar,pp,c.1+1,c.dest-c.1-1,bar)
pp=overlay(bar,pp,c.1+1,c.dest-c.1-1,bar)
pp=pp || tr
pp=pp || tr
end
end
if from==3 then do
if from==3 then do
pp=overlay(br,pp,c.3)
pp=overlay(br,pp,c.3)
pp=overlay(bar,pp,c.dest+1,c.3-c.dest-1,bar)
pp=overlay(bar,pp,c.dest+1,c.3-c.dest-1,bar)
pp=overlay(tl,pp,c.dest)
pp=overlay(tl,pp,c.dest)
end
end
if from==2 then do
if from==2 then do
lpost=min(2,dest)
lpost=min(2,dest)
hpost=max(2,dest)
hpost=max(2,dest)
if dest==1 then do
if dest==1 then do
pp=overlay(tl,pp,c.1)
pp=overlay(tl,pp,c.1)
pp=overlay(bar,pp,c.1+1,c.2-c.1-1,bar)
pp=overlay(bar,pp,c.1+1,c.2-c.1-1,bar)
pp=pp || br
pp=pp || br
end
end
if dest==3 then do
if dest==3 then do
pp=overlay(bl,pp,c.2)
pp=overlay(bl,pp,c.2)
pp=overlay(bar,pp,c.2+1,c.3-c.2-1,bar)
pp=overlay(bar,pp,c.2+1,c.3-c.2-1,bar)
pp=pp || tr
pp=pp || tr
end
end
end
end
say translate(pp,verts,Bcorners||Tcorners||bar); say overlay(movek,pp,1)
say translate(pp,downs,Bcorners||Tcorners||bar); say overlay(movek,pp,1)
say translate(pp,verts,Tcorners||Bcorners||bar)
say translate(pp,verts,Tcorners||Bcorners||bar)
say translate(pp,downs,Tcorners||Bcorners||bar)
say translate(pp,downs,Tcorners||Bcorners||bar)
movek=movek-1
movek=movek-1
$.from=$.from-1; $.dest=$.dest+1; _f=$.from+1; _t=$.dest
$.from=$.from-1; $.dest=$.dest+1; _f=$.from+1; _t=$.dest
@.dest._t=@.from._f; @.from._f=blanks
@.dest._t=@.from._f; @.from._f=blanks
call showtowers
call showtowers
return
return
/*─────────────────────────────SHOWTOWERS subroutine────────────────────*/
/*─────────────────────────────SHOWTOWERS subroutine────────────────────*/
showtowers: do j=z to 1 by -1
showtowers: do j=N by -1 for N
_p=@.1.j @.2.j @.3.j; if _p\='' then say _p
_=@.1.j @.2.j @.3.j; if _\='' then say _
end /*j*/
end /*j*/
return</lang>
return</lang>
{{out}}when the default input was used:
{{out}}
<pre>
<pre style="height:50ex">
░░
░░
▒▒▒▒
▒▒▒▒
▓▓▓▓▓▓
▓▓▓▓▓▓
7 └───────────────────────────────────────────────────┐
7 └───────────────────────────────────────────────────┐
Line 2,306: Line 2,307:
▒▒▒▒
▒▒▒▒
▓▓▓▓▓▓ ░░
▓▓▓▓▓▓ ░░
6 └─────────────────────────┐
6 └─────────────────────────┐
▓▓▓▓▓▓ ▒▒▒▒ ░░
▓▓▓▓▓▓ ▒▒▒▒ ░░
5 ┌─────────────────────────┘
5 ┌─────────────────────────┘
Line 2,317: Line 2,318:
░░
░░
▓▓▓▓▓▓ ▒▒▒▒
▓▓▓▓▓▓ ▒▒▒▒
4 └───────────────────────────────────────────────────┐
4 └───────────────────────────────────────────────────┐
Line 2,323: Line 2,324:
░░
░░
▒▒▒▒ ▓▓▓▓▓▓
▒▒▒▒ ▓▓▓▓▓▓
3 ┌─────────────────────────┘
3 ┌─────────────────────────┘
░░ ▒▒▒▒ ▓▓▓▓▓▓
░░ ▒▒▒▒ ▓▓▓▓▓▓
2 └─────────────────────────┐
2 └─────────────────────────┐
Line 2,334: Line 2,335:
▒▒▒▒
▒▒▒▒
░░ ▓▓▓▓▓▓
░░ ▓▓▓▓▓▓
1 └───────────────────────────────────────────────────┐
1 └───────────────────────────────────────────────────┐
Line 2,342: Line 2,343:
▓▓▓▓▓▓
▓▓▓▓▓▓


The minimum number of moves for a 3 ring Tower of Hanoi is 7
The minimum number of moves to solve a 3 Tower of Hanoi is 7
</pre>
</pre>