import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
### population data frame
population=pd.DataFrame()
import random
x_number_list=[]

for i in range(0,300):
    num=random.randint(1,50)
    x_number_list.append(num)
population["x"]= pd.Series(x_number_list)
y_number_list=[]

for i in range(0,300):
    num=random.randint(1,50)
    y_number_list.append(num)
population["y"]= pd.Series(y_number_list)
"""
## initial positions of the population
"""

figure(figsize=(8, 6), dpi=80)
sns.scatterplot(x=population["x"],y=population["y"])
### include status to each person on the map, where all the population are humans in the initial stage

population["status"]='human'
## now thw data frame looks as below
population.head()
from matplotlib.pyplot import figure

"""
### now introduce a zombie in the plot

### introducing Zombie in the population
"""

x= population.sample(1)
index= x.index
population.at[index,"status"]="zombie"

## the plot after introducing zombie looks as below
figure(figsize=(8, 6), dpi=80)
sns.scatterplot(data=population,x="x",y="y",hue="status")
"""
## applying random movements on the population
"""
def population_random_walk_1(n):
    for i in range(n):
        for a in population[population["status"]=="human"].index:
            population.loc[a,"x"]= population.loc[a,"x"]+np.random.choice([-1,1])
            population.loc[a,"y"]= population.loc[a,"y"]+np.random.choice([-1,1])
        
  
population_random_walk_1(10)
figure(figsize=(8, 6), dpi=80)
sns.scatterplot(data=population,x="x",y="y",hue="status")
"""
## applyinig random walk on zombies
"""
def zombie_random_walk_1(n):
    
    for i in range(n):
        for a in population[population["status"]=="zombie"].index:
            population.loc[a,"x"]= population.loc[a,"x"]+np.random.choice([-1,1])
            population.loc[a,"y"]= population.loc[a,"y"]+np.random.choice([-1,1])

zombie_random_walk_1(10)
"""
## the plot after introducing zombie looks as below
"""
figure(figsize=(8, 6), dpi=80)
sns.scatterplot(data=population,x="x",y="y",hue="status")
## check on number of zombies and humans in the population
population["status"].value_counts()
"""
## create zombie movement modification
"""
### create zombie movement modification - latest code

def spread_zombies(zom_speed,population_speed,zombie_spread):
    counter=0
    intervels = [x for x in zombie_spread]
    
    for a in intervels:
        while counter<=a:
            population_random_walk_1(population_speed)
            zombie_random_walk_1(zom_speed)
        
            for index2,rows2 in population[population["status"]=='zombie'].iterrows():
                for index1,rows1 in population[population["status"]=='human'].iterrows():
        
                    if rows2["x"]+1 == rows1["x"] and rows2["y"]+1 == rows1["y"]:
            
                        population.loc[(population["x"]==rows1["x"]) & (population["y"]==rows1["y"]),["status"]]="zombie"
                        counter=counter+1
        
                    if rows2["x"]+1 == rows1["x"] and rows2["y"] == rows1["y"]:
            
                        population.loc[(population["x"]==rows1["x"]) & (population["y"]==rows1["y"]),["status"]]="zombie"
                        counter=counter+1
            
                    if rows2["x"] == rows1["x"] and rows2["y"]+1 == rows1["y"]:
            
                        population.loc[(population["x"]==rows1["x"]) & (population["y"]==rows1["y"]),["status"]]="zombie"
                        counter=counter+1
            
                    if rows2["x"]-1 == rows1["x"] and rows2["y"]-1 == rows1["y"]:
            
                        population.loc[(population["x"]==rows1["x"]) & (population["y"]==rows1["y"]),["status"]]="zombie"
                        counter=counter+1
            
                    if rows2["x"]-1 == rows1["x"] and rows2["y"] == rows1["y"]:
            
                        population.loc[(population["x"]==rows1["x"]) & (population["y"]==rows1["y"]),["status"]]="zombie"
                        counter=counter+1
            
                    if rows2["x"] == rows1["x"] and rows2["y"]-1 == rows1["y"]:
            
                        population.loc[(population["x"]==rows1["x"]) & (population["y"]==rows1["y"]),["status"]]="zombie"
                        counter=counter+1
            
                    if rows2["x"]+1 == rows1["x"] and rows2["y"]-1 == rows1["y"]:
            
                        population.loc[(population["x"]==rows1["x"]) & (population["y"]==rows1["y"]),["status"]]="zombie"
                        counter=counter+1
            
                    if rows2["x"]-1 == rows1["x"] and rows2["y"]+1 == rows1["y"]:
            
                        population.loc[(population["x"]==rows1["x"]) & (population["y"]==rows1["y"]),["status"]]="zombie"
                        counter=counter+1
        plot_zombie_spread(counter)
            
