Langton's ant: Difference between revisions
Content added Content deleted
No edit summary |
m (→{{header|REXX}}: changed/added comments and whitespace, changed indentations, used dynamic field, so the ant is less constricted for its walk.) |
||
Line 4,651: | Line 4,651: | ||
<br>Or in other words, this REXX program only shows the pertinent part of the ant's walk-field. |
<br>Or in other words, this REXX program only shows the pertinent part of the ant's walk-field. |
||
<lang rexx>/*REXX program implements Langton's ant walk and displays the ant's path (finite field).*/ |
<lang rexx>/*REXX program implements Langton's ant walk and displays the ant's path (finite field).*/ |
||
parse arg dir char . |
parse arg dir char seed . /*obtain optional arguments from the CL*/ |
||
if datatype(seed, 'W') then call random ,,seed /*Integer? Then use it as a RANDOM SEED*/ |
|||
if char=='' | char=="," then char='#' /*binary colors: 0≡white, 1≡black. */ |
|||
if dir=='' | dir=="," then dir=random(1, 4) /*ant is facing a random direction, */ |
|||
⚫ | |||
if char=='' | char=="," then char= '#' /*binary colors: 0≡white, 1≡black. */ |
|||
parse value scrSize() with sd sw . /*obtain the terminal's depth and width*/ |
|||
sd=sd-5; sw=sw01 /*adjust for terminal's useable area. */ |
|||
x=1000000 ; y=1000000 /*start ant's walk in middle of nowhere*/ |
|||
$.=1; $.0=4 ; $.2=2; $.3=3; $.4=4 /* 1≡north 2≡east 3≡south 4≡west.*/ |
|||
⚫ | |||
/* [↓] ant walks hither and thither. */ |
/* [↓] ant walks hither and thither. */ |
||
do steps=1 until |
do steps=1 until (maxx-miny>sw) | (maxy-miny>sd) /*'til the ant is out─of─bounds.*/ |
||
black=@.x.y; |
black=@.x.y; @.x.y= \ @.x.y /*invert (flip) ant's cell color code.*/ |
||
if black then dir=dir-1 |
if black then dir=dir-1 /*if cell color was black, turn left.*/ |
||
else dir=dir+1 |
else dir=dir+1 /* " " " " white, turn right.*/ |
||
dir=$.dir |
dir=$.dir /*possibly adjust for under/over. */ |
||
select /*ant walks the direction it's facing. */ |
|||
when dir==1 then y= y + 1 |
when dir==1 then y= y + 1 /*is ant walking north? Then go up. */ |
||
when dir==2 then x= x + 1 |
when dir==2 then x= x + 1 /* " " " east? " " right.*/ |
||
when dir==3 then y= y - 1 |
when dir==3 then y= y - 1 /* " " " south? " " down. */ |
||
when dir==4 then x= x - 1 |
when dir==4 then x= x - 1 /* " " " west? " " left. */ |
||
end /*select*/ |
end /*select*/ |
||
minx=min(minx,x); miny=min(miny,y) |
|||
⚫ | |||
maxx=max(maxx,x); maxy=max(maxy,y) |
|||
end /*steps*/ |
|||
⚫ | |||
say center(" Langton's ant walked " steps ' steps. ', 79, "─") |
|||
/* [↓] show Langton's ant's trail. */ |
/* [↓] show Langton's ant's trail. */ |
||
do |
do y=maxy to miny by -1; _= /*display a single row of cells. */ |
||
do |
do x=minx to maxx; _=_ || @.x.y /*build a cell row for the display. */ |
||
⚫ | |||
if @.minx.y then leave minx /*found one, now we can quit searching.*/ |
|||
⚫ | |||
⚫ | |||
if _\=='' then say _ /*display line (strip trailing blanks).*/ |
|||
⚫ | |||
The terminal's screen size used was <big>80</big>x<big>160</big>. |
|||
do y=Tb to Bb by -1; _= /*display a single row of cells. */ |
|||
do x=minx to Rb; _=_ || @.x.y /*build a cell row for the display. */ |
|||
end /*x*/ |
|||
⚫ | |||
if _\=='' then say _ /*display line (strip trailing blanks).*/ |
|||
⚫ | |||
'''output''' when using the default inputs: |
'''output''' when using the default inputs: |
||
<br><br>(Shown at <sup>'''2'''</sup>/<sub>'''3'''</sub> size.) |
<br><br>(Shown at <sup>'''2'''</sup>/<sub>'''3'''</sub> size.) |
||
<b> |
<b> |
||
<pre style="font-size:67%"> |
<pre style="font-size:67%"> |
||
───────────────────── Langton's ant walked 12960 steps. ───────────────────── |
|||
────────────────────── Langton's ant walked 11759 steps. ────────────────────── |
|||
# # |
|||
## |
## # # |
||
#### |
### # |
||
# ## # |
# ## # |
||
## #### |
## ### # |
||
# # # # |
# # # ### |
||
## # |
## # ##### |
||
# # ## |
# # ### # |
||
## # ### |
|||
# # ### # |
|||
## # ### |
|||
# # ### # |
|||
## # ### |
|||
# # ### # |
|||
## # ### |
|||
# # ### # |
|||
## # ### |
|||
# # ### # |
|||
## # ### |
|||
# # ### # |
|||
## # ### |
|||
# # ### # |
|||
## # ### |
|||
# # ### # |
|||
## # ### |
|||
# # ### # |
|||
## # ### |
|||
# # ### # |
|||
## # ### |
|||
# # ### # |
|||
## # ### |
## # ### |
||
## # # ### # |
## # # ### # |