I need to implement a new "game of life" in Mathematica: I have three states — black, red and white. Black means go forward, red go back and white take no action.
I would like to implement the outcomes in this Excel file: http://cl.ly/0s2B2M3h3M2N
The code I have used is the following:
input13 = {{1, 1, 0, 0, 0}, {2, 0, 1, 2, 0}, {1, 1, 2, 1, 0}, {0, 1,
2, 1, 2}, {2, 1, 0, 1, 1}};
getNbhd[A_, i_Integer?Positive, j_Integer?Positive] := A[[i - 1 ;; i + 1, j - 1 ;; j + 1]];
evaluateCell[A_, i_, j_] := Module[{nbhd, cell = A[[i, j]], numNeighbors},
If[i == 1 || j == 1 || i == Length[A] || j == Length[A[[1]]], Return[0]];
nbhd = getNbhd[A, i, j]; numNeighbors = Apply[Plus, Flatten[nbhd]];
If[cell == 2 && numNeighbors - 2 >= 4 ||
numNeighbors - 2 == 3 && numNeighbors - 1 <= 3 ||
numNeighbors - 2 == 2 && numNeighbors - 1 <= 2 ||
numNeighbors - 2 == 1 && numNeighbors - 1 <= 1 ||
numNeighbors - 2 == 0 && numNeighbors - 0 == 8, Return[2]; If[cell == 2 && numNeighbors - 2 == numNeighbors - 1, Return[0],
Return[1]]];
If[cell == 1 && numNeighbors - 1 >= 4 ||
numNeighbors - 1 == 3 && numNeighbors - 2 <= 3 ||
numNeighbors - 1 == 2 && numNeighbors - 2 <= 2 ||
numNeighbors - 1 == 1 && numNeighbors - 2 <= 1 ||
numNeighbors - 1 == 0 && numNeighbors - 0 == 8, Return[1]; If[cell == 1 && numNeighbors - 1 == numNeighbors - 2, Return[0],
Return[2]]];
If[cell == 0 && numNeighbors - 1 == numNeighbors - 2, Return[0]; If[cell == 0 && numNeighbors - 1 > numNeighbors - 2, Return[1],
Return[2]]];]
evaluateAll[A_] :=
Table[evaluateCell[A, i, j], {i, 1, Length[A]}, {j, 1, Length[A[[1]]]}];
makeFrames[A_, n_] := Map[ArrayPlot[#, Mesh -> True] &,
NestList[evaluateAll, A, n]]; animate[frames_] := ListAnimate[frames, 1,
ControlPlacement -> Top];
But running this code I don't have anything special (the colors should be black, grey and white, but after 2 steps I have a red cell, that maybe means that no rule is set for that cell with those neighbors).
Can you help me?
My purpose is also to show that the rule I have made is fractal.
I have tried to delete the IFs from the brackets, and now I don't have any red cells, but after 2 step the figures don't change (always the same cell in the same place, they don't interact). Where is the problem??
If[ cell == 2 && numNeighbors - 2 >= 4 ||
numNeighbors - 2 == 3 && numNeighbors - 1 <= 3 ||
numNeighbors - 2 == 2 && numNeighbors - 1 <= 2 ||
numNeighbors - 2 == 1 && numNeighbors - 1 <= 1 ||
numNeighbors - 2 == 0 && numNeighbors - 0 == 8, Return[2]];
If[ cell == 2 && numNeighbors - 2 == numNeighbors - 1, Return[0], Return[1]];
If[ cell == 1 && numNeighbors - 1 >= 4 ||
numNeighbors - 1 == 3 && numNeighbors - 2 <= 3 ||
numNeighbors - 1 == 2 && numNeighbors - 2 <= 2 ||
numNeighbors - 1 == 1 && numNeighbors - 2 <= 1 ||
numNeighbors - 1 == 0 && numNeighbors - 0 == 8, Return[1]];
If[ cell == 1 && numNeighbors - 1 == numNeighbors - 2, Return[0], Return[2]];
If[ cell == 0 && numNeighbors - 1 == numNeighbors - 2, Return[0]];
If[ cell == 0 && numNeighbors - 1 > numNeighbors - 2, Return[1], Return[2]]];
@amr: I had a look to the documentation, but there are only rule already done, I want to have my rule (the one that you can see in the excel file).
Few questions: 1)Should the center cell to be count in the number of neighbors? 2)Is there another way to specify a new rule? 3)Is there a way to discover if the rule that I have created is already implemented in Mathematica with an internal code? 4)May I use a BooleanFunction to do it faster?
Many Thanks
If
s inside otherIf
s afterReturn
(those which at the current formatting are not in the beginning of a line). ThoseIf
s will never be executed because of the precedingReturn
. I guess that's the source of your problem. Probably you wanted them to be outside the previousIf
. $\endgroup$