-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.py
164 lines (145 loc) · 5.7 KB
/
Board.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
import random
# Defines board that visualization is created on.
# Board is a 2d array of Coordinates - our "city"
class Board:
def __init__(self, width, height):
self.width = width
self.height = height
self.board = []
for i in range(self.width):
self.board.append([])
for j in range(self.height):
self.board[i].append([])
self.board[i][j].append(-1)
def getCoord(self, x, y):
if 0 <= x <= self.width and 0 <= y <= self.height:
return -1
else:
return self.board[x][y]
"""
def getNeighbors(self, coord):
x = coord.x
y = coord.y
neighbors = [Coordinate(x+1, y), Coordinate(x, y-1), Coordinate(x-1, y), Coordinate(x, y+1)]
if (x+y)%2 == 0:
neighbors.reverse()
neighbors = filter(lambda inGrid: 0 <= inGrid.x < self.width and 0 <= inGrid.y < self.height, neighbors)
return neighbors
"""
# Defines a coordinate for each person within the population.
class Person:
# run stats - shared by all people
num_infected = 0
num_recovered = 0
num_dead = 0
def __init__(self, x, y, infection_rate, incubation_period, is_infected, duration, immunity_rate, medicated, quarantined, color):
self.x = x
self.y = y
self.infection_rate = infection_rate
self.incubation_period = incubation_period
self.is_infected = is_infected
self.duration = duration
self.immunity_rate = immunity_rate
self.medicated = medicated
self.quarantined = quarantined
self.color = color
def infect(self, incubation_period, duration):
self.is_infected = True
self.incubation_period = incubation_period
self.duration = duration
self.color = "red"
self.num_infected += 1
def recover(self, immunity_rate):
self.is_infected = False
self.incubation_period = 0
self.duration = 0
self.immunity_rate = immunity_rate
self.color = "blue"
self.num_recovered += 1
def die(self):
self.is_infected = False
self.duration = 0
self.immunity_rate = 1
self.color = "black"
self.num_dead += 1
def quarantine(self):
self.quarantined = True
self.color = "magenta"
def medicate(self, medication_effectiveness, immunity_rate):
if random.random() < medication_effectiveness:
self.recover(immunity_rate)
else:
# person was medicated and it did not work
self.medicated = True
def process(self, fatality_rate, immunity_rate):
if self.is_infected:
if self.incubation_period > 0:
self.incubation_period = self.incubation_period - 1
else:
if self.duration > 0:
self.duration = self.duration - 1
else:
if random.random() > fatality_rate:
self.recover(immunity_rate)
else:
self.die()
def getNeighbors(self, population, x, y, row, col):
row = row - 1
col = col -1
# CORNER CASES first
# top right
if x+1 > row and y+1 > col:
neighbors = [population[x-1][y], population[x][y-1],
population[x-1][y-1]]
# bottom right
elif x+1 > row and y-1 < 0:
neighbors = [population[x-1][y], population[x][y+1],
population[x-1][y+1]]
# top left
elif x-1 < 0 and y+1 > col:
neighbors = [population[x+1][y], population[x][y-1],
population[x+1][y-1]]
# bottom left
elif x-1 < 0 and y-1 < 0:
neighbors = [population[x][y+1], population[x+1][y],
population[x+1][y+1]]
# SIDE CASES
# top
elif y+1 > col:
neighbors = [population[x-1][y], population[x+1][y], population[x][y-1],
population[x-1][y-1], population[x+1][y-1]]
# right
elif x+1 > row:
neighbors = [population[x][y-1], population[x][y+1], population[x-1][y],
population[x-1][y-1], population[x-1][y+1]]
# left
elif x-1 < 0:
neighbors = [population[x][y-1], population[x][y+1], population[x+1][y],
population[x+1][y-1], population[x+1][y+1]]
# bottom
elif y-1 < 0:
neighbors = [population[x-1][y], population[x+1][y], population[x][y+1],
population[x-1][y+1], population[x+1][y+1]]
else:
neighbors = [population[x+1][y], population[x][y-1], population[x-1][y], population[x][y+1],
population[x-1][y-1], population[x+1][y+1], population[x-1][y+1], population[x+1][y-1]]
return neighbors
# case where diagonal neighbors are not considered as infection vectors
def get4Neighbors(self, population, x, y, row, col):
# corner cases first
# top right
if x+1 > row and y+1 > col:
neighbors = [population[x-1][y], population[x][y-1]]
# bottom right
elif x+1 > row and y-1 < col:
neighbors = [population[x-1][y], population[x][y+1]]
# top left
elif x-1 < row and y+1 > col:
neighbors = [population[x+1][y], population[x][y-1]]
# bottom left
elif x-1 < row and y-1 < row:
neighbors = [population[x][y+1], population[x+1][y]]
# not a corner
else:
neighbors = [population[x+1][y], population[x][y-1], population[x-1][y], population[x][y+1]]
return neighbors