4
$\begingroup$

I want to modify Game of Life code so that the lattice can be seeded with one or two new life forms like:

glider[x_, y_] := {{x, y}, {x+1, y}, {x+2, y}, {x+2, y+1}, {x+1, y+2}}

snake[x_, y_] := {{x, y}, {x, y+1}, {x+1, y}, {x+2, y+1}, {x+3, y+1}, {x+3, y}}

I don't know how to modify my code to put above code! I'd be grateful if you could help.

Here is my code:

Life[n_, t_] := 
     Module[{initConfig, Moore, update, LiveConfigs, DieConfigs},

initConfig = Table[Random[Integer], {n}, {n}];

LiveConfigs = 
    Join[Map[Join[{0}, #] &, Permutations[{1, 1, 1, 0, 0, 0, 0, 0}]], 
    Map[Join[{1}, #] &, Permutations[{1, 1, 1, 0, 0, 0, 0, 0}]], 
    Map[Join[{1}, #] &, Permutations[{1, 1, 0, 0, 0, 0, 0, 0}]]];

DieConfigs = 
    Complement[Flatten[Map[Permutations, 
  Map[Join[Table[1, {#}], Table[0, {(9 - #)}]] &, Range[0, 9]]], 1], LiveConfigs];

Apply[(update[##] = 1) &, LiveConfigs, 1]; 
Apply[(update[##] = 0) &, DieConfigs, 1];


Moore[func__, lat_] := 
  MapThread[func, 
     Map[RotateRight[lat, #] &, {{0, 0}, {1, 0}, {0, -1}, {-1, 0}, {0, 1},
           {1, -1}, {-1, -1}, {-1, 1}, {1, 1}}], 2];

FixedPointList[Moore[update, #] &, initConfig, t]]
$\endgroup$
  • $\begingroup$ Hi ! Please, refer to the help centre and read more about proper code formatting practices. $\endgroup$ – Sektor Feb 7 '15 at 11:00
  • $\begingroup$ It's utterly strange to ignore how to modify your own code. $\endgroup$ – Dr. belisarius Feb 7 '15 at 12:26
  • $\begingroup$ @belisarius: I don't think "be ignorant of" is one of the meanings of the verb "ignore"... $\endgroup$ – Rahul Feb 7 '15 at 23:50
  • $\begingroup$ @Rahul One of these days I'll have to return to my English class ... $\endgroup$ – Dr. belisarius Feb 9 '15 at 4:41
5
$\begingroup$

Here's a way to seed the game of life with a glider, using the built-in game of life CellularAutomaton and the OPs glider (starting at 10,10).

glider[x_, y_] := {{x, y}, {x + 1, y}, {x + 2, y}, {x + 2, y + 1}, {x + 1, y + 2}};
gameOfLife = {224, {2, {{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}}, {1, 1}};
board = SparseArray[Flatten[{Thread[glider[10, 10] -> 1], {50, 50} -> 1}, 1]]; 
Dynamic[ArrayPlot[board = Last[CellularAutomaton[gameOfLife, board, {{0, 1}}]]]]

enter image description here

The glider starts at {10,10}.

$\endgroup$
  • $\begingroup$ @kuba -- thanks for the picture! $\endgroup$ – bill s Dec 9 '15 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.