-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSean_the_sheep_of_the_dead_with_GUI.py
More file actions
214 lines (156 loc) · 6.19 KB
/
Sean_the_sheep_of_the_dead_with_GUI.py
File metadata and controls
214 lines (156 loc) · 6.19 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 16 14:12:33 2019
@author: gynjkm
"""
#imports required packages
import tkinter as tk
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.animation
import agentframework_zombies
import csv
import random
#Creates a window which allows us to enter in parameters
fields = ('Number of Sheep', 'Number of Zombies', 'Number of Landmines', 'Number of Iterations', 'Neighborhood size', 'Explosion size')
def run():
animation = matplotlib.animation.FuncAnimation(fig, update(), interval=1, repeat=False, frames=num_of_iterations)
canvas.show()
def makeform(root, fields):
entries = {}
for field in fields:
row = tk.Frame(root)
lab = tk.Label(row, width=22, text=field+": ", anchor='w')
ent = tk.Entry(row)
ent.insert(0, "0")
row.pack(side=tk.TOP,
fill=tk.X,
padx=5,
pady=5)
lab.pack(side=tk.LEFT)
ent.pack(side=tk.RIGHT,
expand=tk.YES,
fill=tk.X)
entries[field] = ent
return entries
root = tk.Tk()
root.wm_title("Sheep Horror Model")
ents = makeform(root, fields)
b1 = tk.Button(root, text='Run the model!',command=(run))
b1.pack(side=tk.LEFT, padx=5, pady=5)
fig = plt.figure(figsize=(12, 12))
root.mainloop()
canvas = matplotlib.backends.backend_tkagg.FigureCanvasTkAgg(fig, master=root)
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
#defines our arguments and creating the lists of sheep and zombiesheep
num_of_agents = int(entries['Number of Sheep'].get())
num_of_zombsheep = int(entries['Number of Zombies'].get())
num_of_landmines = int(entries['Number of Landmines'].get())
num_of_iterations = int(entries['Number of Iterations'].get())
neighbourhood = int(entries['Neighborhood size'].get())
blast_radius = int(entries['Explosion size'].get())
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))
'''
#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 model in an 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()
#ends
tk.mainloop()