%Cicada survival simulation
%Derek Jung
%EPS 109 final project matlab simulation file

clear all;
%make a matrix that would contain all the system
xmax=100;
ymax=50;
soilmax=80;
A=zeros(xmax,ymax);
iter_cicada=30; %number of cicada
iter_pred1=5;   %number of pred 1 (random walking predator)
iter_pred2=5;   %number of pred 2 (horizontally moving predator)
iter_pred3=5;   %number of pred 3 (predators that stay still)
cicadacolor=1;
pred1color=2;
pred2color=3;
pred3color=4;
soilcolor=5;
aircolor=6;
freecicadacolor=7;
map=[255,255,255;0,255,0;0,0,255;255,0,0;106,66,42;170,85,0;0,255,255]/255;
count=0;
% Color coding: cicada=1 predator=2~4 soil=5 atmosphere=6

A(1:soilmax, 1:ymax)=soilcolor;
A(soilmax+1:xmax, 1:ymax)=aircolor;
%place cicadas until there are iter_cicada amount of cicada on the field

survived_cicada_count=0;
cicadainfield=iter_cicada;
while iter_cicada>0
      cicadax=ceil(rand()*soilmax/4);
      cicaday=ceil(rand()*ymax);
      if A(cicadax,cicaday)>4
          A(cicadax,cicaday)=cicadacolor;
          iter_cicada=iter_cicada-1;
      end
end

%place predator type 1 code 2

while iter_pred1>0
    pred1x=ceil(rand()*soilmax/4+soilmax/4+5);
    pred1y=ceil(rand()*ymax);
    if A(pred1x,pred1y)>4
        A(pred1x,pred1y)=pred1color;
        iter_pred1=iter_pred1-1;
    end
end

%place predator type 2 code 3

while iter_pred2>0
    pred2y=ceil(rand()*ymax);
    A(soilmax-5*iter_pred2,pred2y)=pred2color;
    iter_pred2=iter_pred2-1;
end

%place predator type 3

while iter_pred3>0
    pred3x=ceil(rand()*0.75*soilmax)+ceil(0.25*soilmax);
    pred3y=ceil(rand()*ymax);
    if A(pred3x,pred3y)>4
        A(pred3x,pred3y)=pred3color;
        iter_pred3=iter_pred3-1;
    end
end


