clear all
close all
growthParticles = 5000;
maxX = 200;
maxY = 200;
f = 0;
growthNewParticlesPerFrame = 100;

A = zeros(maxX, maxY);
A(:,1)=1;
A(1,3:199)=1;

yBuffer = 2;
yStart = 1+yBuffer;

xBuffer = 2;
xStart = 1+xBuffer;
for i = 1:growthParticles
	
 	x  = 1+floor(maxX*rand); 

 	y  = yStart; 


    while 1
        xOld = x; 
        yOld = y;
        
        
		r = rand();
        
        if (r<=0.25)
            x=x+1;
        elseif (0.26<=r)&&(r<=0.50)
            x=x-1;
        elseif (0.51<=r)&&(r<=0.75)
            y=y+1;
        elseif (0.75 < r)
            y=y-1;
        end

             
        
        if (x > maxX)
      
            x=maxX;
        elseif (x< 1)
            x=1;
        end
        
        if (y>yStart)   
             y = 1; 
           
        end
        
        if (A(x,y)>0) 
            x=xOld;
            y=yOld;
            continue;
        end
        
        xR = x+1; 
        xL = x-1; 
        yU = y+1; 
        yD = y-1; 
        
       
        if (xL < 1)
            xL=maxX;
        elseif (xR> maxX)
            xR=1;
        end
        
        if (yU < 1)
            yU=maxY;
        elseif (yD > maxY)
            yD=1;
        end
        
 		
        if (A(xR,y) > 0 || A(xL,y) > 0 || A(x,yU) > 0 || A(x,yD) > 0)
          
            A(x,y) = 1; 
          
            if (y+yBuffer>yStart && y+yBuffer<maxY) 
                yStart = y+yBuffer;
            end
             if (x+xBuffer>xStart && x+xBuffer<maxX) 
                xStart = x+xBuffer;
            end
            fprintf('i= %5d x=%4d y=%4d yStart=%4d\n',i,x,y,yStart);
            break;
        end

    end
    
    if (i==growthParticles || mod(i,growthNewParticlesPerFrame)==0 || yStart+1==maxY) 
        f = f+1;
        imagesc(A');
        set(gca,'YDir','normal');
        colormap(1-gray);
        axis equal;
        axis tight;
        title ('moss grows in a moisture room');
        drawnow
%         saveas(gcf, ['growth',sprintf('%03d',f),'.png']);
    end

    if (yStart+1==maxY) 
        fprintf('Growth reached Y limit after only %d particles\n',i);
        break;
    end
    
end

destroyParticles = 8000; 
destroyNewParticlesPerFrame = 100;
fdie = f+1;
for j = 1:destroyParticles
    
    x  = 1+floor(maxX*rand); 
 	y  = yStart; 

    while 1
        xOld = x; 
        yOld = y;
        r = rand();
        
        if (r<=0.25)
            x=x+1;
        elseif (0.26<=r)&&(r<=0.50)
            x=x-1;
        elseif (0.51<=r)&&(r<=0.75)
            y=y+1;
        elseif (0.75 < r)
            y=y-1;
        end


        if (x > maxX)
      
            x=maxX;
        elseif (x< 1)
            x=1;
        end
        
        if (y>yStart)   
             y = 1; 
        end
        
        if (A(x,y)>0) 
            x=xOld;
            y=yOld;
            continue;
        end
       
        xR = x+1; 
        xL = x-1; 
        yD = y-1; 
        
        if (xL < 1)
            xL=maxX;
        elseif (xR> maxX)
            xR=1;
        end
        if (yU < 1)
            yU=maxY;
        elseif (yD > maxY)
            yD=1;
        end

         if (A(xR,y) > 0 || A(xL,y) > 0 || A(x,yD) > 0)
            A(x,yU) = 0;
            A(xL,y) = 0;
            A(xR,y) = 0;
            break;
        end
        
    end
    
    if (j==destroyParticles || mod(j,destroyNewParticlesPerFrame)==0)
        f = f+1;
        imagesc(A');
        set(gca,'YDir','normal');
        colormap(1-gray);
        axis equal;
        axis tight;
        title ('then die out when drought season is coming');
        drawnow
%         saveas(gcf, ['growth',sprintf('%03d',f),'.png']);
        
    end
end






	