CA-LifeGame

In [ ]:
showSites[sites_, opts___] :=
    ArrayPlot[sites, opts, Mesh -> True, PixelConstrained -> 4]
    
makeConfiguration[n_, form_] := Module[{config, sites},
  config = ConstantArray[0, {n, n}];
  sites = form[Floor[n/2] + 1, Floor[n/2] + 1];
  sites = {#[[2]], #[[1]]} & /@ sites;
  Reverse[ReplacePart[config, 1, sites]]]

進化ルール

  • 自身 $c_i$ を除く周囲8個のサイト値の合計が3の時、$c_{i+1}$の値を1とする。
  • 自身 $c_i$の値が1の時、周囲8個のサイト値の合計が2の時、$c_{i+1}$の値を1とする。
  • それ以外は $c_i$の値を0とする。
In [ ]:
lifeGameSteps[board_, steps_ : 10] := With[{
   rule = {
     {{nw_, n_, ne_}, {w_, c_, e_}, {sw_, s_, se_}} :>
         Which[
             Plus[nw, n, ne, w, e, sw, s, se] == 3, 1,
             c == 1 && Plus[nw, n, ne, w, e, sw, s, se] == 2, 1,
            True, 0]},
   init = board,
   step = steps},
  CellularAutomaton[rule, init, step]]

例 beehive

In [ ]:
beehive[x_, y_] :=
  {{x, y - 2}, {x, y - 1}, {x, y}, {x, y + 1}, {x, y + 2}};
config = makeConfiguration[21, beehive];
showSites[config]
Out[ ]:
Output
In [ ]:
showSites /@ lifeGameSteps[config]
Out[ ]:
Output

例 glider

In [ ]:
glider[x_, y_] :=
  {{x, y}, {x + 1, y}, {x + 2, y}, {x + 2, y + 1}, {x + 1, 
    y + 2}};
config = makeConfiguration[21, glider];
showSites[config]
Out[ ]:
Output
In [ ]:
showSites /@ lifeGameSteps[config, 20]
Out[ ]:
Output
In [ ]: