%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 = ['art1IT',num2str(IT),'N',num2str(N),'.avi'];mov = avifile(moviename); end
imh = imagesc(cells,[0 1]);
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;
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);       
        set(imh,'cdata',cells)
        it=it+1;
        f=getframe(gca);
       	if makemovie==1; mov = addframe(mov,f); end
end
if makemovie==1; mov = close(mov); end