Langton's ant: Difference between revisions

→‎{{header|REXX}}: added/changed comments and whitespace, add a more idiomatic direction handler, added support for the character used for the path.
m (→‎{{header|Sidef}}: minor code simplifications)
(→‎{{header|REXX}}: added/changed comments and whitespace, add a more idiomatic direction handler, added support for the character used for the path.)
Line 3,801:
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.
<lang rexx>/*REXX program implements Langton's ant walk and displays the pathant's it walked.path*/
parse arg dir char . /*allow specification: ant facing*/
if char=='' then char='#' /*binary colors: 0=white0≡white, 1=black1≡black*/
@.=0 /*define stem array (all white).*/
lbLb=1 ; rbRb=100 /* left boundry, right boundry.*/
bbBb=1 ; tbTb=100 /*bottom " top " */
x=(rbRb-lbLb)%2 ; y=(tbTb-bbBb)%2 /*approximate center (walk start)*/
if dir=='' then dir=random(1,4) /*ant is facing random direction,*/
$.=1; $.0=4; $.2=2; $.3=3; $.4=4 /*1≡north 2≡east 3≡south 4≡west*/
/*1=north 2=east 3=south 4=west*/
/*───────────────────────────────────────────ant walks hither & thither.*/
do steps=1 until x<lbLb | x>rbRb | y<bbBb | y>tb Tb /*walk until out-of-boundsout─of─bounds*/
black=@.x.y; @.x.y= \ @.x.y /*getant's cell color code; offlip ant's cellit. */
@.x.y=\@.x.y if black then dir=dir-1 /*"flip"if thecell colorwas ofblack, the cell.turn left.*/
if black then else dir=dir-+1 /*if cell" was black " " white, turn leftright. */
dir=$.dir else dir=dir+1 /* " "/*possibly adjust for " white, " rightunder/over. */
if dir==0 then dir=4select /*ant should be facing "west". /*ant walks direction it's facing*/
if dir==5 then when dir==1 then y= y + 1 /*walking " north? Then go " up". " "north". */
when selectdir==2 then x= x + 1 /* " east? " /*ant walks direction" it's facing"right"*/
when dir==13 then y= y+1 - 1 /*walking north " south? Then go "up ". "down".*/
when dir==24 then x= x+1 - 1 /* " eastwest? " " "rightleft".*/
end /*select*/
when dir==3 then y=y-1 /* " south? " " "down".*/
end /*steps*/
when dir==4 then x=x-1 /* " west? " " "left".*/
end /*select*/
end /*steps*/
/*───────────────────────────────────────────the ant is finished walking*/
say center(" Langton's ant walked" steps 'steps. ', 79, "─"); say
/*Display [↓] show Langton's ant's trail. */
do minx =lbLb to rb Rb /*find leftmost non-blanknon─blank column.*/
do y=bbBb to tb Tb /*search row by row for itthe min. */
if @.minx.y then leave minx /*found one, now quit searching. */
end /*y*/
end /*minx*/ /*above code crops left of array.*/
 
do y=tbTb to bbBb by -1; _='' /*display a plane (row) of cells.*/
do x=minx to rb Rb; _=_ || @.x.y /*processbuild a "cell row" offor cellsdisplay. */
end /*x*/
_=_ || @.x.y /*build a cell row for display. */
_=strip(translate(_,char,10), 'T') /*color the cells: black | white.*/
end /*x*/
if _\==translate(_,'#',10) then say _ /*colorsay the cells:line, blackstrip |trailing white.blanks*/
end /*y*/ /*1=northstick a 2=eastfork in 3=southit, 4=westwe're done.*/</lang>
if _\='' then say strip(_,'T') /*say line, strip trailing blanks*/
end /*y*/
/*stick a fork in it, we're done.*/</lang>
'''output'''
<pre style="height:60ex">