%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=rand; if D<=0.5; C(i,j)=0; else; C(i,j)=1; end; end; end; %for j=1:Y; % D=rand; % if D<=0.5; % C(1,j)=0; % else; % C(1,j)=1; % end; %end; %cutpoint=D*Y; %cut=cutpoint-rem(cutpoint,1); %if cut<=0; cut=Y/2; %end; %CSUM=0; %for i=2:X; % for j=1:Y; % if j+cut<=Y; % C(i,j)=C((i-1),(j+cut)); % else; % C(i,j)=C((i-1),(Y-j+1)); % end; % CSUM=CSUM+C(i,j); % end; % cutpoint=CSUM; % cut=cutpoint-rem(cutpoint,1); % if cut<=0; cut=Y/2; % end; % CSUM=0; %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=3; BORN=4; KEEP=5; OVERDIE=6; 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)>=0.625; DIE=3; %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=6; %Above this number of neighbors, the cell dies of (super)over-population. elseif count/(X*Y)<=0.015; DIE=1; BORN=2; KEEP=3; OVERDIE=8; % 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(-E); colormap(hot(9)); axis('ij'); axis('square'); end; file=fopen('PopulationControl.txt','w'); for loop=1:generations; fprintf(file,'%d %d\r\n',loop, data(loop)); end; st=fclose(file);