Towers of Hanoi: Difference between revisions
Content added Content deleted
m (→simple text moves: added/changed comments and whitespace.) |
m (→pictorial moves: added/changed comments and whitespace, changed indentations.) |
||
Line 2,868: | Line 2,868: | ||
Also, since the pictorial showing of the moves may be voluminous (especially for a larger number of disks), the move counter is started with the maximum and is the count shown is decremented so the viewer can see how many moves are left to display. |
Also, since the pictorial showing of the moves may be voluminous (especially for a larger number of disks), the move counter is started with the maximum and is the count shown is decremented so the viewer can see how many moves are left to display. |
||
<lang rexx>/*REXX program |
<lang rexx>/*REXX program displays the moves to solve the Tower of Hanoi (with N disks). */ |
||
parse arg N . |
parse arg N . /*get optional number of disks from CL.*/ |
||
if N=='' | N=="," then N=3 /*Not specified? Then use the default.*/ |
|||
sw=80; wp=sw%3-1; blanks=left('',wp) /*define some default REXX variables. */ |
|||
sw=80; wp=sw%3-1; blanks=left('', wp) /*define some default REXX variables. */ |
|||
c.1= sw % 3 % 2 /* [↑] SW: assume default Screen Width*/ |
|||
c.2=sw%2-1 |
|||
c. |
c.2= sw % 2 - 1 |
||
c.3= sw - 1 - c.1 - 1 |
|||
#=0; z=2**N-1; moveK=z /*#moves; min# of moves; where to move.*/ |
#=0; z=2**N-1; moveK=z /*#moves; min# of moves; where to move.*/ |
||
@abc='abcdefghijklmnopqrstuvwxyN' /*dithering chars when many disks used.*/ |
|||
@abc='abcdefghijklmnopqrstuvwxyN' /*dithering chars when many disks used.*/ |
|||
ebcdic= ('f0'x==0) /*determine if EBCDIC or ASCII machine.*/ |
|||
if ebcdic then do; bar='bf'x; ar="df"x; boxen='db9f9caf'x; down="9a"x |
if ebcdic then do; bar= 'bf'x; ar= "df"x; boxen= 'db9f9caf'x; down= "9a"x |
||
tr='bc'x; bl="ab"x; br='bb'x; vert="fa"x; tl='ac'x |
tr= 'bc'x; bl= "ab"x; br= 'bb'x; vert= "fa"x; tl= 'ac'x |
||
end |
end |
||
else do; bar='c4'x; ar="10"x; boxen='b0b1b2db'x; down="18"x |
else do; bar= 'c4'x; ar= "10"x; boxen= 'b0b1b2db'x; down= "18"x |
||
tr='bf'x; bl="c0"x; br='d9'x; vert="b3"x; tl='da'x |
tr= 'bf'x; bl= "c0"x; br= 'd9'x; vert= "b3"x; tl= 'da'x |
||
end |
end |
||
verts= vert || vert; Tcorners= tl || tr |
verts= vert || vert; Tcorners= tl || tr |
||
downs= down || down; Bcorners= bl || br |
downs= down || down; Bcorners= bl || br |
||
box = left(boxen,1); boxChars= boxen || @abc |
box = left(boxen, 1); boxChars= boxen || @abc |
||
$.=0; |
$.=0; $.1=N; k=N; kk=k+k |
||
do j=1 for N; @.3.j=blanks; @.2.j=blanks; @.1.j=center(copies(box,kk),wp) |
do j=1 for N; @.3.j=blanks; @.2.j=blanks; @.1.j=center( copies( box, kk), wp) |
||
if N<=length(boxChars) then @.1.j=translate(@.1.j,,substr(boxChars,kk%2,1),box) |
if N<=length(boxChars) then @.1.j= translate(@.1.j, , substr( boxChars, kk%2, 1), box) |
||
kk=kk-2 |
kk=kk - 2 |
||
end /*j*/ /*populate the tower of Hanoi spindles.*/ |
end /*j*/ /*populate the tower of Hanoi spindles.*/ |
||
call |
call showTowers; call mov 1,3,N; say |
||
say 'The minimum number of moves to solve a ' N"-disk Tower of Hanoi is " z |
say 'The minimum number of moves to solve a ' N"-disk Tower of Hanoi is " z |
||
exit /*stick a fork in it, we're all done. */ |
|||
exit |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*─────────────────────────────MOV subroutine─────────────────────────────────*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
mov: if arg(3)==1 then call dsk arg(1) arg(2) |
mov: if arg(3)==1 then call dsk arg(1) arg(2) |
||
else do; call mov arg(1), 6-arg(1)-arg(2), arg(3)-1 |
else do; call mov arg(1), 6-arg(1)-arg(2), arg(3)-1 |
||
Line 2,904: | Line 2,931: | ||
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 |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*─────────────────────────────DSK subroutine─────────────────────────────────*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
if dest==1 then do |
|||
⚫ | |||
⚫ | |||
⚫ | |||
if dest==3 then do |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
if from==3 then do |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
/*─────────────────────────────SHOWTOWERS subroutine──────────────────────────*/ |
|||
⚫ | |||
return</lang> |
|||
'''output''' when using the default input: |
'''output''' when using the default input: |
||
<pre> |
<pre> |
||
Line 2,986: | Line 2,983: | ||
The minimum number of moves to solve a 3-disk Tower of Hanoi is 7 |
The minimum number of moves to solve a 3-disk Tower of Hanoi is 7 |
||
</pre> |
</pre> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |