Implementation [of 2D cellular automata]

An n × n array of white squares with a single black square in the middle can be generated by

PadLeft[{{1}}, {n, n}, 0, Floor[{n, n}/2]]

For the 5-neighbor rules introduced on page 170 each step can be implemented by

CAStep[rule_, a_] := Map[rule[[10-#]] &,

ListConvolve[{{0, 2, 0}, {2, 1, 2}, {0, 2, 0}}, a, 2], {2}]

where rule is obtained from the code number by IntegerDigits[code, 2, 10].

For the 9-neighbor rules introduced on page 177

CAStep[rule_, a_] := Map[rule[[18- #]] &,

ListConvolve[{{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}, a, 2], {2}]

where rule is given by IntegerDigits[code, 2, 18].

In d dimensions with k colors, 5-neighbor rules generalize to (2d+1)-neighbor rules, with

CAStep[{rule_, d_}, a_] := Map[rule[[-1-#]] &, a + k AxesTotal[a, d], {d}]

AxesTotal[a_, d_] := Apply[Plus, Map[ (RotateLeft[a, #] + RotateRight[a, #])&, IdentityMatrix[d]]]

with rule given by IntegerDigits[code, k, k(2d(k-1) + 1)].

9-neighbor rules generalize to 3^^{d}-neighbor rules, with

CAStep[{rule_, d_}, a_] := Map[rule[[-1-#]]&, a + k FullTotal[a, d], {d}]

FullTotal[a_, d_] := Array[RotateLeft[a, {##}]&, Table[3, {d}], -1, Plus] - a

with rule given by IntegerDigits[code, k, k((3^^{d}-1)(k-1) + 1)].

In 3 dimensions, the positions of black cells can conveniently be displayed using

Graphics3D[ Map[ Cuboid[-Reverse[#]]&, Position[a, 1] ] ]