%This a simulation program for Life of Game with modified rules.

clear;

generations=100; %Total number of generations to be tested.
X=60; %Number of columns of the game data matrix.
Y=60; %Number of rows of the game data matrix.
pt=0; %pause time

for i=1:X;
   for j=1:Y;
      D=randn;
      if D<=0;
         C(i,j)=0;
      else;
         C(i,j)=1;
      end;
   end;
end;

%C=[  0     0     0     0     0     0     0     0     1     0
%     0     0     0     0     0     0     0     0     0     0
%     0     0     0     0     0     0     0     0     0     0
%     0     0     0     0     0     0     0     0     1     0
%     0     0     0     0     1     0     0     0     0     0
%     0     0     0     0     0     1     0     0     0     0
%     0     0     0     0     0     0     1     0     0     0
%     0     1     0     0     0     0     0     0     0     0
%     0     0     0     0     0     0     0     0     0     0
%     0     0     0     0     0     0     0     0     0     0];
  
  
  B=C;
  for j=1:(Y+1);
     B((X+1),j)=0;
  end;
         DIE=2;
         BORN=3;
         KEEP=4;
         OVERDIE=5;
  for loop=1:generations;
     A=B;
     count=0;
     for i=1:X;
        for j=1:Y;
           count=count+B(i,j);
        end;
     end;
     if count/(X*Y)>=8;
        DIE=2; %Below this number of neighbors, the cell dies of loliness
        BORN=4; %With this number of neighbors, the cell is borned.
        KEEP=5; %With this number of neighbors, the cell keep it's condition, alive or dead.
        OVERDIE=7; %Above this number of neighbors, the cell dies of (super)over-population.
      elseif count/(X*Y)<=2;
         DIE=1;
         BORN=3;
         KEEP=4;
         OVERDIE=6;
%      else;
%         DIE=2;
%         BORN=3;
%         KEEP=4;
%         OVERDIE=5;
     end;
        
n=0;
for i=1:X;
   for j=1:Y;
      if i==1;
         if j==1;
            n=A((i+(X-1)),(j+(Y-1)))+A((i+(X-1)),j)+A((i+(X-1)),(j+1))+A(i,(j+(Y-1)))+A(i,j)+A(i,(j+1))+A((i+1),(j+(Y-1)))+A((i+1),j)+A((i+1),(j+1));
         elseif j==Y;
            n=A((i+(X-1)),(j-1))+A((i+(X-1)),j)+A((i+(X-1)),(j-(Y-1)))+A(i,(j-1))+A(i,j)+A(i,(j-(Y-1)))+A((i+1),(j-1))+A((i+1),j)+A((i+1),(j-(Y-1)));
         else;
            n=A((i+(X-1)),(j-1))+A((i+(X-1)),j)+A((i+(X-1)),(j+1))+A(i,(j-1))+A(i,j)+A(i,(j+1))+A((i+1),(j-1))+A((i+1),j)+A((i+1),(j+1));
         end;
         if n<=DIE; B(i,j)=0; end;
         if n==BORN; B(i,j)=1; end;
         if n==KEEP; B(i,j)=A(i,j); end;
         if n>=OVERDIE; B(i,j)=0; end;
         E(i,j)=n;
      elseif i==X;
         if j==1;
            n=A((i-1),(j+(Y-1)))+A((i-1),j)+A((i-1),(j+1))+A(i,(j+(Y-1)))+A(i,j)+A(i,(j+1))+A((i-(X-1)),(j+(Y-1)))+A((i-(X-1)),j)+A((i-(X-1)),(j+1));
         elseif j==Y;
            n=A((i-1),(j-1))+A((i-1),j)+A((i-1),(j-(Y-1)))+A(i,(j-1))+A(i,j)+A(i,(j-(Y-1)))+A((i-(X-1)),(j-1))+A((i-(X-1)),j)+A((i-(X-1)),(j-(X-1)));
         else;
            n=A((i-1),(j-1))+A((i-1),j)+A((i-1),(j+1))+A(i,(j-1))+A(i,j)+A(i,(j+1))+A((i-(X-1)),(j-1))+A((i-(X-1)),j)+A((i-(X-1)),(j+1));
         end;
         if n<=DIE; B(i,j)=0; end;
         if n==BORN; B(i,j)=1; end;
         if n==KEEP; B(i,j)=A(i,j); end;
         if n>=OVERDIE; B(i,j)=0; end;
         E(i,j)=n;
      else;
         if j==1;
            n=A((i-1),(j+(Y-1)))+A((i-1),j)+A((i-1),(j+1))+A(i,(j+(Y-1)))+A(i,j)+A(i,(j+1))+A((i+1),(j+(Y-1)))+A((i+1),j)+A((i+1),(j+1));
         elseif j==Y;
            n=A((i-1),(j-1))+A((i-1),j)+A((i-1),(j-(Y-1)))+A(i,(j-1))+A(i,j)+A(i,(j-(Y-1)))+A((i+1),(j-1))+A((i+1),j)+A((i+1),(j-(Y-1)));
         else;
            n=A((i-1),(j-1))+A((i-1),j)+A((i-1),(j+1))+A(i,(j-1))+A(i,j)+A(i,(j+1))+A((i+1),(j-1))+A((i+1),j)+A((i+1),(j+1));
         end;
         if n<=DIE; B(i,j)=0; end;
         if n==BORN; B(i,j)=1; end;
         if n==KEEP; B(i,j)=A(i,j); end;
         if n>=OVERDIE; B(i,j)=0; end;     
         E(i,j)=n;
      end;
   end;
end;
data(loop)=count;
pause(pt);
pcolor(-B);
colormap(gray(2));


axis('ij');
axis('square');
end;

file=fopen('StableOscillator.txt','w');
for loop=1:generations;
   fprintf(file,'%d	 %d\r\n',loop, data(loop));  
end;
st=fclose(file);