Langton's ant is a cellular automaton that models an ant sitting on a plane of cells, all of which are white initially, the ant facing in one of four directions.

Each cell can either be black or white.

The ant moves according to the color of the cell it is currently sitting in, with the following rules:

   If the cell is black, it changes to white and the ant turns left;
   If the cell is white, it changes to black and the ant turns right;
   The ant then moves forward to the next cell, and repeat from step 1.

This rather simple ruleset leads to an initially chaotic movement pattern, and after about 10000 steps, a cycle appears where the ant moves steadily away from the starting location in a diagonal corridor about 10 cells wide.
Conceptually the ant can then walk infinitely far away.



Task

Start the ant near the center of a 100x100 field of cells, which is about big enough to contain the initial chaotic part of the movement.

Follow the movement rules for the ant, terminate when it moves out of the region, and show the cell colors it leaves behind.



The problem has received some analysis; for more details, please take a look at the Wikipedia article   (a link is below)..



See also

   Wikipedia:   Langton's ant.


Related task

   Rosetta Code:   Conway's Game of Life.


 
Module Ant {
	Form 120,102
	N=100
	Enum CellColor {black=0,white=#FFFFFF}
	Enum Direction{North=90, West=180, South=270, East=0}
	Function Rotate(cd as Direction, clockwise=true) {
			cd=(cd+if(clockwise->270,90)) mod 360
			=cd  ' return a Direction Enum type
	}
 
	dim rect(1 to N, 1 to N)=white
	cx=N div 2
	cy=N div 2
	cd=North
	rect(cx,cy)=black
	endmove=False
	while not endmove
		movecell()
 
	end while
	Disp()
 
	sub movecell()
		select case rect(cx,cy)
		case black
			cd=Rotate(cd, false) : rect(cx, cy)=white
		case white
			cd=Rotate(cd) : rect(cx, cy)=black
		end select
		select case cd
		case North
			cy--
		case West
			cx--
		case South
			cy++
		case East
			cx++
		end select
		endmove= cx<1 or cx>N or cy<1 or cy>N
	end sub
	sub disp()
		Local Doc$, i, j
		Document Doc$
		for i=1 to N:for j=1 to N
			Doc$=if$(rect(j,i)=White->"_","#")
		next
		Doc$={
		}
		next
		cls
		Print #-2,Doc$
		clipboard Doc$
	end sub
}
Ant
 