%My game of art
clf
clear all
%number of iterations and case scenario
IT=40; N=2; m=IT*2+4;  makemovie=0;
%initialize the arrays
cells = zeros(m,m,IT+1);
cells(round(m/2),round(m/2),1) = 1;
nely = size(cells,1);
nelx = size(cells,2);
sum = zeros(nely,nelx);
%movie and image
if makemovie==1; moviename = ['art3IT',num2str(IT),'N',num2str(N),'.avi'];mov = avifile(moviename); end
surfl(cells(:,:,1)*2); hold on; view(3)
colormap(copper);
set(gcf, 'WindowButtonDownFcn','stop=1;')
stop = 0;
axis equal; axis tight; axis off
%index definition
x = 2:nelx-1;
y = 2:nely-1;
it=1;
while (stop==0 & it<IT)
        %nearest neighbor sum
        sum(x,y) = cells(x,y-1,it) + cells(x,y+1,it) + cells(x-1, y,it) + cells(x+1,y,it) + cells(x-1,y-1,it) + cells(x-1,y+1,it) + cells(x+1,y-1,it) + cells(x+1,y+1,it);
        % The CA rule
        cells(:,:,it+1) = (cells(:,:,it)==0) & (sum==1 | sum==N); 
        surfl(cells(:,:,it+1)*(IT-it+1)*2);
        axis equal; axis tight; axis off
        it=it+1;
        f=getframe(gca);
       	if makemovie==1; mov = addframe(mov,f); end
end
if makemovie==1; mov = close(mov); end