-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSean_the_sheep_of_the_dead.py
More file actions
171 lines (121 loc) · 4.73 KB
/
Sean_the_sheep_of_the_dead.py
File metadata and controls
171 lines (121 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 16 14:12:33 2019
@author: gynjkm
"""
#imports required packages
import matplotlib.pyplot as plt
import matplotlib.animation
import agentframework_zombies
import csv
import random
#defines our arguments and creating the lists of sheep and zombiesheep
num_of_agents = 100
num_of_iterations = 150
neighbourhood = 15
num_of_zombsheep = 3
num_of_landmines = 20
blast_radius = 25
agents = []
zombsheep = []
holylandmines = []
#creates the environment from the csv file
environment = []
with open('in.txt', newline='') as f:
reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
for row in reader:
rowlist = []
for value in row:
rowlist.append(value)
environment.append(rowlist)
#Tests whether the environment has read in properly
"""
plt.imshow(environment)
plt.show()
"""
#Assign starting points to all our agents in their environment
for i in range (num_of_agents):
agents.append(agentframework_zombies.Agent(environment, agents))
for i in range (num_of_zombsheep):
zombsheep.append(agentframework_zombies.Zombiesheep(environment, zombsheep, agents))
for i in range (num_of_landmines):
holylandmines.append(agentframework_zombies.Holy_landmine_of_Antioch(environment, zombsheep))
fig = plt.figure(figsize=(12, 12))
'''
#Testing to see if our agents have acces to the locations of other agents
print("Our first sheep is at", agents[0].x, agents[0].y, ", some other sheep he knows are at:")
for i in range(10):
print(agents[0].agents[i].x, agents[0].agents[i].y)
'''
'''
This makes the model run until the zombies have wiped out all ofthe sheep or
the desired number of iterations has been reached.
'''
def update(frame_number):
fig.clear()
plt.imshow(environment)
plt.xlim(0, agents[0].environment_width)
plt.ylim(0, agents[0].environment_height)
plt.xlim(0, zombsheep[0].environment_width)
plt.ylim(0, zombsheep[0].environment_height)
if len(holylandmines) == 0:
pass
else:
plt.xlim(0, holylandmines[0].environment_width)
plt.ylim(0, holylandmines[0].environment_height)
#shuffles the order in which agents in a list move to avoid "first mover" advantages
random.shuffle(agents)
random.shuffle(zombsheep)
random.shuffle(holylandmines)
for agent in agents:
agent.move()
agent.eat()
agent.share_with_neighbours(neighbourhood)
for zombiesheep in zombsheep:
zombiesheep.move()
#creates a list of all sheep within "biting range"
target_agents = zombiesheep.bite(neighbourhood, agents, zombsheep)
for target in target_agents:
#adds a new zombie in place of the target's location
zombsheep.append(agentframework_zombies.Zombiesheep(environment, zombsheep, agents, [target.y, target.x]))
#kills the target
agents.remove(target)
#this is done in this order to avoid losing the coordinates of the target
if len(holylandmines) == 0:
pass
else:
for Holy_landmine_of_Antioch in holylandmines:
ded_zombies = Holy_landmine_of_Antioch.detonate(blast_radius, zombsheep)
if len(ded_zombies)> 0:
for ded_zombie in ded_zombies:
zombsheep.remove(ded_zombie)
holylandmines.remove(Holy_landmine_of_Antioch)
#plots our sheep in white and our zombies in red and our landmines in gold
for agent in agents:
plt.scatter(agent.x, agent.y, c="snow")
for zombiesheep in zombsheep:
plt.scatter(zombiesheep.x, zombiesheep.y, c="red")
if len(holylandmines) == 0:
pass
else:
for Holy_landmine_of_Antioch in holylandmines:
plt.scatter(Holy_landmine_of_Antioch.x, Holy_landmine_of_Antioch.y, c="gold")
print(frame_number)
#Prints an update on how the sheep vs zombie battle is going
print("There are", str(len(agents)), "sheep, ", str(len(zombsheep)), "zombie sheep, and", str(len(holylandmines)), "remaining.")
#prints a victory message for the zombies if they manage to convert all the sheep
if len(agents) == 0:
print("Braiiiiins! Zombies win!")
#prints a victory message for the sheep if they manage to survive until dawn or all zombies die
if int(frame_number) == int(num_of_iterations)-1:
print("Baaaahhhh! Sheep win!")
if len(zombsheep) == 0:
print("Baaaahhhh! Sheep win!")
#Showing our agents in animation
plt.ylim(0, 299)
plt.xlim(0, 299)
plt.imshow(environment)
#for i in range (num_of_agents):
# plt.scatter(agents[i].x,agents[i].y)
animation = matplotlib.animation.FuncAnimation(fig, update, interval=1, repeat=False, frames=num_of_iterations)
plt.show()