function z=Final109Project(n,t)
%%
z=zeros(n,n);

surf(z); 

storage=zeros(1,1000);

pause(3)
endtime=t;

f=0;
factor=1;

for iter=1:endtime
    cut=round(rand()*n);
    coin=round(rand);
    type=round(rand*3);
    a=round(rand*n);
    factor=1;
    
  
    if type==1 %diagonal cut
        for i=1:n
            for j=1:n
                if coin==1
                    if i+j>(cut+cut)
                        z(i,j)=z(i,j)+factor;
                    elseif i+j<(cut+cut)
                        z(i,j)=z(i,j)-factor;
                    end
                else
                     if i+j<(cut+cut)
                        z(i,j)=z(i,j)+factor;
                     elseif i+j>(cut+cut)
                        z(i,j)=z(i,j)-factor;
                     end
                end
            end
        end
    
    
    
    elseif type==2 %cut along x axis
        if coin==1
            for i=1:a
                for j=1:n
                    z(i,j)=z(i,j)+factor*sind(i);
                end
            end
            for i=a+1:n
                for j=1:n
                    z(i,j)=z(i,j)-factor*sind(i);
                end
            end
        elseif coin==0
            for i=1:a
                for j=1:n
                    z(i,j)=z(i,j)-factor*sind(i);
                end
            end
            for i=a+1:n
                for j=1:n
                    z(i,j)=z(i,j)+factor*sind(i);
                end
            end
                    
        end
        
        
        elseif type==3%cut along y-axis
                if coin==1
                    for i=1:n
                        for j=1:a
                            z(i,j)=z(i,j)+factor*sind(i);
                        end
                    end
                    for i=1:n
                        for j=a+1:n
                            z(i,j)=z(i,j)-factor*sind(i);
                        end
                    end
                elseif coin==0
                    for i=1:n
                        for j=1:a
                            z(i,j)=z(i,j)-factor*sind(i);
                        end
                    end
                    for i=1:n
                        for j=a+1:n
                            z(i,j)=z(i,j)+factor*sind(i);
                        end
                    end
                end

    end
    %%
    pause(0.001);
    
    h=surf(z);
    set(h,'edgecolor','none');
    colormap('summer');
    
    axis([0 n 0 n 0 250]);
    storage(iter)=factor;
    view(iter/3,30)
    pause(0.001)
   
    %%
    
    if mod(iter+4,5)==0
        f=f+1;
       saveas(gcf, ['island_',num2str(f, '%03d'),'.png'])
        pause(0.01)
    end
end
f=0;   
endk=round(max(max(z)))+1;
for k=1:endk
    z=z-1;
    
    
    h=surf(z);
    set(h,'edgecolor','none');
    colormap('summer');
    
    axis([0 n 0 n 0 250]);
    storage(iter)=factor;
    view(iter/3,30)
    saveas(gcf, ['island2_',num2str(f, '%03d'),'.png'])
    f=f+1;
    pause(0.1)
end
end


    
    

