-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzombie_apocalypse.py
More file actions
executable file
·352 lines (307 loc) · 10.8 KB
/
zombie_apocalypse.py
File metadata and controls
executable file
·352 lines (307 loc) · 10.8 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"""
Name: Zombie Apocalypse mini-project.
Author: jraleman
Year: 2014
"""
try:
import poc_grid
import poc_queue
import poc_zombie_gui
except ImportError:
import assets.poc_grid as poc_grid
import assets.poc_queue as poc_queue
import assets.poc_zombie_gui as poc_zombie_gui
import random
# Global constants.
EMPTY = 0
FULL = 1
FOUR_WAY = 0
EIGHT_WAY = 1
OBSTACLE = "obstacle"
HUMAN = "human"
ZOMBIE = "zombie"
class Grid:
"""
Implementation of 2D grid of cells
Includes boundary handling
"""
def __init__(self, grid_height, grid_width):
"""
Initializes grid to be empty, take height and width of grid as
parameters, idexed by rows (left to right),
then by columns (top to bottom).
"""
self._grid_height = grid_height
self._grid_width = grid_width
self._cells = [[EMPTY for dummy_col in range(self._grid_width)] \
for dummy_row in range(self._grid_height)]
def __str__(self):
"""
Return multi-line string representation for grid.
"""
ans = ""
for row in range(self._grid_height):
ans += str(self._cells[row]) + '\n'
return ans
def get_grid_width(self):
"""
Return the width of the grid for use in the GUI.
"""
return self._grid_width
def get_grid_height(self):
"""
Return the height of the grid for use in the GUI
"""
return self._grid_height
def clear(self):
"""
Clears grid to be empty.
"""
self._cells = [[EMPTY for dummy_col in range(self._grid_width)] \
for dummy_row in range(self._grid_height)]
def set_empty(self, row, col):
"""
Set cell with the index (row, col) to be empty.
"""
self._cells[row][col] = EMPTY
def set_full(self, row, col):
"""
Set cell with the index (row, col) to be full.
"""
self._cells[row][col] = FULL
def is_empty(self, row, col):
"""
Checks whether cell with index (row, col) is empty.
"""
return self._cells[row][col] == EMPTY
def four_neighbors(self, row, col):
"""
Returns horizontal/vertical neighbours of the cell (row, col).
"""
ans = []
if row > 0:
ans.append((row - 1, col))
if row < self._grid_height - 1:
ans.append((row + 1, col))
if col > 0:
ans.append((row, col - 1))
if col < self._grid_width - 1:
ans.append((row, col + 1))
return ans
def eight_neighbors(self, row, col):
"""
Returns horizontal/vertical neighbours of the cell (row, col),
as well as diagonal neighbours.
"""
ans = []
if row > 0:
ans.append((row - 1, col))
if row < self._grid_height - 1:
ans.append((row + 1, col))
if col > 0:
ans.append((row, col - 1))
if col < self._grid_width - 1:
ans.append((row, col + 1))
if (row > 0) and (col > 0):
ans.append((row - 1, col - 1))
if (row > 0) and (col < self._grid_width - 1):
ans.append((row - 1, col + 1))
if (row < self._grid_height - 1) and (col > 0):
ans.append((row + 1, col - 1))
if (row < self._grid_height - 1) and (col < self._grid_width - 1):
ans.append((row + 1, col + 1))
return ans
def get_index(self, point, cell_size):
"""
Takes point in screen coordinates and returns index
of the containing cell.
"""
return (point[1] / cell_size, point[0] / cell_size)
class Queue:
"""
A simple implementation of a FIFO (first in - first out) queue.
"""
def __init__(self):
"""
Initialize the queue.
"""
self._items = []
def __len__(self):
"""
Return the number of items in the queue.
"""
return len(self._items)
def __iter__(self):
"""
Create an iterator for the queue.
"""
for item in self._items:
yield item
def enqueue(self, item):
"""
Add item to the queue.
"""
self._items.append(item)
def dequeue(self, item):
"""
Remove and return the least recently inserted item.
"""
return self._items.pop(0)
def clear(self):
"""
Remove all items from the queue.
"""
self._items = []
class Zombie(poc_grid.Grid):
"""
Class for simulating zombie pursuit of human on grid with
obstacles.
"""
def __init__(self, grid_height, grid_width, obstacle_list = None,
zombie_list = None, human_list = None):
"""
Create a simulation of given size with given obstacles,
humans, and zombies.
"""
poc_grid.Grid.__init__(self, grid_height, grid_width)
if obstacle_list != None:
for cell in obstacle_list:
self.set_full(cell[0], cell[1])
self._obstacle_list = obstacle_list
else:
self._obstacle_list = []
if zombie_list != None:
self._zombie_list = list(zombie_list)
else:
self._zombie_list = []
if human_list != None:
self._human_list = list(human_list)
else:
self._human_list = []
def clear(self):
"""
Set cells in obstacle grid to be empty.
Reset zombie and human lists to be empty.
"""
self._zombie_list = []
self._human_list = []
poc_grid.Grid.clear(self)
def add_zombie(self, row, col):
"""
Add zombie to the zombie list.
"""
self._zombie_list.append((row, col))
def num_zombies(self):
"""
Return number of zombies.
"""
return len(self._zombie_list)
def zombies(self):
"""
Generator that yields the zombies in the order they were
added.
"""
for zombie in self._zombie_list:
yield zombie
def add_human(self, row, col):
"""
Add human to the human list.
"""
self._human_list.append((row, col))
def num_humans(self):
"""
Return number of humans.
"""
return len(self._human_list)
def humans(self):
"""
Generator that yields the humans in the order they were added.
"""
for human in self._human_list:
yield human
def obstacle(self):
"""
Generator that yields the list of obstacles.
"""
for obstacle in self._obstacle_list:
yield obstacle
def compute_distance_field(self, entity_type):
"""
Function computes a 2D distance field.
Distance at member of entity_queue is zero.
Shortest paths avoid obstacles and use distance_type distances.
"""
# Same size as the grid and initialized with artificially high values.
distance_field =[[self._grid_height * self._grid_width \
for dummy_col in range(self._grid_width)] \
for dummy_row in range(self._grid_height)]
# Grid visited initialized as to be empty.
visited = poc_grid.Grid(self._grid_height, self._grid_width)
for obstacle in self.obstacle():
visited.set_full(obstacle[0], obstacle[1])
# Creates a copy of the human/zombie list.
boundary = poc_queue.Queue()
if entity_type == ZOMBIE:
list_type = self._zombie_list
elif entity_type == HUMAN:
list_type = self._human_list
# Check if the cell is passable and update the neighbour's distance.
for item in list_type:
boundary.enqueue(item)
visited.set_full(item[0], item[1])
distance_field[item[0]][item[1]] = 0
# Breadth-first search
while boundary:
cell = boundary.dequeue()
neighbors = visited.four_neighbors(cell[0], cell[1])
for resident in neighbors:
if visited.is_empty(resident[0], resident[1]):
distance_field[resident[0]][resident[1]] = \
min(distance_field[resident[0]][resident[1]],
distance_field[cell[0]][cell[1]] + 1)
visited.set_full(resident[0], resident[1])
boundary.enqueue(resident)
return distance_field
def move_humans(self, zombie_distance):
"""
Function that moves humans away from zombies, diagonal moves
are allowed
"""
temp_human_list = []
for human in self.humans():
neighbors = self.eight_neighbors(human[0], human[1])
# Store current position.
distance = [zombie_distance[human[0]][human[1]]]
location = [human]
for resident in neighbors:
if self.is_empty(resident[0], resident[1]):
# Store rest of 8 other positions if not occupied.
distance.append(zombie_distance[resident[0]][resident[1]])
location.append(resident)
# Find the current safest location, move there.
safest = location[distance.index(max(distance))]
self.set_empty(human[0], human[1])
temp_human_list.append(safest)
self._human_list = temp_human_list
def move_zombies(self, human_distance):
"""
Function that moves zombies towards humans, no diagonal moves
are allowed
"""
temp_zombie_list = []
# For zombie in self.zombies()
for zombie in self._zombie_list:
neighbors = self.four_neighbors(zombie[0], zombie[1])
distance = [human_distance[zombie[0]][zombie[1]]]
location = [zombie]
for resident in neighbors:
if self.is_empty(resident[0], resident[1]):
# Store rest of four (4) other positions if not occupied.
distance.append(human_distance[resident[0]][resident[1]])
location.append(resident)
# Find the current most closest location, and move there.
closest = location[distance.index(min(distance))]
self.set_empty(zombie[0], zombie[1])
temp_zombie_list.append(closest)
self._zombie_list = temp_zombie_list
poc_zombie_gui.run_gui(Zombie(30, 40))