Langton's Loops- a self-reproducing Cellular Automaton.

This models an array of 8-state cells on a regular Cartesian grid. Synchronously, all cells update to a new set according to a rule table and their own present state & the four neighbour states.. this is four-fold symmetrical, so one rule that states '1 2345 6'is interpreted to mean that a cell currently in state 1, & with neighbours 2345 will become state 6- but so, in the other three compass directions, will '1 3452 6', '1 4523 6' and ' 1 5234 6'. This makes the rule table more comapact. A kind of 'genetic code' circulates in each loop, and as the 'genes' arrive at the active site they govern how it extends. The two main genes are one to extend and sheathe the core, and one to turn a right angle corner. The other states allow termination & disconnection of completed loops.

The starting gene-string, wrapped as shown, is ..

The Rule Table is as follows.

000000
000012
000020
000030
000050
000063
000071
000112
000122
000132
000212
000220
000230
000262
000272
000320
000525
000622
000722
001022
001120
002020
002030
002050
002125
002220
002322
005222
012321
012421
012525
012621
012721
012751
014221
014321
014421
014721
016251
017221
017255
017521
017621
017721
025271
100011
100061
100077
100111
100121
100211
100244
100277
100511
101011
101111
101244
101277
102026
102121
102211
102244
102263
102277
102327
102424
102626
102644
102677
102710
102727
105427
111121
111221
111244
111251
111261
111277
111522
112121
112221
112244
112251
112277
112321
112424
112621
112727
113221
122244
122277
122434
122547
123244
123277
124255
124267
125275
200012
200022
200042
200071
200122
200152
200212
200222
200232
200242
200250
200262
200272
200326
200423
200517
200522
200575
200722
201022
201122
201222
201422
201722
202022
202032
202052
202073
202122
202152
202212
202222
202272
202321
202422
202452
202520
202552
202622
202722
203122
203216
203226
203422
204222
205122
205212
205222
205521
205725
206222
206722
207122
207222
207422
207722
211222
211261
212222
212242
212262
212272
214222
215222
216222
217222
222272
222442
222462
222762
222772
300013
300022
300041
300076
300123
300421
300622
301021
301220
302511
401120
401220
401250
402120
402221
402326
402520
403221
500022
500215
500225
500232
500272
500520
502022
502122
502152
502220
502244
502722
512122
512220
512422
512722
600011
600021
602120
612125
612131
612225
700077
701120
701220
701250
702120
702221
702251
702321
702525
702720
The 'genetic code' amounts to descriptions of the state-transition a particular neighbourhood provokes.
0    Background    Black      Empty, quiescent, 'vacuum' state.
1    Core          Blue       Fill tube & 'conduct'.
2    Sheath        Red        Form the boundary /container of the gene in the loop.
3                  Green      Support left tyrning; bonding two arms; generating new off-shoot; cap off-shoot.    4                  Yellow     Control left-turning & finishing a sprouted loop.
5                  Pink       Disconnect parent from offspring.
6                  White      Point to where new sprout should start; guide sprout; finish sprout growth.
7                  Cyan       Hold info. on straight growth of arm & offspring.  

Code for a version in LB follows. LB is very slow for this kind of set-up, with two nested loops to scan the 'universe' of cells, and inside then a loop to query the neighbours' states. I've speeded it up a bit by only looking at the area of the 'iunuverse' known to include live cells, and increasing its boundaries as the pattern ( often said to look like coral) grows. I've also added code that saves the image of each generation, and if you have installed ImageMagick it turns these into an animated GIF.

I ran it for twelve hours on my 1.7G Athlon, and got 320 generations. The GIF can replay these at several per second....

While I've a few speed-up ideas, including 'boosting', but it's never going to be real-time. There are good Java simulations of Langton's Loops out there, and I also recommend the versatile 'Golly', freeware that generates most of the investigated Cellular Automata.

nomainwin

dim p( 128, 128), q( 128, 128)

y     = 0
itern = 1
colour$ = "black blue red green yellow pink white cyan"
global i, horizMin, horizMax, vertMin, vertMax, flag$

while a$ <>"="
  read a$
  for x =1 to len( a$ )
    p( x +64, y +64 ) =val( mid$( a$, x, 1))
  next x
  y = y +1
