clf;
clear;

maxX =200;
maxY =200;

                                   % A(x,y)=0 ... cite is empty
                                   % A(x,y)=1 ... cite is filled
A = zeros(maxX, maxY);            %empty
x=round (maxX/2);                 %round it, incase the maxX or maxY is not an integer
y=round (maxY/2);                 %initial position at center

d=3;                              %starting direction 1 right 2 up 3 left 4 down 5 right......




image = image(cat(3,A,A,A));              %3 dimention...
set(image, 'erasemode', 'none')           %none — Do not erase the object when it is moved or destroyed. 
axis equal                                %Erase mode. Controls the technique MATLAB uses to draw and erase objects.
axis tight



n = 50000        % iterations

set(gcf,'Color',[0.6,0.5,0.9])  %lol just set the background to purple
set(gca,'YDir','normal')        %reverse y axis

f=0
 
for i=1:n
    
    if (A(x,y)==0)     %if the ant is on a white square
       d=mod(d-2,4)+1;  % turn right (90°)    ex:(4-2)/4, re=2, 2+1 =3
       A(x,y)=1;       % flip to black
    else               % if it's not white,
       d=mod(d,4)+1;    % turn left (90°) 
       A(x,y)=0;       % flip to white
    end

    
    if (d == 1)        %right
        x= x+1;
        y= y;
    end
    
    if (d == 2)        %up
        x=x;
        y=y+1;
    end
    
    if (d == 3)       %left
        x=x-1;
        y=y;
    end
    
    if (d == 4)       %down
        x=x;
        y=y-1;
    end
    
   
    
    
    
    if (x > maxX)       %no boundary!
    x = 1;              %same as in hw5
    end
    if (x < 1)
    x = maxX;
    end
    if (y > maxY)
        y = 1;
    end
    if (y < 1)
        y = maxY;
    end

       
    antColor=A; antColor(x,y)=1.0;   % the red square is the position of the ant
    
    set(image, 'cdata', cat(3,antColor,A,A) )   %color, 3,R,G,B
    

    drawnow

    
    title(sprintf('%d', i))        %count steps
    
    
        if mod(i+9,10) == 0  
        f=f+1;
%         surf(X,Y,T)
%         axis([0 L 0 L 0 100])
        saveas(gcf, ['ants',sprintf('%03d',f),'.png'])
        pause(0.01)
    end

    
    
    
    
    
    
    
end


title('n=50000')


