%animates a spaceship in the Game of Life
N = 20;
colormap(gray(2));
A=zeros(N);
A(1:3,1:3)=[0 1 0; 0 0 1; 1 1 1];
while(true)
B=A;
for r=1:N
    for c=1:N
        alive_neighbors = 0;
        for i=max(1,r-1):min(N,r+1)
            for j=max(1,c-1):min(N,c+1)
                if ~((i==r)&&(j==c))
                alive_neighbors = alive_neighbors + A(i,j);
                end;
            end;
        end;
        if (A(r,c)==1)
            if ((alive_neighbors <2 )||(alive_neighbors > 3))
                B(r,c)=0;
            end;
        else
            if (alive_neighbors == 3)
                B(r,c)=1;
            end;
        end;
    end;
end;
A = B;
imagesc(A)
axis equal;
pause(1/3);
end;