# Zhang Cellular Automata

Inspiration

Another new cellular automaton. This time inspired by this post by Xiaohan Zhang (@hellocharlien on Twitter). I messaged Xiaohan about his algorithm and he generously provided the source code to his CA processing sketch here.

I called them the “Zhang Cellular Automata” as there was no official name given to these.

How they work

Setup a 2D array of integers for the CA cells. Fill with random values between 0 and numstates-1. I allow up to 9 for the numstates which means a 0 dead state and 1-8 alive states for cells.

Create a rule table which is an array 9×9 integers in size. Fill with random values between 0 and numstates-1.

Now the steps when processing each cell.
1. Count how many of the 8 Moore Neighborhood neighbors there are in each state (between 0 and numstates-1). This gives you an array countStatesOfNeighbors[0..numstates-1].
2. Remember the current cell state value.
3. Loop through the possible state ranges from 0 to numstates-1.
4. If the rule entry matches the loop then assign the loop value to the cell, otherwise the cell remains as is.

Here is my code for updating each cell

``````
mystate:=c[x,y];
for s:=0 to numstates-1 do
begin
if (countStatesOfNeighbors[s]=rule[myState,s]) then
begin
mystate:=s;
break;
end;
end;
t[x,y]:=mystate;
```
```

Results

There are interesting cyclic results that tend to go through cycles of life and death. One state will fill out an area of random noise with a new pattern and that pattern allows a new pattern to fill it, before the original random state refills the area.