while cicadainfield>0
    for i=1:xmax
        for j=1:ymax
            oldA=A;
            id=i-1;
            if id<1
                id=i;
            end
            iu=i+1;
            if iu>xmax
                iu=i;
            end
            jr=j+1;
            if jr>ymax
                jr=j;
            end
            jl=j-1;
            if jl<1
                jl=j;
            end
            if oldA(i,j)<cicadacolor+0.1
                r=rand();
                A(i,j)=soilcolor;
                if r<0.1
                    if A(id,j)<cicadacolor+0.1
                        A(i,j)=cicadacolor;
                    elseif A(id,j)<pred3color+0.1
                        cicadainfield=cicadainfield-1;
                    else A(id,j)=cicadacolor;
                    end
                elseif r<0.3
                    if A(i,jr)<cicadacolor+0.1
                        A(i,j)=cicadacolor;
                    elseif A(i,jr)<pred3color+0.1
                        cicadainfield=cicadainfield-1;
                    else A(i,jr)=cicadacolor;
                    end
                elseif r<0.5
                    if A(i,jl)<cicadacolor+0.1
                        A(i,j)=cicadacolor;
                    elseif A(i,jl)<pred3color+0.1
                        cicadainfield=cicadainfield-1;
                    else A(i,jl)=cicadacolor;
                    end
                else if A(iu,j)<cicadacolor+0.1
                        A(i,j)=cicadacolor;
                    elseif A(iu,j)<pred3color+0.1
                        cicadainfield=cicadainfield-1;
                    elseif A(iu,j)<soilcolor+0.1
                        A(iu,j)=cicadacolor;
                    else A(iu,j)=freecicadacolor;
                        cicadainfield=cicadainfield-1;
                    end
                end
            elseif oldA(i,j)<pred1color+0.1
                r2=rand();
                A(i,j)=soilcolor;
                if iu>soilmax
                    iu=i;
                end
                if r2<0.25
                    if A(iu,j)<cicadacolor+0.1
                        A(iu,j)=pred1color;
                        cicadainfield=cicadainfield-1;
                    elseif A(iu,j)<pred3color+0.1
                        A(i,j)=pred1color;
                    else A(iu,j)=pred1color;
                    end
                elseif r2<0.5
                    if A(id,j)<cicadacolor+0.1
                        A(id,j)=pred1color;
                        cicadainfield=cicadainfield-1;
                    elseif A(id,j)<pred3color+0.1
                        A(i,j)=pred1color;
                    else A(id,j)=pred1color;
                    end
                elseif r2<0.75
                    if A(i,jl)<cicadacolor+0.1
                        A(i,jl)=pred1color;
                        cicadainfield=cicadainfield-1;
                    elseif A(i,jl)<pred3color+0.1
                        A(i,j)=pred1color;
                    else A(i,jl)=pred1color;
                    end
                else if A(i,jr)<cicadacolor +0.1
                        A(i,jr)=pred1color;
                        cicadainfield=cicadainfield-1;
                    elseif A(i,jr)<pred3color+0.1
                        A(i,j)=pred1color;
                    else A(i,jr)=pred1color;
                    end
                end
            elseif oldA(i,j)<pred2color+0.1
                r3=rand();
                A(i,j)=soilcolor;
                if r3<0.5
                    if A(i,jr)<cicadacolor+0.1
                        A(i,jr)=pred2color;
                        cicadainfield=cicadainfield-1;
                    elseif A(i,jr)<pred3color+0.1
                        A(i,j)=pred2color;
                    else A(i,jr)=pred2color;
                    end
                else if A(i,jl)<cicadacolor+0.1
                        A(i,jl)=pred2color;
                        cicadainfield=cicadainfield-1;
                    elseif A(i,jl)<pred3color+0.1
                        A(i,j)=pred2color;
                    else A(i,jl)=pred2color;
                    end
                end
            elseif oldA(i,j)<pred3color+0.1
                    if A(iu,j)<cicadacolor+0.1
                        A(iu,j)=soilcolor;
                        cicadainfield=cicadainfield-1;
                    end
                    if A(id,j)<cicadacolor+0.1
                        A(id,j)=soilcolor;
                        cicadainfield=cicadainfield-1;
                    end
                    if A(i,jl)<cicadacolor+0.1
                        A(i,jl)=soilcolor;
                        cicadainfield=cicadainfield-1;
                    end
                    if A(i,jr)<cicadacolor+0.1
                        A(i,jr)=soilcolor;
                        cicadainfield=cicadainfield-1;
                    end
                    if A(iu,jr)<cicadacolor+0.1
                        A(iu,jr)=soilcolor;
                        cicadainfield=cicadainfield-1;
                    end
                    if A(iu,jl)<cicadacolor+0.1
                        A(iu,jl)=soilcolor;
                        cicadainfield=cicadainfield-1;
                    end
                    if A(id,jr)<cicadacolor+0.1
                        A(id,jr)=soilcolor;
                        cicadainfield=cicadainfield-1;
                    end
                    if A(id,jl)<cicadacolor+0.1
                        A(id,jl)=soilcolor;
                        cicadainfield=cicadainfield-1;
                    end
            elseif oldA(i,j)<aircolor+0.1
            else A(i,j)=aircolor;
                survived_cicada_count=survived_cicada_count+1;
            end
        end
    end

count=count+1;
B=flipud(A);
imagesc(A)
set(gca,'YDir','normal');
colormap(map);
axis equal
axis tight
title(['grand total of ', num2str(survived_cicada_count), ' cicadas have survived'])
saveas(gcf, ['Cicada2_',sprintf('%03d',count),'.png'])
pause(0.2);
end





