%Type: "lifan_zhang_eps_final" to run the code
%after 100 particles are placed on the grid, tap space bar twice to start
%the infestation.

clear

world_width = 50;
world_length = 50;
population = 100;
time = 400;
range = 3;
prob_infected = 0.5;
prob_diseased = 0.1;
num_diseased = 0;

nothing = 0;
diseased = 2;
infected = 6;
normal = 10;

WORLD = zeros(world_width,world_length);

draw_X = zeros(1,population);
draw_Y = zeros(1,population);
%initialize population into the world matrix
 
j= 1;
h1 = figure(2);

for person = 1:population
    found = true;
    while found
        prob_x = floor(rand*(world_width-1)+1);
        prob_y = floor(rand*(world_length-1)+1);
        if ~(WORLD(prob_x, prob_y) == normal)
            WORLD(prob_x, prob_y) = normal;
            draw_X(person) = prob_x;
            draw_Y(person) = prob_y;
            found = false;
        end
    end
    imagesc(WORLD);
    set(gca,'YDir','normal');
    colormap(hot);
    axis equal;
    axis tight;
    drawnow
    %saveas(h1,strcat('eps',num2str(999+j),'.jpg'));
    %j = j+1;
end

% imagesc(WORLD);
% set(gca,'YDir','normal');
% colormap(hot);
% axis equal;
% axis tight;
% drawnow
pause

dis_per = floor(rand*population)+1;
dis_x = draw_X(dis_per);
dis_y = draw_Y(dis_per);
num_diseased = num_diseased + 1;
WORLD(dis_x, dis_y) = diseased;
%     draw_X(dis_per) = -1;
%     draw_Y(dis_per) = -1;
    
imagesc(WORLD);
set(gca,'YDir','normal');
colormap(hot);
axis equal;
axis tight;
drawnow
pause

%saveas(h1,strcat('eps',num2str(999+j),'.jpg'));
%j = j+1;



for iter = 1:time
    
    for person = 1:population
        
        person_x = draw_X(person);
        person_y = draw_Y(person);
        
        isdiseased = false;
        isinfected = false;
        
        if WORLD(person_x, person_y) == diseased
            isdiseased = true;
        end
        
        if WORLD(person_x, person_y) == infected
            isinfected = true;
        end

        
        dis_x = draw_X(dis_per);
        dis_y = draw_Y(dis_per);
        
        distance_to_diseased = (dis_x-person_x)^2 + (dis_y-person_y)^2;
        if (isinfected == false && isdiseased == false) && distance_to_diseased < range^2
            if rand < prob_infected
                isinfected = true;
            end
        else if isinfected == true && isdiseased == false && distance_to_diseased < range^2
                if rand < prob_diseased
                    isdiseased = true;
                    num_diseased = num_diseased + 1;
                end
            end
        end
        
        
        
        if (person_x-1 >0 && WORLD(person_x-1, person_y)== normal) || ...
                (person_y-1 > 0 && WORLD(person_x, person_y-1)== normal) || ...
                (person_x+1 < world_width && WORLD(person_x+1, person_y)== normal)  || ...
                (person_y+1 < world_length && WORLD(person_x, person_y+1)== normal)
            moving = 0;
        else
            moving = floor(rand*4) + 1;
        end
        
        if isinfected == true || isdiseased == true
            moving = floor(rand*4) + 1;
        end
        
        if moving == 1  && person_x-1>0 && WORLD(person_x-1, person_y) == nothing
            WORLD(person_x, person_y) = nothing;
            WORLD(person_x-1, person_y) = normal;
            if isdiseased == true
                WORLD(person_x-1, person_y) = diseased;
            else if isinfected == true
                    WORLD(person_x-1, person_y) = infected;
                end
            end
            draw_X(person) = person_x-1;
            %break
        end
        if moving == 2 && person_y+1<world_length && WORLD(person_x, person_y+1) == nothing
            WORLD(person_x, person_y) = nothing;
            WORLD(person_x, person_y+1) = normal;
            if isdiseased == true
                WORLD(person_x, person_y+1) = diseased;
            else if isinfected == true
                    WORLD(person_x, person_y+1) = infected;
                end
            end
            draw_Y(person) = person_y+1;
            %break
        end
        if moving == 3 && person_x+1 < world_width && WORLD(person_x+1, person_y) == nothing
            WORLD(person_x, person_y) = nothing;
            WORLD(person_x+1, person_y) = normal;
            if isdiseased == true
                WORLD(person_x+1, person_y) = diseased;
            else if isinfected == true
                    WORLD(person_x+1, person_y) = infected;
                end
            end
            draw_X(person) = person_x+1;
            %break
        end
        if moving == 4 && person_y-1 > 0 && WORLD(person_x, person_y-1) == nothing
            WORLD(person_x, person_y) = nothing;
            WORLD(person_x, person_y-1) = normal;
            if isdiseased == true
                WORLD(person_x, person_y-1) = diseased;
            else if isinfected == true
                    WORLD(person_x, person_y-1) = infected;
                end
            end
            draw_Y(person) = person_y-1;
            %break
        end
    end
    
    
    imagesc(WORLD);
    set(gca,'YDir','normal');
    colormap(hot);
    axis equal;
    axis tight;
    drawnow
    %saveas(h1,strcat('eps',num2str(999+j),'.jpg'));
    %j = j+1;
    %pause
    
end

fprintf('The number of diseased people is %4d out of %4d\n',num_diseased,population);
fprintf('The duration of this infection period is %4d.\n',time);
fprintf('The range of infection is %4d distance away.\n',range);
fprintf('The probability of being infected is %4d and the probability of turning into a diseased is %4d.\n',prob_infected, prob_diseased);