A=ones(15);  %initializing our array

g=0;    %For movie

%Constructing the map (0's are walls)
A(:,1)=0 ;   %should be upper and bottom walls
A(:,15)=0;
A(1,1:7)=0;  %Left and Right
A(1,9:15)=0;
A(15,1:7)=0;
A(15,9:15)=0;
A(1:3,7)=0  ;%Top and bottom straight sections
A(1:3,9)=0;
A(13:15,7)=0;
A(13:15,9)=0;
A(3,3:5)=0;  %Corner sections
A(4:5,3)=0;
A(11:13,3)=0;
A(13,4:5)=0;
A(3,11:13)=0;
A(4:5,13)=0;
A(11:13,13)=0;
A(13,11:12)=0;
A(7,3:5)=0;  %reverse corners
A(5:6,5)=0;
A(9,3:5)=0;
A(10:11,5)=0;
A(5:7,11)=0;
A(7,12:13)=0;
A(9,11:13)=0;
A(10:11,11)=0;
A(11,7:9)=0;    %4 center blocks
A(9,7:9)=0;
A(7,7:9)=0;
A(5,7:9)=0;

time=0;     %time counter, want to update after each iteration

%Want to define starting locations for monsters and men
%Start position of 'Guy'
xG = 8;
yG = 4;
%Start position of 'MonsterA'
xA = 12;
yA = 8;
%Start position of 'MonsterB'
xB = 8;
yB = 12;
%Start position of 'MonsterC'
xC = 4;
yC = 8;

%Initializes locations
A(xG,yG)=.8;
A(xA,yA)=.4;
A(xB,yB)=.3;
A(xC,yC)=.2;

while 1
    %Want continually update position of 4 characters, this stores
    %positions, allows us to reassign xG,A-C
    xOldG = xG; %Color: A(xG,yG)=.8
    yOldG = yG;
    
    xOldA = xA; %Color: A(xA,yZ)=.4
    yOldA = yA;
    
    xOldB = xB; %Color: A(xB,yB)=.3
    yOldB = yB;
    
    xOldC = xC; %Color: A(xC,yC)=.2
    yOldC = yC;
    
    %After updating positions, update time. Will use this to provide a
    %counter in my plot
    
    time=time+1;
    f=time
    
    
    
    %Monsters move randomly with situational intelligence
   smart=rand() %stops the monsters from "sticking" to G
   
    if (xOldA-xOldG)<3 && (xOldA-xOldG)>0 && yOldA-yOldG == 0 && smart>=.20
        xA=xOldA-1;
    elseif (xOldG-xOldA)<3 && (xOldG-xOldA)>0 && yOldA-yOldG == 0 && smart>=.20
        xA=xOldA+1;
    elseif (yOldA-yOldG)<3 && (yOldA-yOldG)>0 && xOldA-xOldG == 0 && smart>=.20
        yA=yOldA-1;
    elseif (yOldG-yOldA)<3 && (yOldG-yOldA)>0 && xOldA-xOldG == 0 && smart>=.20
        yA=yOldA+1;
    else
        r=rand();
        if r<=.25
            xA=xOldA-1;
        elseif r>.25 && r<=.50
            xA=xOldA+1;
        elseif r>.50 && r<=.75
            yA=yOldA-1;
        else yA=yOldA+1;
        end
    end
    
    if (xOldB-xOldG)<3 && (xOldB-xOldG)>0 && yOldB-yOldG == 0 && smart>=.20
        xB=xOldB-1;
    elseif (xOldG-xOldB) < 3 && (xOldG-xOldB)>0 && yOldB-yOldG == 0 && smart>=.20
        xB=xOldB+1;
    elseif (yOldB-yOldG) < 3 && (yOldB-yOldG)>0 && xOldB-xOldG == 0 && smart>=.20
        yB=yOldB-1;
    elseif (yOldG-yOldB) < 3 && (yOldG-yOldB)>0 && xOldB-xOldG == 0 && smart>=.20
        yB=yOldB+1;
    else
        r=rand();
        if r<=.25
            xB=xOldB-1;
        elseif r>.25 && r<=.50
            xB=xOldB+1;
        elseif r>.50 && r<=.75
            yB=yOldB-1;
        else yB=yOldB+1;
        end
    end
        
    if (xOldC-xOldG) < 3 && (xOldC-xOldG)>0 && yOldC-yOldG == 0 && smart>=.20
        xC=xOldC-1;
    elseif (xOldG-xOldC) < 3 && (xOldG-xOldC)>0 && yOldC-yOldG == 0 && smart>=.20
        xC=xOldC+1;
    elseif (yOldC-yOldG) < 3 && (yOldC-yOldG)>0 && xOldC-xOldG == 0 && smart>=.20
        yC=yOldC-1;
    elseif (yOldG-yOldC) < 3 && (yOldG-yOldC)>0 && xOldC-xOldG == 0 && smart>=.20
        yC=yOldC+1;
    else
        r=rand();
        if r<=.25
            xC=xOldC-1;
        elseif r>.25 && r<=.50
            xC=xOldC+1;
        elseif r>.50 && r<=.75
            yC=yOldC-1;
        else yC=yOldC+1;
        end
    end
    
    
      %Need periodic coundary conditions to account for the 'tube' at y=8
    if xA<1
       xA=15;
    elseif xA>15
        xA=1;
    end
    
     if xB<1
        xB=15;
    elseif xB>15
        xB=1;
     end
     
     if xC<1
        xC=15;
    elseif xC>15
        xC=1;
     end
    
     if xG<1
        xG=15;
        xOldG=15;
    elseif xG>15
        xG=1;
        xOldG=1;
    end
    
    %Now our monsters walk randomly, but what if they hit each other or a
    %wall?
    
    if A(xA,yA)==0 || A(xA,yA)==.3 || A(xA,yA)==.2
        xA=xOldA;
        yA=yOldA;
        A(xOldA,yOldA)= .4;  %Resets to original position if collision
    else A(xOldA,yOldA)= 1;
         A(xA,yA)=.4;        %If there is no collision, change value of new cell and change back value of old cell
    end
    
    
    if A(xB,yB)==0 || A(xB,yB)==.4 || A(xA,yA)==.2
        xB=xOldB;
        yB=yOldB;
        A(xOldB,yOldB)= .3 ; 
    else A(xOldB,yOldB)= 1;
         A(xB,yB)=.3;       
    end
    
    
    if A(xC,yC)==0 || A(xC,yC)==.3 || A(xC,yC)==.4
        xC=xOldC;
        yC=yOldC;
        A(xOldC,yOldC)= .2;  
    else A(xOldC,yOldC)= 1;
         A(xC,yC)=.2;        
    end
    
    %Now for the tricky part; making our G "intelligent"
    
    %If a monster is within 2 spaces of G either right, left, above or below
    %Also check one space diagonally in each to direction
    
   
    
    if (xOldA-xOldG)<3 && (xOldA-xOldG)>0 && yOldA-yOldG==0 || (xOldB-xOldG)<3 && (xOldB-xOldG)>0 && yOldB-yOldG==0 || (xOldC-xOldG)<3 && (xOldC-xOldG)>0 && yOldC-yOldG==0
        while 1
            r=rand();
            if r<=.33
                xG=xOldG-1;
            elseif r>.33 && r<=.66
                yG=yOldG+1;
            else yG=yOldG-1;
            end
            if A(xG,yG)==0
                xG=xOldG;
                yG=yOldG;
                A(xOldG,yOldG)=.8;
            else
                break
            end
        end
    elseif (xOldG-xOldA)<3 && (xOldG-xOldA)>0 && yOldA-yOldG==0 || (xOldG-xOldB)<3 && (xOldG-xOldB)>0 && yOldB-yOldG==0 || (xOldG-xOldC)<3 && (xOldG-xOldC)>0 && yOldC-yOldG==0
        while 1
            r=rand();
            if r<=.33
                xG=xOldG+1;
            elseif r>.33 && r<=.66
                yG=yOldG+1;
            else yG=yOldG-1;
            end
            if A(xG,yG)==0
                xG=xOldG;
                yG=yOldG;
                A(xOldG,yOldG)=.8;
            else
                break
            end
        end
    elseif (yOldA-yOldG)<3 && (yOldA-yOldG)>0 && xOldA-xOldG==0 || (yOldB-yOldG)<3 && (yOldB-yOldG)>0 && xOldB-xOldG==0 || (yOldC-yOldG)<3 && (yOldC-yOldG)>0 && xOldC-xOldG==0
        while 1
            r=rand();
            if r<=.33
                yG=yOldG-1;
            elseif r>.33 && r<=.66
                xG=xOldG+1;
            else xG=xOldG-1;
            end
            if A(xG,yG)==0
                xG=xOldG;
                yG=yOldG;
                A(xOldG,yOldG)=.8;
            else
                break
            end
        end
    elseif (yOldG-yOldA)<3 && (yOldG-yOldA)>0 && xOldA-xOldG==0 || (yOldG-yOldB)<3 && (yOldG-yOldB)>0 && xOldB-xOldG==0 || (yOldG-yOldC)<3 && (yOldG-yOldC)>0 && xOldC-xOldG==0
        while 1
            r=rand();
            if r<=.33
                yG=yOldG+1;
            elseif r>.33 && r<=.66
                xG=xOldG+1;
            else xG=xOldG-1;
            end
            if A(xG,yG)==0
                xG=xOldG;
                yG=yOldG;
                A(xOldG,yOldG)=.8;
            else
                break
            end
        end
    elseif (xOldA-xOldG)==1 && yOldA-yOldG==1 || (xOldB-xOldG)==1 && yOldB-yOldG==1 || (xOldC-xOldG)==1 && yOldC-yOldG==1
        while 1
            r=rand();
            if r<=.5
                xG=xOldG-1;
            else yG=yOldG-1;
            end
            if A(xG,yG)==0
                xG=xOldG;
                yG=yOldG;
                A(xOldG,yOldG)=.8;
            else
                break
            end
        end
    elseif (xOldG-xOldA)==1 && yOldA-yOldG==1 || (xOldG-xOldB)==1 && yOldB-yOldG==1 || (xOldG-xOldC)==1 && yOldC-yOldG==1
        while 1
            r=rand();
            if r<=.5
                xG=xOldG+1;
            else yG=yOldG-1;
            end
            if A(xG,yG)==0
                xG=xOldG;
                yG=yOldG;
                A(xOldG,yOldG)=.8;
            else
                break
            end
        end
    elseif (xOldG-xOldA)==1 && yOldG-yOldA==1 || (xOldG-xOldB)==1 && yOldG-yOldB==1 || (xOldG-xOldC)==1 && yOldG-yOldC==1
        while 1
            r=rand();
            if r<=.5
                xG=xOldG+1;
            else yG=yOldG+1;
            end
            if A(xG,yG)==0
                xG=xOldG;
                yG=yOldG;
                A(xOldG,yOldG)=.8;
            else
                break
            end
        end
    elseif (xOldA-xOldG)==1 && yOldG-yOldA==1 || (xOldB-xOldG)==1 && yOldG-yOldB==1 || (xOldC-xOldG)==1 && yOldG-yOldC==1
        while 1
            r=rand();
            if r<=.5
                xG=xOldG-1;
            else yG=yOldG+1;
            end
            if A(xG,yG)==0
                xG=xOldG;
                yG=yOldG;
                A(xOldG,yOldG)=.8;
            else
                break
            end
        end
    else
        r=rand();
        if r<=.25
            xG=xOldG+1;
        elseif r>.25 && r<=.5
            xG=xOldG-1;
        elseif r>.5 && r<=.75
            yG=yOldG+1;
        else
            yG=yOldG-1;
        end
    end

    if xG<1
        xG=15;
    elseif xG>15
        xG=1;
    end
    
    %Update position of our daring hero!
    if A(xG,yG)==0
        xG=xOldG;
        yG=yOldG;
        A(xOldG,yOldG)=.8;
    else
        A(xOldG,yOldG)=1;
        A(xG,yG)=.8;
    end
    
    %This should fix a bug where if the two monsters are next to each
    %other, the monster will update it's position to G's position
    %first, but not detect a collision since G hasn't moved yet. Then, when
    %G moves away, it reassigns its old position to a white "non wall"
    %block (=1) and the monster is effectively removed from the simulation.
    
     A(xA,yA)=.4;
     A(xB,yB)=.3;
     A(xC,yC)=.2;
    
    %After all positions have been recorded if G particle is in same
    %position as a monster, break
    
    if xG == xA && yG == yA
        break;
    end
    
    if xG == xB && yG == yB
        break;
    end
    
    if xG == xC && yG == yC
        break;
    end
    
    %These next three commands fix a bug i ran into. If the two particles
    %both moved into each other, they would switch places and it would'nt
    %register as a collision.
    
    if xG == xOldA && yG == yOldA && xA == xOldG && yA == yOldG
        break;
    end
    
    if xG == xOldB && yG == yOldB && xB == xOldG && yB == yOldG
        break;
    end
    
    if xG == xOldC && yG == yOldC && xC == xOldG && yC == yOldG
        break;
    end
    
   % if mod(g,1)==0
   %     g=g+1;
   %     imagesc(A');
   %     set(gca,'YDir','normal');
   %     colormap(hot);
   %     str='Time Survived=';
   %     title (['Monsters and Man  |  ' str num2str(f)]);
   %     axis square;
   %     drawnow;
   %     saveas(gcf, ['pacattack',sprintf('%03d',f),'.png'])
   %     pause(0.03)
   % end
    
    
    imagesc(A');        
    set(gca,'YDir','normal');
    colormap(hot);
    str='Time Survived=';
    title (['Monsters and Man  |  ' str num2str(f)]);
    axis equal;
    axis square;
    axis tight;
    drawnow
    pause(.05)
end