Cellular Automata

Cellular automata are very important mathematical objects. They are capable of universal computation and are fundamental to the modern theory of computation.

This program constructs a very basic cellular automata.

to cellaut
setpencolor (list 254 254 254)
cs
make "a (array 40 0)
make "b (array 40 0)
for [c 0 39 1][
setitem :c :a random 2
]
for [x 0 150 1] [
pu home rt 90 fd -400+:x*5
lt 90 pd
ifelse ( (item 0 :a)+(item 1 :a)+(item 39 :a)= 1) [ setitem 0 :b 1] [ setitem 0 :b 0]
ifelse ( (item 38 :a)+(item 39 :a)+(item 0 :a)= 1) [ setitem 39 :b 1] [ setitem 39 :b 0]
for [c 1 38 1][
ifelse ( (item :c+1 :a)+(item :c :a)+(item :c-1 :a)= 1) [ setitem :c :b 1] [ setitem :c :b 0]
]
for [c 0 39 1][
setitem :c :a item :c :b
ifelse ( (item :c :b)= 0) [ setfc [ 0 0 0]] [ setfc [ 255 255 255]]
repeat 4 [ fd 5 rt 90]
pu rt 45 fd 2 fill bk 2 lt 45 pd
fd 5
]]
end


This is a fairly complex program.
It uses arrays to store values. Arrays are read using the item command. Values are put into arrays by using the setitem command.
The array is read from array :a constructed in array :b and drawn to the screen. The array :b is then written into array :a and the process repeats.

This cellular automata uses a transition rule which fills the child cell to the left iff (if and only if) one of its three parent cells is full. The parent cells are the cells that are level with, one above and one below the child cell in the column to the right.