-
Notifications
You must be signed in to change notification settings - Fork 0
/
zombie-apocalypse.py
139 lines (121 loc) · 4.04 KB
/
zombie-apocalypse.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
"""
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 = 5
HUMAN = 6
ZOMBIE = 7
class Apocalypse(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])
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 = []
self._grid_height = grid_height
self._grid_width = grid_width
def clear(self):
"""
Set cells in obstacle grid to be empty
Reset zombie and human lists to be empty
"""
poc_grid.Grid.clear(self)
self._human_list = []
self._zombie_list = []
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.
"""
# replace with an actual generator
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.
"""
# replace with an actual generator
for human in self._human_list:
yield human
def compute_distance_field(self, entity_type):
"""
Function computes and returns a 2D distance field
Distance at member of entity_list is zero
Shortest paths avoid obstacles and use four-way distances
"""
visited = poc_grid.Grid.__init__(self, self._grid_height, self._grid_width)
distance_field = [[self._grid_height*self._grid_width for dummy in range(self._grid_width)] for dummy_j in range(self._grid_width)]
boundary = poc_queue.Queue()
if entity_type == "ZOMBIE":
boundary.enqueue(self.zombies)
elif entity_type == "HUMAN":
boundary.enqueue(self.humans)
visited[boundary.__iter__()[0]][boundary.__iter__()[1]] = FULL
distance_field[boundary.__iter__()[0]][boundary.__iter__()[1]] = EMPTY
curr_cell = boundary.dequeue()
neighbors = poc_grid.Grid.four_neighbors(self,curr_cell[0],curr_cell[1])
for cell in neighbors:
if visited[cell[0]][cell[1]] == 0:
visited[cell[0]][cell[1]] = FULL
boundary.enqueue(cell)
distance_field[cell[0]][cell[1]] += 1
return distance_field
def move_humans(self, zombie_distance_field):
"""
Function that moves humans away from zombies, diagonal moves
are allowed
"""
pass
def move_zombies(self, human_distance_field):
"""
Function that moves zombies towards humans, no diagonal moves
are allowed
"""
pass
# Start up gui for simulation - You will need to write some code above
# before this will work without errors
poc_zombie_gui.run_gui(Apocalypse(30, 40))