top of page
Zombie Simulation

# -*- coding: utf-8 -*-
"""
Created on Thu Dec  2 22:51:20 2021

@author: downs
"""
import matplotlib.pyplot as plt
import random

class Scientist(object):
    def __init__(self, name):
        self.my_name = name
        self.X = 0
        self.Y = 0        
        
class Removed(Scientist):
    pass

class  Alive(Scientist):
    def Move(self):
        pass
    def __init__(self, name):
        super().__init__(name)
        self.health =  random.randint(1,100)

class Susceptible(Alive):
    def Move(self, a , b):
        self.X = random.randint(1,5)
        self.Y = random.randint(1,5)
        self.X += a
        self.Y += b
    
class LessSusceptible(Alive):
    def Move(self, a , b):
        self.X = 0
        self.Y = 0
        self.X += a
        self.Y += b

class Infected(Alive):
    def Move(self, a , b):
        self.X = random.randint(1,9)
        self.Y = random.randint(5,9)
        self.X -= a
        self.Y -= b

def ZombieSimulation(S,I, IslandSize):
    
    S_Count = S
    I_Count = I
    LS_Count = 0
    R_Count = 0
    
    SusceptibleList = []
    for i in range(1,S+1):
        Susceptible_obj = Susceptible(i)
        SusceptibleList.append(Susceptible_obj)
    
    InfectedList = []
    for j in range(1,I+1):
        Infected_obj = Infected(j)
        InfectedList.append(Infected_obj)        
    
    LessSusceptibleList = []
    RemovedList = []
    
    Iplot = []
    Splot = []
    Rplot = []
    
    KeepGoing = True    
    while(KeepGoing):  
        
        for k in SusceptibleList:
            k.Move(1,2)
            if k.X == IslandSize:
                k.Move(-1,2)
            if k.Y == IslandSize:
                k.Move(1,-2)
            if k.X == 0:
                k.Move(1,-2)
            if k.Y == 0:
                k.Move(-1,2) 

         
        for m in InfectedList:
            m.Move(1,2)
            if m.X == IslandSize:
                m.Move(-1,2)
            if m.Y == IslandSize:
                m.Move(1,-2)
            if m.X == 0:
                m.Move(1,-2)
            if m.Y == 0:
                m.Move(-1,2)            
     
        SusceptiblesToRemove = 0
        for k in SusceptibleList:
            #print(k.health)
            InfectedsToRemove = 0
            for m in InfectedList:
                if ((m.X in [k.X-3,k.X-2,k.X-1,k.X,k.X+1,k.X+2,k.X+3]) and (m.Y in [k.Y-3,k.Y-2,k.Y-1,k.Y,k.Y+1,k.Y+2,k.Y+3])):
                    randx = random.randint(1,100)
                    if randx > k.health:    #Infected wins
                        q = I_Count + 1
                        Infected_obj = Infected(q+1)                        
                        InfectedList.append(Infected_obj)
                        
                        S_Count -= 1
                        I_Count += 1
                        SusceptiblesToRemove += 1                            
                        
                        print("Susceptables = "+str(S_Count)) 
                        Splot.append(S_Count)                        
                        print("Infected = "+str(I_Count))
                        Iplot.append(I_Count)
                        print("Removed = "+str(R_Count))
                        Rplot.append(R_Count)
                        print()
                        
                        break
                    
                    else:   #Susceptible Wins  
     
                        k.health -= 1
                        InfectedsToRemove += 1
                        I_Count -= 1
                        R_Count += 1
                        Removed_obj = Removed(R_Count)                        
                        RemovedList.append(Removed_obj)
                        
                        print("Susceptables = "+str(S_Count)) 
                        Splot.append(S_Count) 
                        print("Infected = "+str(I_Count))
                        Iplot.append(I_Count)
                        print("Removed = "+str(R_Count))
                        Rplot.append(R_Count)
                        print()
                        
                        if k.health <= 0:
                            SusceptiblesToRemove += 1
                            break   
                   
            for v in range(InfectedsToRemove):
                InfectedList.remove(InfectedList[0])
       
        for l in range(SusceptiblesToRemove):
            SusceptibleList.remove(SusceptibleList[0])
          
        if(S_Count<=0):
            KeepGoing = False
        if(I_Count<=0):
            KeepGoing = False
    
    plt.figure(num=0,dpi=120)
    plt.plot(Iplot)
    plt.plot(Splot)
    plt.plot(Rplot)
    
ZombieSimulation(50,100, 100)
            
                        

bottom of page