Langton's ant: Difference between revisions

→‎{{header|REXX}}: added comments, DO-END labels, remove superflous blanks. -- ~~~~
(→‎{{header|REXX}}: added comments, DO-END labels, remove superflous blanks. -- ~~~~)
Line 1,916:
 
=={{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*/
/*binary colors: 0=white, 1=black*/
@.=0 /*define stem array (all white).*/
lb=1 1 ; rb=100 /* right boundry, right boundry.*/
bb=1 1 ; tb=100 /*bottom boundry, top boundry.*/
x=(rb-lb)%2 ; y=(tb-bb)%2 /*approximate center (walk start)*/
if dir=='' then dir=random(1,4) /*ant is facing random direction,*/
/*1=north 2=east 3=south 4=west*/
 
/*───────────────────────────────────────────ant walks hither & thither.*/
do steps=1 until x<lb | x>rb | y<bb | y>tb /*walk until out-of-bounds*/
cellblack=@.x.y /*get color code of ant's cell. */
@.x.y=\@.x.y /*change the color of the cell. */
if cellblack then dir=dir-1 /*if cell iswas black, turn left. */
else dir=dir+1 /*if cell iswas white, turn right. */
if dir==0 then dir=4 /*ant should be facing "west". */
if dir==5 then dir=1 /*and should be facing "north". */
Line 1,940 ⟶ 1,938:
when dir==3 then y=y-1 /*walking south? Then go "down".*/
when dir==4 then x=x-1 /*walking west? Then go "left".*/
end /*select*/
end /*steps*/
 
/*───────────────────────────────────────────────display the ant's walk.*/
say "──────── Langton's ant walked" steps 'steps. ────────'; say
say
 
do minx=lb to rb /*find the leftest black column.*/
do y=bb to tb /*search row by row for it. */
if @.minx.y then leave minx /*found one, now quit searching.*/
end /*y*/
end /*minx*/ /*above code crops left of array*/
 
do y=tb to bb by -1; _='' /*display a plane (row) of cells*/
do x=minx to rb /*process a "row" of cells. */
_=_||@.x.y /*build a cell row for display. */
end /*x*/
_=translate(_,'#',10) /*color the cells: black | white*/
if _\='' then say strip(_,'T') /*crop completely white lines. */
end /*y*/</lang>
'''output'''
</lang>
Output
<pre style="height:60ex;overflow:scroll">
──────── Langton's ant walked 11759 steps. ────────