function abm_life_game gr_sz = 120; % grid size iterations = 100; M = moviein( iterations ); % define initial state x = round(rand(gr_sz,gr_sz)); for it = 1:iterations for i = 1:gr_sz for j = 1:gr_sz if ( i == 1 ) & ( j == 1 ) bdry = x(i,j+1) + x(i+1,j) + 2; elseif ( i == 1 ) & ( j == gr_sz ) bdry = x(i,j-1) + x(i+1,j) + 2; elseif ( i == gr_sz ) & ( j == 1 ) bdry = x(i,j+1) + x(i-1,j) + 2; elseif ( i == gr_sz ) & ( j == gr_sz ) bdry = x(i,j-1) + x(i-1,j) + 2; elseif ( i == 1 ) bdry = x(i,j-1) + x(i,j+1) + x(i+1,j) + 1; elseif ( i == gr_sz ) bdry = x(i,j-1) + x(i,j+1) + x(i-1,j) + 1; elseif ( j == 1 ) bdry = x(i,j+1) + x(i-1,j) + x(i+1,j) + 1; elseif ( j == gr_sz ) bdry = x(i,j-1) + x(i-1,j) + x(i+1,j) + 1; else bdry = x(i,j-1) + x(i,j+1) + x(i-1,j) + x(i+1,j); end if ( bdry == 3 ) y(i,j) = 1; elseif ( bdry == 2 ) & ( x(i,j) == 1 ) y(i,j) = 1; else y(i,j) = 0; end end end pcolor( 1-x ); % surf( x ); view(0,90); colormap bone; % gray bone; shading flat; % flat faceted interp M( : , it ) = getframe; x = y; end % pcolor does not plot the last row for some reason % so we copy the last row twice: % x(:,gr_sz+1) = x(:,gr_sz); %axis([ 1 gr_sz 1 iterations ]); %movie(M,3,40); % Play 3 times at 40 fps. %map=colormap; %mpgwrite(M, map, 'filename.mpg')