Langton's ant: Difference between revisions

Content added Content deleted
(added GFA Basic example)
m (→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.)
Line 4,649: Line 4,649:
This REXX program automatically justifies (or ''crops'') the '''left''', '''right''', '''top''' and '''bottom''' of the ant's walk field on the screen.
This REXX program automatically justifies (or ''crops'') the '''left''', '''right''', '''top''' and '''bottom''' of the ant's walk field on the screen.
<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*/
<lang rexx>/*REXX program implements Langton's ant walk and displays the ant's path (finite field).*/
parse arg dir char . /*allow specification: ant facing*/
parse arg dir char . /*obtain optional arguments from the CL*/
if char=='' then char='#' /*binary colors: 0≡white, 1≡black*/
if char=='' | char=="," then char='#' /*binary colors: 0≡white, 1≡black. */
@.=0 /*define stem array (all white).*/
@.=0 /*define stem array, default: all white*/
Lb=1 ; Rb=100 /* left boundry, right boundry.*/
Lb=1 ; Rb=100 /*the left boundary, right boundary*/
Bb=1 ; Tb=100 /*bottom " top " */
Bb=1 ; Tb=100 /*the bottom " top " */
x=(Rb-Lb)%2 ; y=(Tb-Bb)%2 /*approximate center (walk start)*/
x=(Rb-Lb)%2 ; y=(Tb-Bb)%2 /*approximate center where walk starts.*/
$.=1; $.0=4; $.2=2; $.3=3; $.4=4 /* 1≡north 2≡east 3≡south 4≡west.*/
if dir=='' then dir=random(1,4) /*ant is facing random direction,*/
if dir=='' then dir=random(1, 4) /*ant is facing a random direction, */
$.=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 & thither.*/
do steps=1 until x<Lb | x>Rb | y<Bb | y>Tb /*walk until out─of─bounds*/
do steps=1 until x<Lb | x>Rb | y<Bb | y>Tb /*walk until the ant is out─of─bounds.*/
black=@.x.y; @.x.y= \ @.x.y /*ant's cell color code; flip it.*/
black=@.x.y; @.x.y= \ @.x.y /*invert (flip) ant's cell color code.*/
if black then dir=dir-1 /*if cell was black, turn left.*/
if black then dir=dir-1 /*if cell color was black, turn left.*/
else dir=dir+1 /* " " " white, turn right.*/
else dir=dir+1 /* " " " " white, turn right.*/
dir=$.dir /*possibly adjust for under/over.*/
dir=$.dir /*possibly adjust for under/over. */
select /*ant walks direction it's facing*/
select /*ant walks the direction it's facing. */
when dir==1 then y= y + 1 /*walking north? Then go "up". */
when dir==1 then y= y + 1 /*is ant walking north? Then go up. */
when dir==2 then x= x + 1 /* " east? " " "right"*/
when dir==2 then x= x + 1 /* " " " east? " " right.*/
when dir==3 then y= y - 1 /* " south? " " "down".*/
when dir==3 then y= y - 1 /* " " " south? " " down. */
when dir==4 then x= x - 1 /* " west? " " "left".*/
when dir==4 then x= x - 1 /* " " " west? " " left. */
end /*select*/
end /*select*/
end /*steps*/
end /*steps*/ /* [↑] the ant is finished walking. */

/*───────────────────────────────────────────the ant is finished walking*/
say center(" Langton's ant walked" steps 'steps. ', 79, "─"); say
say center(" Langton's ant walked " steps ' steps. ', 79, "─"); say
/* [↓] show Langton's ant's trail*/
/* [↓] show Langton's ant's trail. */
do minx =Lb to Rb /*find leftmost non─blank column.*/
do minx =Lb to Rb /*find the leftmost non─blank column. */
do y=Bb to Tb /*search row by row for the min. */
do y=Bb to Tb /*search row by row for the minimum. */
if @.minx.y then leave minx /*found one, now quit searching. */
if @.minx.y then leave minx /*found one, now we can quit searching.*/
end /*y*/
end /*y*/
end /*minx*/ /*above code crops left of array.*/
end /*minx*/ /* [↑] above code crops left of array.*/


do y=Tb to Bb by -1; _= /*display a plane (row) of cells.*/
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 display. */
do x=minx to Rb; _=_ || @.x.y /*build a cell row for the display. */
end /*x*/
end /*x*/
_=strip(translate(_,char,10), 'T') /*color the cells: black | white.*/
_=strip(translate(_,char,10), 'T') /*color the cells: black or white. */
if _\=='' then say _ /*say line, strip trailing blanks*/
if _\=='' then say _ /*display line (strip trailing blanks).*/
end /*y*/ /*stick a fork in it, we're done.*/</lang>
end /*y*/ /*stick a fork in it, we're all done. */</lang>
'''output'''
'''output''' &nbsp; when using the default inputs:
<br><br>(Shown at <sup>'''3'''</sup>/<sub>'''4'''</sub> size.)

<br>(Shown at three-quarter size.)

<pre style="font-size:75%">
<pre style="font-size:75%">
────────────────────── Langton's ant walked 11759 steps. ──────────────────────
────────────────────── Langton's ant walked 11759 steps. ──────────────────────


##
##