Langton's ant: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: added comments, DO-END labels, remove superflous blanks. -- ~~~~)
Line 1,916: Line 1,916:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program implements Langton's ant and displays it's walk(s). */
<lang rexx>
/*Langton's ant*/
parse arg dir . /*allow specification: ant facing*/
parse arg dir . /*allow specification: ant facing*/
/*binary colors: 0=white, 1=black*/
/*binary colors: 0=white, 1=black*/
@.=0 /*define stem array (all white).*/
@.=0 /*define stem array (all white).*/
lb= 1 ; rb=100 /* right boundry, right boundry.*/
lb=1 ; rb=100 /* right boundry, right boundry.*/
bb= 1 ; tb=100 /*bottom boundry, top boundry.*/
bb=1 ; tb=100 /*bottom boundry, top boundry.*/
x=(rb-lb)%2 ; y=(tb-bb)%2 /*approximate center (walk start)*/
x=(rb-lb)%2 ; y=(tb-bb)%2 /*approximate center (walk start)*/
if dir=='' then dir=random(1,4) /*ant is facing random direction,*/
if dir=='' then dir=random(1,4) /*ant is facing random direction,*/
/*1=north 2=east 3=south 4=west*/
/*1=north 2=east 3=south 4=west*/

/*───────────────────────────────────────────ant walks hither & 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 out-of-bounds*/
cell=@.x.y /*get color code of ant's cell. */
black=@.x.y /*get color code of ant's cell. */
@.x.y=\@.x.y /*change the color of the cell. */
@.x.y=\@.x.y /*change the color of the cell. */
if cell then dir=dir-1 /*if cell is black, turn left. */
if black then dir=dir-1 /*if cell was black, turn left. */
else dir=dir+1 /*if cell is white, turn right. */
else dir=dir+1 /*if cell was white, turn right. */
if dir==0 then dir=4 /*ant should be facing "west". */
if dir==0 then dir=4 /*ant should be facing "west". */
if dir==5 then dir=1 /*and should be facing "north". */
if dir==5 then dir=1 /*and should be facing "north". */
Line 1,940: Line 1,938:
when dir==3 then y=y-1 /*walking south? Then go "down".*/
when dir==3 then y=y-1 /*walking south? Then go "down".*/
when dir==4 then x=x-1 /*walking west? Then go "left".*/
when dir==4 then x=x-1 /*walking west? Then go "left".*/
end
end /*select*/
end
end /*steps*/

/*───────────────────────────────────────────────display the ant's walk.*/
/*───────────────────────────────────────────────display the ant's walk.*/
say "──────── Langton's ant walked" steps 'steps. ────────'
say "──────── Langton's ant walked" steps 'steps. ────────'; say
say


do minx=lb to rb /*find the leftest black column.*/
do minx=lb to rb /*find the leftest black column.*/
do y=bb to tb /*search row by row for it. */
do y=bb to tb /*search row by row for it. */
if @.minx.y then leave minx /*found one, now quit searching.*/
if @.minx.y then leave minx /*found one, now quit searching.*/
end
end /*y*/
end /*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 plane (row) of cells*/
do x=minx to rb /*process a "row" of cells. */
do x=minx to rb /*process a "row" of cells. */
_=_||@.x.y /*build a cell row for display. */
_=_||@.x.y /*build a cell row for display. */
end
end /*x*/
_=translate(_,'#',10) /*color the cells: black | white*/
_=translate(_,'#',10) /*color the cells: black | white*/
if _\='' then say strip(_,'T') /*crop completely white lines. */
if _\='' then say strip(_,'T') /*crop completely white lines. */
end
end /*y*/</lang>
'''output'''
</lang>
Output
<pre style="height:60ex;overflow:scroll">
<pre style="height:60ex;overflow:scroll">
──────── Langton's ant walked 11759 steps. ────────
──────── Langton's ant walked 11759 steps. ────────