clf
clear

%build maze
%dimensions of maze
maxX=54;
maxY=36;
A=zeros(maxX,maxY);
%walls of maze
A(1,:)=1;
A(maxX,:)=1;
A(:,1)=1;
A(:,maxY)=1;
%lower right half of maze
A(10,2:5)=1;
A(10:13,5)=1;
A(13,5:9)=1;
A(13:19,9)=1;
A(15,9:13)=1;
A(9:15,13)=1;
A(9,9:13)=1;
A(19,7:15)=1;
A(19:31,7)=1;
A(23,7:11)=1;
A(19:27,15)=1;
A(27,15:19)=1;
A(27:32,19)=1;
A(32,17:21)=1;
A(31,5:7)=1;
A(40,2:11)=1;
A(31:40,11)=1;
A(31,11:13)=1;
A(35,11:13)=1;
A(49,2:24)=1;
A(39:49,24)=1;
A(39,16:24)=1;
A(39:45,16)=1;
A(45,16:20)=1;
A(50:53,30)=1;
%upper left half of maze
A(2:5,5)=1;
A(5,5:17)=1;
A(5:11,17)=1;
A(11,17:22)=1;
A(5:21,22)=1;
A(5,21:22)=1;
A(21,19:22)=1;
A(8,22:32)=1;
A(5:8,28)=1;
A(15,26:35)=1;
A(13:17,26)=1;
A(21,29:35)=1;
A(21:26,29)=1;
A(25,24:29)=1;
A(35,28:35)=1;
A(30:39,28)=1;
A(39,28:32)=1;
A(39:44,32)=1;
%entrance and exit to maze
A(8,1)=0;
A(maxX,maxY-3:maxY-1)=0;

%initial conditions
x=8;
y=1;
time=0;
timestep=1;
nstep=0;

%main loop
while (nstep<2001)
    nstep = nstep + 1;        

        xOld=x;
        yOld=y;
        timeNow=time
        
        A(x,y)=0.4;
        
        %Have the maze solving guy move in a random direction
        %This is really only important at the beginning because after this
        %the maze solving guy just follows the wall.
        r=rand();
        if 0<=r && r<=.25
            x=xOld-1;
            y=yOld;
        end
        if .25<=r && r<=.5
            x=xOld+1;
            y=yOld;
            
        end
        if .5<=r && r<=.75
            x=xOld;
            y=yOld-1;
           
        end
        if .75<=r && r<=1
           x=xOld;
           y=yOld+1;
        end
        
        %reflexive boundary conditions
        if x<1;
            x=xOld;
            continue
        end
        if x>maxX
            x=xOld;
            continue
        end
        if y<1
            y=yOld;
            continue
        end
        if y>maxY;
            y=yOld;
            continue
        end
        
        xR=x+1;
        xL=x-1;
        yU=y+1;
        yD=y-1;
        
        %ignore boundary conditions at the exit to maze
        if xR==maxX+1 && y==maxY-3;
            A(x,y)=0.4;
        end
        if xR==maxX+1 && y==maxY-1;
            A(x,y)=0.4;
        end
        
        
        if A(x,y)==0;
            %make sure that maze solving guy only goes where he is touching
            %a wall
            if (A(xR,y) > 0.5 || A(xL,y) > 0.5 || A(x,yU) > 0.5 || A(x,yD) > 0.5|| A(xR,yD) > 0.5 || A(xR,yU) > 0.5 || A(xL,yU) > 0.5 || A(xL,yD)>0.5)
                A(x,y)=0.4;
                time = time +1;

            else
                x=xOld;
                y=yOld;
            end
        else
            x=xOld;
            y=yOld;
            
        end
        
        fprintf('time= %4d x=%4d y=%4d',i,x,y);
        
        %pring results
        imagesc(A');
        set(gca,'YDir','normal');
        colormap(1-gray);
        axis equal;
        axis tight;
        drawnow
       
     if mod(nstep+9,10)==0
         timestep=timestep+1;
         saveas(gcf,['movie2_',sprintf('%03d',timestep),'.png']);
     end      
 end   