%My game of art
clf
clear all
%number of iterations and case scenario
IT=125; N=2; m=IT*2+4;  makemovie=0;
%initialize the arrays
cells = zeros(m,m);
cells(round(m/2),round(m/2)) = 1;
[nely,nelx] = size(cells);
sum=zeros(nely,nelx);
%movie and image
if makemovie==1; moviename = ['art2IT',num2str(IT),'N',num2str(N),'.avi'];mov = avifile(moviename); end
chigh = max(IT, 20*IT);
imh = imagesc(cells,[0 chigh]);
colormap(copper);
set(imh, 'erasemode', 'none')
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;
tcells=cells;
while (stop==0 & it<IT)
        %nearest neighbor sum
        sum(x,y) = cells(x,y-1) + cells(x,y+1) + cells(x-1, y) + cells(x+1,y) + cells(x-1,y-1) + cells(x-1,y+1) + cells(x+1,y-1) + cells(x+1,y+1);
        % The CA rule
        cells = (cells==0) & (sum==1 | sum==N);
        tcells = tcells + cells*it;
        set(imh,'cdata',tcells);
		it=it+1;
        f=getframe(gca);
       	if makemovie==1; mov = addframe(mov,f); end
end
%figure
imagesc(tcells)
colormap(copper);
axis equal; axis tight; axis off
f=getframe(gca);
if makemovie==1; mov = addframe(mov,f); end
if makemovie==1; mov = close(mov); end