Langton's ant: Difference between revisions

→‎{{header|REXX}}: added the REXX language solution.
(→‎{{header|REXX}}: added the REXX language solution.)
Line 1,728:
=={{header|REXX}}==
<lang rexx>
/*Langton's ant*/
xxx
/*binary colors: 0=white, 1=black*/
@.=0 /*define stem array (all white).*/
lb= 1 ; rb=100 /* right boundry, right boundry.*/
bb= 1 ; tb=100 /*bottom boundry, top boundry.*/
x=(rb-lb)%2 ; y=(tb-bb)%2 /*approximate center (walk start)*/
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*/
cell=@.x.y /*get color code of ant's cell. */
@.x.y=\@.x.y /*change the color of the cell. */
if cell then dir=dir-1 /*if cell is black, turn left. */
else dir=dir+1 /*if cell is white, turn right. */
if dir==0 then dir=4 /*ant should be facing "west". */
if dir==5 then dir=1 /*and shouod be facing "north". */
select /*ant walks direction it's facing*/
when dir==1 then y=y+1 /*walking north? Then go "up". */
when dir==2 then x=x+1 /*walking east? Then go "right"*/
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
end
 
 
/*───────────────────────────────────────────────display the ant's walk.*/
say "──────── Langton's ant walked" steps 'steps. ────────'
say
do y=tb to bb by -1; r='' /*display the plane of cells. */
do x=lb to rb /*process a "row" of cells. */
r=r||@.y.x /*build a cell row for display. */
end
r=translate(r,' █',01) /*color the cells: black | white*/
if r\='' then say strip(r,'T') /*crop blank parts of the plane.*/
end
</lang>
Output
<pre style="height:40ex;overflow:scroll">
──────── Langton's ant walked 11565 steps. ────────
 
██
████
█ ██ █
████ ██
█ █ █ █ █ ██
███ █ ██ ██ ██
█ ███ █ █ █ ██ █ █ █
███ █ ██ █ █ ███ ███
█ ███ █ █ █ ██ ████ █
███ █ ██ ██
█ ███ █ █ ██ ██ ██
███ █ ██ █ ██ █ ████
█ ███ █ █ ██ ██ ██ ███ ██ █████
███ █ ██ ██ ████ ██ █ ██ █ █ █
█ ███ █ █ █ ██ ████ ████ ███ ████
███ █ ██ ██ █ ██ ██ █ █ ███
█ ███ █ █ █ █ █ █ ██ ██ ██ ████
███ █ ██ █ █ █ ██ ██ █ █ ██ █
█ ███ █ █ █ █ █ ████████ █ █ ██ ████ █
███ █ ██ ██ ██ █ █ █ ██ ██ █ █ ██ █ ██ ██
█ ███ █ █ █████ █ ██ ██ █ █ █ █ █ █ █ █
███ █ ██ █ ██ ██ █ █ ████ █ ██ ████ ██
█ ███ █ █ █ █ ████ █ █████ ██ ██████████ ██
███ █ ██ █ █ ██ █████ ██ █ █ █ █ ██ █ █ █
█ ███ █ █ █ █████ █ █████ █ █ ██ █ ██ █
███ █ ██ █ █ ██████ ██ █ ██ █ █ ███ ███ ██ █
█ ███ █ █ ██ █ ██ ██ ██ ███ ███ █ █ ██ ████ █
███ █ ██ █ ████ ██ █████████ █ ██ █ ██
█ ███ █ █ █ ██ ███████████ █ ████ █ █ █
███ █ ██ ███ ██ █ ██ ████ ████ █ █
█ ███ █ █ █ █ ███ █ █ ██ ██ █ █ █ █ █
███ █ █ ██ █ ██ ██ █████ ████ ████ ██ █
█ ███ █ ██ █ █ ███ █ ███ ██ █ █ ██
███ █ █ █ █ █ ████ ██ █ ██ ███ █ █
█ ███ ██ ██ ██ ███ █ █ ██ ████ █
███ █ █ ██ ███ █ ██ █ ███ ██ ██ █
█ █ ██ ██ █ ██ ██ █ ██
█ █ █ ██ ███ █ █ █ ████ █ ██
█ ███ █ ██ █ █ ██ ██ ██ █ █████ ███ ██
███ ██ █ ████ ██ ██ ██████ █ ███ █ █
█ █ █████ █ █ ████ █ ███ █ █ █
█ ███ ██ █ ██ ███ █ █ ███ ███
█ █ ███ ██ ████ ██ █ █ █ ██ ██
███ █ █ █ █ ██ ██ ███
██ ██ █ █ ███ █
█ █ █ █ █ █
███ ██ ██ █
█ ████ █ ██
██ ████████████ ██
</pref>
 
=={{header|Ruby}}==