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.


'This code will create a GUI Form to display the result
 
hGridView As GridView                                               'The display is on a GridView
iCol As Integer = 38                                                'Column start position
iRow As Integer = 30                                                'Row start position
 
Public Sub Form_show()
 
SetUpForm                                                           'Run the SetUpForm routine
Go                                                                  'Run the Go routine
 
End
 
Public Sub Go()                                                     'This is what does the work
Dim siDir As Short = 3                                              'Stores the Direction of the ant 0 = North, 1 = East, 2 = South ,3 = West
Dim siCount As Short                                                'Counter
 
Repeat                                                              'Repeat loop
  Inc siCount                                                       'Increase siCount
  If hGridView[iRow, iCol].background = -1 Then                     'If the Background of the cell is white then..(Right turn)
    hGridView[iRow, iCol].background = 0                            'Make the Background black
    siDir = Direction(siDir, True)                                  'Get the direction to turn
    If siDir = 0 Then Dec iRow                                      'Decrease Row if facing North
    If siDir = 1 Then Inc iCol                                      'Increase Column if facing East
    If siDir = 2 Then Inc iRow                                      'Increase Row if facing South
    If siDir = 3 Then Dec iCol                                      'Decrease Column if facing West
  End If 
'Wait                                                                'This will allow you to see the Grid being populated. Rem it out for an instant result
  If hGridView[iRow, iCol].background = 0 Then                      'If the Background of the cell is black then.. Left Turn
    hGridView[iRow, iCol].background = -1                           'Make the Background white
    siDir = Direction(siDir, False)                                 'Get the direction to turn
    If siDir = 0 Then Dec iRow                                      'Decrease Row if facing North
    If siDir = 1 Then Inc iCol                                      'Increase Column if facing East
    If siDir = 2 Then Inc iRow                                      'Increase Row if facing South
    If siDir = 3 Then Dec iCol                                      'Decrease Column if facing West
  End If 
Until siCount = 9660                                                'Loop 9660 times
 
End
 
Public Sub Direction(siDirection As Short, bWay As Boolean) As Byte 'To workout which way to go
 
If bWay Then                                                        'If turning Right then
  Inc siDirection                                                   'Increase siDirection e.g. 0 = North to 1 = East 
Else                                                                'Else if turning Left
  Dec siDirection                                                   'Decrease siDirection e.g. 2 = South to 1 = East 
End If
 
If siDirection < 0 Then siDirection = 3                             'To address 0 - 1 = -1
If siDirection > 3 Then siDirection = 0                             'To address 3 + 1 = 4
 
Return siDirection                                                  'Return the correct direction
 
End
 
Public Sub SetUpForm()                                              'Set up the Form and Create the Gridview
 
With Me                                                             'Change the Properties of the Form
  .Height = 1012                                                    'Set the Form Height
  .Width = 1012                                                     'Set the Form Width
  .Arrangement = Arrange.Vertical                                   'Set the Form Arrangement
  .Padding = 5                                                      'Set the Form Padding (Border)
  .title = "Langton's ant"                                          'Set the Form Title
End With
 
hGridView = New GridView(Me)                                        'Create a GridView
With hGridView                                                      'Change the Properties of the GridView
  .Columns.count = 100                                              'Create 100 Columns
  .Rows.count = 100                                                 'Create 100 Rows
  .Columns.Width = 10                                               'Set the Column Width
  .Rows.Height = 10                                                 'Set the Column Height
  .expand = True                                                    'Set the Gridview to Expand to fill the Form
  .background = -1                                                  'Set the Gridview background to White
End With
 
End