% Lagton's ant model
clf
clear all
%size of central lattice
nelx=100; nely=100; x=50; y=50; direction='n';
makemovie=0;
%initialize the arrays
cells = 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
it=0;
while (stop==0 & it<15000)
    if cells(y,x)
        switch direction
            case 'n'
                x=x-1; newdirection='w';
            case 's'
                x=x+1; newdirection='e';
            case 'e'
                y=y-1; newdirection='n';
            case 'w'
                y=y+1; newdirection='s';
        end
    else
        switch direction
            case 'n'
                x=x+1; newdirection='e';
            case 's'
                x=x-1; newdirection='w';
            case 'e'
                y=y+1; newdirection='s';
            case 'w'
                y=y-1; newdirection='n';
        end
    end
    direction=newdirection;
    cells(y,x)=mod(cells(y,x)+1,2);
    set(imh,'cdata',-cells);
    f=getframe(gca);
    it=it+1;
    %if it==6900 | it==10431 | it==12000
    %    pause
    %end
    %title(['Ant model (It=',num2str(it),')'])
   	if makemovie==1;mov = addframe(mov,f);end
end
if makemovie==1;mov = close(mov);end