wend

data " 22222222"    '   initial conditions
data "2170140142"
data "2022222202"
data "272    212"
data "212    212"
data "202    212"
data "272    212"
data "21222222122222"
data "20710710711111"
data " 2222222222222"
data "="

dim r$( 400)

open "langton-.txt" for input as #in
  i =0
  while not( eof( #in))
    line input #in, r$( i)
    i =i +1
  wend
close #in

max = i -1
vertMin =63: vertMax =74: horizMin =64: horizMax =79

WindowWidth  =860: WindowHeight =780
UpperLeftX   = 10: UpperLeftY   =  1

button #m.b1, "Make Animation", [makeAnimGIF], LR, 150, 720, 130, 40

open "Langton's loop" for graphics_nsb_nf as #m
  #m "trapclose [quit]"
  #m "down ; size 11 ; fill 140 140 140 ; backcolor 200 210 70 ; font Arial 9 11"

do                                                      '   Display old cells while calculating new data.

  for x =horizMin to horizMax                           '   copy old cell data array p() to array q() to be updated.
    for y =vertMin to vertMax
      q( x, y) =p( x, y)
    next y
  next x

  flag$ =""
  for x =horizMin to horizMax                           '   calc new data from q() and put in array p().
    for y =vertMin to vertMax

      for i =0 to max                                   '   check all rules against current situation of cell & surroundings.
        old =val( mid$( r$( i), 1, 1))                  '   try matching first digits 2 to 5 of rule for match against cell.
        n   =val( mid$( r$( i), 2, 1))                  '   current, N, E, S, W for matching against rules array r()..
        e   =val( mid$( r$( i), 3, 1)): s   =val( mid$( r$( i), 4, 1)): w   =val( mid$( r$( i), 5, 1))

        call rule x, y, old, n, e, s, w                 '   check the four symmetry variants
        call rule x, y, old, e, s, w, n: call rule x, y, old, s, w, n, e: call rule x, y, old, w, n, e, s

        #m "color "; word$( colour$, q( x, y) +1)       '   display old data while calc'g new
        #m "set "; x *10 -300; " "; y *11 -304
        scan

      next i

    next y
  next x

  #m "font Courier_New 20 bold ; color white": #m "place 10 20": #m "\Gen'n "; str$( itern)
  #m "font Arial 9 11 ; flush": #m "getbmp scr 260 160 500 390"
  bmpsave "scr", "C:\Temp\scr" +right$( "000" +str$( itern), 3) +".bmp"    ' These will be made into an animated GIF later.
  itern =itern +1
  bmpsave "scr", "R:\scr.bmp"
  #m "cls"
  loadbmp "scr", "R:\scr.bmp"
  #m "drawbmp scr 260 160"
loop until itern =999

[quit]
  close #m
end

sub rule x, y, old, n, e, s, w
  if q( x, y ) = old _
  and q(   x,             ( y -1) and 127)  =n _
  and q( ( x -1) and 127,   y)              =w _
  and q(   x ,            ( y +1) and 127)  =s _
  and q( ( x +1) and 127,   y )             =e then
        p( x, y) =val( mid$( r$( i), 6, 1))
        if i <>0 and p( x, y) <>0 then
            '   We've changed a cell not originally empty. If it is on existing H & V limits, need to increase range for next generation....
            if x =horizMin then horizMin =horizMin -1
            if x =horizMax then horizMax =horizMax +1
            if y =vertMin  then vertMin  =vertMin  -1
            if y =vertMin  then vertMax  =vertMax  +1
        end if
  end if
end sub

[makeAnimGIF]
    IM$ ="cd C:\Program Files\ImageMagick-6.7.1-Q16"
    'run "cmd.exe /c "; chr$( 34); IM$; chr$( 34), HIDE
    IM$ ="convert -delay 50 C:\Temp\scr*.bmp R:\aniL2.gif"
    'run "cmd.exe /c "; chr$( 34); IM$; chr$( 34), HIDE
        '   Give it 60 seconds to execute (may not be enough for large images)
    'timer 20000, [on]
    'wait
  [on]
    'timer 0
    close #m
    end

Contact me if you have problems... mr.john.f@gmail.com