Game of Life

Luc Barthelet
Date: 2/16/98

Defines the size of the earth, and the original density of life on earth

\(boardSize\  = \ 50; 
density\  = 10/100; \)

Generate "earth" using the values defined above:

earth  =  
      Table[If[Random[] < density, 1, 0], {boardSize}, {boardSize}];

Define a function to display earth:

ShowEarth := Show[DensityGraphics[1 - earth, Axes -> False, Mesh -> False, 
              Frame -> False],Graphics[
              Line[boardSize {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}]]];

Here it is:

ShowEarth;

Defines some rules for evolution:

As a start we will just count how many neighbors any dot has.
we use 4-connectivity first.
This earth is a torus.

\(\(\(FourNeighbors\  := 
      earth + \((RotateRight\ /@\ earth)\)\  + \ 
        \((RotateLeft\ /@\ earth)\)\  + \ \((RotateRight[earth])\) + \ 
        \((RotateLeft[earth])\); \)
\)\)

\(\(\(EightNeighbors\  := 
      earth + \((RotateRight\ /@\ earth)\)\  + \ 
        \((RotateLeft\ /@\ earth)\)\  + \ 
        RotateRight\ /@\((RotateRight[earth])\) + \ 
        \((RotateRight[earth])\) + \ RotateLeft\ /@\((RotateRight[earth])\) + 
        \ RotateRight\ /@\((RotateLeft[earth])\) + \ 
        \((RotateLeft[earth])\) + \ RotateLeft\ /@\((RotateLeft[earth])\); 
    \)
\)\)

defines the survival rules:

nextGen  = 
       { {_, 0} -> 0, {_, 1} -> 0, {_, 2} -> 0, {_, 3} -> 1, {h_, 4} -> 
          h, {_, x_ /; x > 4} -> 0};

earth  = 
      (Transpose/@ Transpose[{ earth, FourNeighbors}])  /.  nextGen;

ShowEarth;

originalEarth  =  
      Table[If[Random[] < density, 1, 0], {boardSize}, {boardSize}];

\(max\  = \ 40; 
earth\  = \ originalEarth; 

    While[\ \(max--\) > 0\ , 
	ShowEarth; 
	
      newEarth\  = 
        \((Transpose/@\ Transpose[{\ earth, FourNeighbors}])\)\  /. \ 
          nextGen; 
	
      If[newEarth\  == \ earth, Break[], \ earth\  = \ newEarth]; 
	]; 

    ShowEarth; \)

density  =  10/100;

originalEarth  =  
      Table[If[Random[] < density, 1, 0], {boardSize}, {boardSize}];

\(max\  = \ 40; 
earth\  = \ originalEarth; 

    While[\ \(max--\) > 0\ , 
	ShowEarth; 
	
      newEarth\  = 
        \((Transpose/@\ Transpose[{\ earth, EightNeighbors}])\)\  /. \ 
          nextGen; 
	
      If[newEarth\  == \ earth, Break[], \ earth\  = \ newEarth]; 
	]; 

    ShowEarth; \)

\(\(originalEarth\  = \ Table[0, {boardSize}, {boardSize}]; 

    originalEarth[\([24, 24]\)] = 1; 
originalEarth[\([24, 26]\)] = 1; 


    originalEarth[\([25, 25]\)] = 1; 
originalEarth[\([25, 24]\)] = 1; 



    originalEarth[\([26, 25]\)] = 1; 
earth\  = \ originalEarth; 

    ShowEarth; 
\)\)

\(max\  = \ 40; 
earth\  = \ originalEarth; 

    While[\ \(max--\) > 0\ , 
	ShowEarth; 
	
      newEarth\  = 
        \((Transpose/@\ Transpose[{\ earth, EightNeighbors}])\)\  /. \ 
          nextGen; 
	
      If[newEarth\  == \ earth, Break[], \ earth\  = \ newEarth]; 
	]; 

    ShowEarth; \)

\(\(originalEarth\  = \ Table[0, {boardSize}, {boardSize}]; 

    originalEarth[\([24, 24]\)] = 1; 
originalEarth[\([24, 27]\)] = 1; 


    originalEarth[\([25, 24]\)] = 1; 
originalEarth[\([25, 25]\)] = 1; 

    originalEarth[\([25, 26]\)] = 1; 
originalEarth[\([25, 27]\)] = 1; 


    earth\  = \ originalEarth; 
ShowEarth; 
\)\)