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 |
<lang rexx>/*REXX pgm shows the moves to solve the Tower of Hanoi (with 3 disks). */ |
||
arg |
parse arg N . /*get optional # towers from C.L.*/ |
||
if |
if N=='' then N=3 /*Not given? Use default 3 towers*/ |
||
#=0; z=2**N - 1 /*number of ring moves so far. */ |
|||
call mov 1,3, |
call mov 1, 3, N /*move top ring, then recurse··· */ |
||
say |
say |
||
say 'The minimum number of moves to solve a' |
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.*/ |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
/*─────────────────────────────DSK subroutine───────────────────────────*/ |
/*─────────────────────────────DSK subroutine───────────────────────────*/ |
||
dsk: #=#+1 /*bump the move counter by one. */ |
|||
dsk: move=move+1 |
|||
say 'step' right( |
say 'step' right(#,length(z))": move disk on tower" arg(1) '───►' arg(2) |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
return</lang> |
return</lang> |
||
{{out}}when the default input was used: |
|||
{{out}} |
|||
<pre> |
<pre> |
||
step 1: move disk 1 |
step 1: move disk on tower 1 ───► 3 |
||
step 2: move disk 1 |
step 2: move disk on tower 1 ───► 2 |
||
step 3: move disk 3 |
step 3: move disk on tower 3 ───► 2 |
||
step 4: move disk 1 |
step 4: move disk on tower 1 ───► 3 |
||
step 5: move disk 2 |
step 5: move disk on tower 2 ───► 1 |
||
step 6: move disk 2 |
step 6: move disk on tower 2 ───► 3 |
||
step 7: move disk 1 |
step 7: move disk on tower 1 ───► 3 |
||
⚫ | |||
⚫ | |||
</pre> |
</pre> |
||
{{out}}when the following was entered (to solve with four |
{{out}}when the following was entered (to solve with four towers): <tt> 4 </tt> |
||
<pre> |
|||
<pre style="height:25ex"> |
|||
step 1: move disk 1 |
step 1: move disk on tower 1 ───► 2 |
||
step 2: move disk 1 |
step 2: move disk on tower 1 ───► 3 |
||
step 3: move disk 2 |
step 3: move disk on tower 2 ───► 3 |
||
step 4: move disk 1 |
step 4: move disk on tower 1 ───► 2 |
||
step 5: move disk 3 |
step 5: move disk on tower 3 ───► 1 |
||
step 6: move disk 3 |
step 6: move disk on tower 3 ───► 2 |
||
step 7: move disk 1 |
step 7: move disk on tower 1 ───► 2 |
||
step 8: move disk 1 |
step 8: move disk on tower 1 ───► 3 |
||
step 9: move disk 2 |
step 9: move disk on tower 2 ───► 3 |
||
step 10: move disk 2 |
step 10: move disk on tower 2 ───► 1 |
||
step 11: move disk 3 |
step 11: move disk on tower 3 ───► 1 |
||
step 12: move disk 2 |
step 12: move disk on tower 2 ───► 3 |
||
step 13: move disk 1 |
step 13: move disk on tower 1 ───► 2 |
||
step 14: move disk 1 |
step 14: move disk on tower 1 ───► 3 |
||
step 15: move disk 2 |
step 15: move disk on tower 2 ───► 3 |
||
The minimum number of moves to solve a 4 |
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). Coloring of the rings is attempted with dithering. |
||
In addition, it shows each move in a countdown manner (the last move is |
In addition, it shows each move in a countdown manner (the last move is marked as #1). |
||
No attempt is |
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). 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 (to the target tower). |
||
<lang rexx>/*REXX |
<lang rexx>/*REXX pgm shows pictorial moves to solve Tower of Hanoi (with N disks).*/ |
||
arg |
parse arg N .; if N=='' then N=3 /*Not given? Use default 3 disks.*/ |
||
sw=80; |
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 |
||
#=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= |
$.=0; $.1=N; k=N; kk=k+k |
||
do j=1 for |
do j=1 for N |
||
@.3.j=blanks; @.2.j=blanks |
@.3.j=blanks; @.2.j=blanks |
||
@.1.j= |
@.1.j=center(copies(box,kk),wp) |
||
if |
if N<=length(boxchars) then @.1.j=translate(@.1.j, , |
||
substr(boxchars,kk%2,1),box) |
|||
kk=kk-2 |
kk=kk-2 |
||
end /*j*/ |
end /*j*/ |
||
call showtowers; call mov 1,3, |
call showtowers; call mov 1,3,N |
||
say |
say |
||
say "The minimum number of moves |
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; |
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, |
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; |
$.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= |
showtowers: do j=N by -1 for N |
||
_=@.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 |
The minimum number of moves to solve a 3 Tower of Hanoi is 7 |
||
</pre> |
</pre> |
||