% Conway's game of life % A. Tovar - 2003 clf clear all %size of central lattice citylimit=30; makemovie=0; %initialize the arrays cells = round(rand(citylimit,citylimit)); cells = [zeros(size(cells,1),1*size(cells,2)),cells,zeros(size(cells,1),1*size(cells,2))]; cells = [zeros(1*size(cells,1),size(cells,2));cells;zeros(1*size(cells,1),size(cells,2))]; [nely,nelx] = size(cells); sum=zeros(nely,nelx); %movie and image if makemovie==1;mov = avifile('mynewgameoflife.avi');end imh = imagesc(-cells,[-1 0]); colormap(gray); set(imh, 'erasemode', 'none') set(gcf, 'WindowButtonDownFcn','stop=1;') stop=0; axis equal axis tight axis off %index definition x = 1:nelx; y = 1:nely; it=0; while (stop==0 && it<5000) %nearest neighbor sum sum(mod(x,nelx)+1,mod(y,nely)+1) = cells(mod(x,nelx)+1,mod(y-1,nely)+1) + cells(mod(x,nelx)+1,mod(y+1,nely)+1) + ... cells(mod(x-1,nelx)+1,mod(y,nely)+1) + cells(mod(x+1,nelx)+1,mod(y,nely)+1) + ... cells(mod(x-1,nelx)+1,mod(y-1,nely)+1) + cells(mod(x-1,nelx)+1,mod(y+1,nely)+1) + ... cells(mod(x+1,nelx)+1,mod(y-1,nely)+1) + cells(mod(x+1,nelx)+1,mod(y+1,nely)+1); % The CA rule cells = (sum==3) | (sum==2 & cells); %draw the new image set(imh,'cdata',-cells) it=it+1; f=getframe(gca); %title(['Game of Life (It=',num2str(it),')']) if makemovie==1;mov = addframe(mov,f);end end if makemovie==1;mov = close(mov);end