## Cellular Automaton

### by Thomas Watson [http://www.cs.wisc.edu/~twatson/]

By Thomas Watson, May 10, 2006

See [http://en.wikipedia.org/wiki/Cellular_automaton] for an explanation of cellular automata.

When the program prompts you for a number, try 18, 30, 99, 100

```nomainwin
global rule
global wide : wide = 400
global high : high = 400
WindowWidth = wide + 2 : WindowHeight = high + 22

dim a(1000) 'current line
dim b(1000) 'next line

open "Cellular Automaton" for graphics_nsb_nf as #1
#1 "trapclose Quit"

#1 "down"

[again]
#1 "cls"

'set up an initial condition (fill in a(1) through a(wide) with bits 0 or 1):
for i = 1 to wide / 2 - 1
a(i) = 0
next

a(wide / 2) = 1

for i = wide / 2 + 1 to wide
a(i) = 0
next
'end initial condition setup

num = 18
prompt "Pick a number between 0 and 255"; num
if (num<0) or (num>255) then num = 18
rule = num

for j = 1 to high
scan
call DrawLine j
call BfromA
next j
#1 "flush"

confirm "Try again?"; answer\$
if answer\$ = "yes" then [again]
wait

Sub BfromA
for i = 1 to wide
index = a(i-1) * 4 + a(i) * 2 + a(i+1)
if (rule and (2 ^ index)) = 0 then
b(i) = 0
else
b(i) = 1
end if
next i

for i = 1 to wide
a(i) = b(i)
next i
end sub

Sub DrawLine lineY
for i = 1 to wide
if a(i) then #1 "set "; i - 1; " "; lineY - 1
next i
end sub

Sub Quit handle\$
close #handle\$
end
end sub
```