Anonymous user
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
parse arg
if
call mov 1, 3,
say
say 'The minimum number of moves to solve a '
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. */
say 'step' right(
▲return
▲/*─────────────────────────────MOV subroutine───────────────────────────*/
▲ else do
▲ end
return</lang>
{{out}}when the default input was used:
<pre>
step 1: move disk on tower 1
step 2: move disk on tower 1
step 3: move disk on tower 3
step 4: move disk on tower 1
step 5: move disk on tower 2
step 6: move disk on tower 2
step 7: move disk on tower 1
▲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
<pre>
step 1: move disk on tower 1
step 2: move disk on tower 1
step 3: move disk on tower 2
step 4: move disk on tower 1
step 5: move disk on tower 3
step 6: move disk on tower 3
step 7: move disk on tower 1
step 8: move disk on tower 1
step 9: move disk on tower 2
step 10: move disk on tower 2
step 11: move disk on tower 3
step 12: move disk on tower 2
step 13: move disk on tower 1
step 14: move disk on tower 1
step 15: move disk on tower 2
The minimum number of moves to solve a 4
</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). Coloring of the rings is attempted with dithering.
In addition, it shows each move in a countdown manner (the last move is
No attempt is
<lang rexx>/*REXX
parse arg
sw=80;
c.1=sw%3%2
c.2=sw%2-1
c.3=sw-1-c.1-1
@abc='abcdefghijklmnopqrstuvwxyN' /*dithering chars when many disks*/
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=
do j=1 for
@.3.j=blanks; @.2.j=blanks
@.1.j=
if
kk=kk-2
end /*j*/
call showtowers; call mov 1,3,
say
say "The minimum number of moves
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;
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,
say translate(pp,verts,Tcorners||Bcorners||bar)
say translate(pp,downs,Tcorners||Bcorners||bar)
movek=movek-1
$.from=$.from-1; $.dest=$.dest+1;
@.dest._t=@.from._f; @.from._f=blanks
call showtowers
return
/*─────────────────────────────SHOWTOWERS subroutine────────────────────*/
showtowers: do j=
end /*j*/
return</lang>
{{out}}when the default input was used:
<pre>
░░
▒▒▒▒
▓▓▓▓▓▓
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
</pre>
|