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 . /*obtain optional arguments from the CL*/
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, */
@.=0 /*define stem array, default: all white*/
Lb=1 ; Rb=100 /*the left boundary, right boundary*/
if char=='' | char=="," then char= '#' /*binary colors: 0≡white, 1≡black. */
Bb=1 ; Tb=100 /*the bottom " top " */
parse value scrSize() with sd sw . /*obtain the terminal's depth and width*/
x=(Rb-Lb)%2 ; y=(Tb-Bb)%2 /*approximate center where walk starts.*/
sd=sd-5; sw=sw01 /*adjust for terminal's useable area. */
$.=1; $.0=4; $.2=2; $.3=3; $.4=4 /* 1≡north 2≡east 3≡south 4≡west.*/
x=1000000 ; y=1000000 /*start ant's walk in middle of nowhere*/
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.*/
@.=0 ; minx=x; miny=y; maxx=x; maxy=y /*define stem array, default: all white*/
/* [↓] ant walks hither and thither. */
/* [↓] ant walks hither and thither. */
do steps=1 until x<Lb | x>Rb | y<Bb | y>Tb /*walk until the ant is out─of─bounds.*/
do steps=1 until (maxx-miny>sw) | (maxy-miny>sd) /*'til the ant is out─of─bounds.*/
black=@.x.y; @.x.y= \ @.x.y /*invert (flip) ant's cell color code.*/
black=@.x.y; @.x.y= \ @.x.y /*invert (flip) ant's cell color code.*/
if black then dir=dir-1 /*if cell color 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 the direction it's facing. */
select /*ant walks the direction it's facing. */
when dir==1 then y= y + 1 /*is ant 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*/
minx=min(minx,x); miny=min(miny,y)
end /*steps*/ /* [↑] the ant is finished walking. */
maxx=max(maxx,x); maxy=max(maxy,y)

say center(" Langton's ant walked " steps ' steps. ', 79, "─"); say
end /*steps*/
/*finished walking, it's out-of-bounds.*/
say center(" Langton's ant walked " steps ' steps. ', 79, "─")
/* [↓] show Langton's ant's trail. */
/* [↓] show Langton's ant's trail. */
do minx =Lb to Rb /*find the leftmost non─blank column. */
do y=maxy to miny by -1; _= /*display a single row of cells. */
do y=Bb to Tb /*search row by row for the minimum. */
do x=minx to maxx; _=_ || @.x.y /*build a cell row for the display. */
end /*x*/
if @.minx.y then leave minx /*found one, now we can quit searching.*/
_=strip( translate(_, char, 10), 'T') /*color the cells: black or white. */
end /*y*/
end /*minx*/ /* [↑] above code crops left of array.*/
if _\=='' then say _ /*display line (strip trailing blanks).*/
end /*y*/ /*stick a fork in it, we're all done. */</lang>
The terminal's screen size used was &nbsp; <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*/
_=strip(translate(_,char,10), 'T') /*color the cells: black or white. */
if _\=='' then say _ /*display line (strip trailing blanks).*/
end /*y*/ /*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; when using the default inputs:
'''output''' &nbsp; 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. ──────────────────────
# #

##
## # #
####
### #
# ## #
# ## #
## ####
## ### #
# # # #
# # # ###
## # ##
## # #####
# # ##
# # ### #
## # ###
# # ### #
## # ###
# # ### #
## # ###
# # ### #
## # ###
# # ### #
## # ###
# # ### #
## # ###
# # ### #
## # ###
# # ### #
## # ###
# # ### #
## # ###
# # ### #
## # ###
# # ### #
## # ###
# # ### #
## # ###
## # ###
## # # ### #
## # # ### #