population_speed= int(input("enter population speed"))
zombie_speed= int(input("enter zombie speed"))

zombie_spread_intervals=[5,10,15,20,25,40,60,70,80,100,120,150,200,230]


spread_zombies(zombie_speed,population_speed,zombie_spread_intervals )
## the population after zombie spread looks as below
population["status"].value_counts()
## the plot after introducing zombie looks as below

def plot_zombie_spread(counter=0):
    figure(figsize=(8, 6), dpi=80)
    palette ={"human": "green", "zombie": "red"}
    fig = sns.scatterplot(data=population,x="x",y="y",hue="status", palette=palette)
    fig.figure.savefig(f"spread{counter}.png")
"""
Try the below code on your own, where i have introduced immunity and code that converts images into gifs
"""
"""
## introducing immunity to the population

"""
"""
Here I am adding an another column in the dataframe with immunity ranging from 0- 10 in random way

if immunity is greater than 7, the human will not get affected by the zombie even with the zombie bite
"""
immunity=[]

for i in range(0,100):
    num=random.randint(0,9)
    immunity.append(num)
population["immunity"]=pd.Series(immunity)
## the zombie spread model is modified as 

def spread_zombies_immunity(zom_speed,population_speed,zombie_spread):
    counter=0
    intervels = [x for x in zombie_spread]
    
    for a in intervels:
        while counter<=a:
            population_random_walk_1(population_speed)
            zombie_random_walk_1(zom_speed)
        
            for index2,rows2 in population[population["status"]=='zombie'].iterrows():
                for index1,rows1 in population[population["status"]=='human'].iterrows():
        
                    if rows2["x"]+1 == rows1["x"] and rows2["y"]+1 == rows1["y"] and rows1["immunity"]>7:
            
                        population.loc[(population["x"]==rows1["x"]) & (population["y"]==rows1["y"]),["status"]]="zombie"
                        counter=counter+1
        
                    if rows2["x"]+1 == rows1["x"] and rows2["y"] == rows1["y"] and rows1["immunity"]>7:
            
                        population.loc[(population["x"]==rows1["x"]) & (population["y"]==rows1["y"]),["status"]]="zombie"
                        counter=counter+1
            
                    if rows2["x"] == rows1["x"] and rows2["y"]+1 == rows1["y"]and rows1["immunity"]>7:
            
                        population.loc[(population["x"]==rows1["x"]) & (population["y"]==rows1["y"]),["status"]]="zombie"
                        counter=counter+1
            
                    if rows2["x"]-1 == rows1["x"] and rows2["y"]-1 == rows1["y"] and rows1["immunity"]>7:
            
                        population.loc[(population["x"]==rows1["x"]) & (population["y"]==rows1["y"]),["status"]]="zombie"
                        counter=counter+1
            
                    if rows2["x"]-1 == rows1["x"] and rows2["y"] == rows1["y"]:
            
                        population.loc[(population["x"]==rows1["x"]) & (population["y"]==rows1["y"]),["status"]]="zombie"
                        counter=counter+1
            
                    if rows2["x"] == rows1["x"] and rows2["y"]-1 == rows1["y"] and rows1["immunity"]>7:
            
                        population.loc[(population["x"]==rows1["x"]) & (population["y"]==rows1["y"]),["status"]]="zombie"
                        counter=counter+1
            
                    if rows2["x"]+1 == rows1["x"] and rows2["y"]-1 == rows1["y"] and rows1["immunity"]>7:
            
                        population.loc[(population["x"]==rows1["x"]) & (population["y"]==rows1["y"]),["status"]]="zombie"
                        counter=counter+1
            
                    if rows2["x"]-1 == rows1["x"] and rows2["y"]+1 == rows1["y"] and rows1["immunity"]>7:
            
                        population.loc[(population["x"]==rows1["x"]) & (population["y"]==rows1["y"]),["status"]]="zombie"
                        counter=counter+1
        plot_zombie_spread(counter)
            
population_speed= int(input("enter population speed"))
zombie_speed= int(input("enter zombie speed"))

zombie_spread_intervals=[5,10,15,20,25,40,60,70,80,100,120,150,200,230]


spread_zombies_immunity(zombie_speed,population_speed,zombie_spread_intervals )
"""
### converting the above spread into gif
"""
"""
the gif is created by using the snapshots of frames and using the application https://imgflip.com/gif-maker
"""
from IPython.display import Image
Image(filename="zombie_spread_immunity.gif")