-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmini_project4.py
375 lines (316 loc) · 11.3 KB
/
mini_project4.py
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# Mini-project 4 for Principles of Computing class, by k., 07/11/2014
# Zombie Apocalypse described at: https://class.coursera.org/principlescomputing-001/wiki/view?page=zombie
# http://www.codeskulptor.org/#poc_zombie_template.py
# Grid class from http://www.codeskulptor.org/#poc_grid.py
"""
Grid class
"""
EMPTY = 0
FULL = 1
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
Indexed 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 represenation for grid
"""
ans = ""
for row in range(self._grid_height):
ans += str(self._cells[row])
ans += "\n"
return ans
def get_grid_height(self):
"""
Return the height of the grid for use in the GUI
"""
return self._grid_height
def get_grid_width(self):
"""
Return the width of the grid for use in the GUI
"""
return self._grid_width
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 index (row, col) to be empty
"""
self._cells[row][col] = EMPTY
def set_full(self, row, col):
"""
Set cell with 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 horiz/vert neighbors of 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 horiz/vert neighbors of cell (row, col) as well as
diagonal neighbors
"""
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
containing cell
"""
return (point[1] / cell_size, point[0] / cell_size)
# Queue class from http://www.codeskulptor.org/#poc_queue.py
'''
Queue class
'''
class Queue:
"""
A simple implementation of a FIFO 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 __str__(self):
"""
Return a string representation of the queue.
"""
return str(self._items)
def enqueue(self, item):
"""
Add item to the queue.
"""
self._items.append(item)
def dequeue(self):
"""
Remove and return the least recently inserted item.
"""
return self._items.pop(0)
def clear(self):
"""
Remove all items from the queue.
"""
self._items = []
'''
student portion of Zombie Apocalypse mini-project
'''
import random
#import poc_grid
#import poc_queue
#import poc_zombie_gui
# global constants
EMPTY = 0
FULL = 1
FOUR_WAY = 0
EIGHT_WAY = 1
OBSTACLE = 'obstacle'
HUMAN = 'human'
ZOMBIE = 'zombie'
class Zombie(Grid): # for grader add poc_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
'''
Grid.__init__(self, grid_height, grid_width) # for grader add poc_grid.
if obstacle_list != None:
for cell in obstacle_list:
self.set_full(cell[0], cell[1])
# obstacle list was missing! (perhaps intentionally?)
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 = []
Grid.clear(self) # for grader add poc_grid.
def add_zombie(self, row, col):
'''
add a zombie to the zombie list
'''
self._zombie_list.append((row, col))
def num_zombies(self):
'''
return current 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 current 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 artifically 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 = Grid(self._grid_height, self._grid_width) # for grader add poc_grid.
for obstacle in self.obstacle():
visited.set_full(obstacle[0], obstacle[1])
# creates a copy of the human/zombie list
boundary = Queue() # for grader add poc_queue.
if entity_type == ZOMBIE:
list_type = self._zombie_list
elif entity_type == HUMAN:
list_type = self._human_list
# check whether the cell is passable and update the neighbor'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,
returns noting
'''
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]):
# and 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, diagonal moves are NOT allowed,
returns nothing
'''
temp_zombie_list = []
#for zombie in self.zombies():
for zombie in self._zombie_list:
neighbors = self.four_neighbors(zombie[0], zombie[1])
# store current position
distance = [human_distance[zombie[0]][zombie[1]]]
location = [zombie]
for resident in neighbors:
if self.is_empty(resident[0], resident[1]):
# and store rest of 4 other positions if not occupied
distance.append(human_distance[resident[0]][resident[1]])
location.append(resident)
# find the current most closest location, 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
# Start up gui for simulation - You will need to write some code above
# before this will work without errors
# poc_zombie_gui.run_gui(Zombie(30, 40))