clf
clear

maxX=24;
maxY=maxX;

A=zeros(maxX,maxY);

%Sets width of playing field
A(1:1+(maxX/4),1:maxY)=6;
A(maxX-(maxX/4):maxX,1:maxY)=6;
A((maxX/4)+2,maxY-4)=6;
A(maxX-((maxX/4)+1),maxY-4)=6;

n=1000; %Number of iterations
h=0; %For saving gif

L=0.10; %Probability of object going LEFT
R=0.20; %Probability of object going RIGHT

for i=1:n
    y=maxY-4;
    x=maxX/2;
    c=rand();
    xnew=x;
    ynew=y;
    if c<=0.25
        for j=1:maxY %Reverse L shape
            b=rand();
            if b<=L
                if A(xnew-1,ynew)>0
                   ynew=y-1;
                elseif A(xnew-1,ynew+1)>0
                   ynew=y-1;
                elseif ynew==1 
                    hold on
                    break
                elseif A(xnew-1,ynew-1)>0
                   ynew=y-1;
                else
                xnew=x-1; %moves left
                ynew=y-1;
                end
            elseif b<=R
                if A(xnew+3,ynew)>0
                   ynew=y-1;
                elseif ynew==1 
                    hold on
                    break
                elseif A(xnew+3,ynew-1)>0
                   ynew=y-1;
                else
                    xnew=x+1; %moves right
                    ynew=y-1;
                end   
            else
                ynew=y-1;
            end
           

            A(xnew:xnew+2,ynew)=1;
            A(xnew,ynew+1)=1;
           
            
            if ynew==1 
                hold on
                break
            end

            %Check if anything below if yes 
            if A(xnew,ynew-1)>0
                hold on 
                break
            end
            if A(xnew+1,ynew-1)>0
                hold on 
                break
            end
            if A(xnew+2,ynew-1)>0
                hold on 
                break
            end

            
            
            imagesc(1:maxX,1:maxY,A');
            set(gca,'YDir','normal');
            colormap(jet);
            axis tight;
            drawnow; h=h+1;
            saveas (gcf, ['JEFF', sprintf('%03d',h),'.png'])
             
            
            for k = 2:maxY;
            A(xnew,ynew+1)=0;
            A(xnew:xnew+2,ynew)=0;
            end
            
            x=xnew;
            y=ynew;
           
        end
    elseif c<=0.5
        for k=1:maxY %Square shape
            d=rand();
            if d<=L
               if A(xnew-1,ynew)>0
                   ynew=y-1;
                elseif A(xnew-1,ynew+1)>0
                   ynew=y-1;
                elseif ynew==1 
                    hold on
                    break
                elseif A(xnew-1,ynew-1)>0
                   ynew=y-1;
                else
                xnew=x-1; %moves left
                ynew=y-1;
                end
            elseif d<=R
                if A(xnew+2,ynew)>0
                   ynew=y-1;
                elseif A(xnew+2,ynew+1)>0
                   ynew=y-1;
                elseif ynew==1 
                    hold on
                    break
                elseif A(xnew+2,ynew-1)>0
                   ynew=y-1;
                else
                    xnew=x+1; %moves right
                    ynew=y-1;
                end   
            else
                ynew=y-1;
            end
           

            A(xnew:xnew+1,ynew:ynew+1)=2;

                      
            if ynew==1 
                hold on
                break
            end

            %Check if anything below if yes break
            if A(xnew,ynew-1)>0
                hold on 
                break
            end
            if A(xnew+1,ynew-1)>0
                hold on 
                break
            end
            
            
            imagesc(1:maxX,1:maxY,A');
            set(gca,'YDir','normal');
            colormap(jet);
            axis tight;
            drawnow; h=h+1;
            saveas (gcf, ['JEFF', sprintf('%03d',h),'.png'])
             
            
            for k = 2:maxY;
            A(xnew:xnew+1,ynew:ynew+1)=0;
            end
            
            x=xnew;
            y=ynew;
           
        end
    elseif c<=0.75
        for l=1:maxY %Stick shape
            e=rand();
            if e<=L
                if A(xnew-1,ynew)>0
                   ynew=y-1;
                elseif A(xnew-1,ynew+1)>0
                   ynew=y-1;
                elseif A(xnew-1,ynew+2)>0
                   ynew=y-1;
                elseif A(xnew-1,ynew+3)>0
                   ynew=y-1;
                elseif ynew==1 
                    hold on
                    break
                elseif A(xnew-1,ynew-1)>0
                   ynew=y-1;
                else
                    xnew=x-1; %moves left
                    ynew=y-1;
                end
            elseif e<=R
                if A(xnew+1,ynew)>0
                   ynew=y-1;
                elseif A(xnew+1,ynew+1)>0
                   ynew=y-1;
                elseif A(xnew+1,ynew+2)>0
                   ynew=y-1;
                elseif A(xnew+1,ynew+3)>0
                   ynew=y-1;
                elseif ynew==1 
                    hold on
                    break
                elseif A(xnew+1,ynew-1)>0
                   ynew=y-1;
                else
                    xnew=x+1; %moves right
                    ynew=y-1;
                end   
            else
                ynew=y-1;
            end
           

            A(xnew,ynew:ynew+3)=3;

                      
            if ynew==1 
                hold on
                break
            end

            %Check if anything below if yes break
            if A(xnew,ynew-1)>0
                hold on 
                break
            end
            
            
            imagesc(1:maxX,1:maxY,A');
            set(gca,'YDir','normal');
            colormap(jet);
            axis tight;
            drawnow; h=h+1;
            saveas (gcf, ['JEFF', sprintf('%03d',h),'.png'])
             
            
            for k = 2:maxY;
            A(xnew,ynew:ynew+3)=0;
            end
            
            x=xnew;
            y=ynew;
           
        end
    else
        for m=1:maxY %L shape
            f=rand();
            if f<=L
                if A(xnew-1,ynew)>0
                   ynew=y-1;
                elseif ynew==1 
                    hold on
                    break
                elseif A(xnew-1,ynew-1)>0
                   ynew=y-1;
                else
                xnew=x-1; %moves left
                ynew=y-1;
                end
            elseif f<=R
                if A(xnew+3,ynew)>0
                   ynew=y-1;
                elseif A(xnew+3,ynew+1)>0
                   ynew=y-1;
                elseif ynew==1 
                    hold on
                    break
                elseif A(xnew+3,ynew-1)>0
                   ynew=y-1;
                else
                    xnew=x+1; %moves right
                    ynew=y-1;
                end   
            else
                ynew=y-1;
            end
           

            A(xnew:xnew+2,ynew)=4;
            A(xnew+2,ynew+1)=4;
           
            
            if ynew==1 
                hold on
                break
            end

            %Check if anything below if yes 
            if A(xnew,ynew-1)>0
                hold on 
                break
            end
            if A(xnew+1,ynew-1)>0
                hold on 
                break
            end
            if A(xnew+2,ynew-1)>0
                hold on 
                break
            end

            
            
            imagesc(1:maxX,1:maxY,A');
            set(gca,'YDir','normal');
            colormap(jet);
            axis tight;
            drawnow; h=h+1;
            saveas (gcf, ['JEFF', sprintf('%03d',h),'.png'])
             
            
            for k = 2:maxY;
            A(xnew+2,ynew+1)=0;
            A(xnew:xnew+2,ynew)=0;
            end
            
            x=xnew;
            y=ynew;
           
        end
    end
 
    %If top row has shape GAME OVER
        for z=((maxX/2)-1):((maxX/2)+1)
            if A(z,maxY-5)>0
                A(4:7,17)=5;
                A(4:3:7,16)=5;
                A(4,15)=5;
                A(6,14)=5;
                A(4:3:7,12:14)=5;
                A(4:7,12)=5;

                A(9:2:11,12:17)=5;
                A(9:11,14:3:17)=5;

                A(13:4:17,12:17)=5;
                A(14:2:16,16)=5;
                A(15,15)=5;

                A(19:21,12:5:17)=5;
                A(19,12:17)=5;
                A(20,14)=5;

                A(4:6,3:5:8)=5;
                A(4:2:6,3:8)=5;

                A(8:4:12,5:8)=5;
                A(9:2:11,4)=5;
                A(10,3)=5;

                A(14:16,3:5:8)=5;
                A(14,3:8)=5;
                A(15,5)=5;

                A(18,3:8)=5;
                A(18:21,5:3:8)=5;
                A(21,5:8)=5;
                A(20,4)=5;
                A(21,3)=5;
                hold on 
                break
            end
        end
   


end
        