-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.py
executable file
·278 lines (217 loc) · 8.03 KB
/
tokens.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
"""
File: tokens.py
Author: Mark Rutherford
Created: 4/20/2021 2:22 PM
"""
from flask import url_for
import math
import random
FRIENDLY_COLOR = '#00AA00'
ENEMY_COLOR = '#AA0000'
ENEMY_INFLUENCE_COLOR = '#F79E9E'
FRIENDLY_INFLUENCE_COLOR = '#9EF79E'
VALID_MOVE_COLOR = '#74C3ED'
ENEMY_VALID_MOVE = '#CDC3ED'
FRIENDLY_VALID_MOVE = '#74EDED'
RADIO_CAN_KILL_PLAYER = False
class Token:
def __init__(self, cell):
self.cell = cell
self.move_deltas = ()
self.influence_deltas = ()
self.faction = None
self.is_village = False
def get_moves(self):
if self.cell.game.is_done:
return []
row = self.cell.row
col = self.cell.col
possible_moves = []
for move_delta_row, move_delta_col in self.move_deltas:
loc_row = row + move_delta_row
loc_col = col + move_delta_col
move_cell = self.cell.game.get_cell(loc_row, loc_col)
if move_cell is not None and (isinstance(move_cell.token, Empty) or isinstance(move_cell.token, TutsiPlayer) or isinstance(move_cell.token, Village)):
possible_moves.append(move_cell)
return possible_moves
def get_influence(self):
row = self.cell.row
col = self.cell.col
influence = []
if self.influence_deltas == 'any':
for row in self.cell.game.board:
for cell in row:
influence.append(cell)
else:
for delta_row, delta_col in self.influence_deltas:
loc_row = row + delta_row
loc_col = col + delta_col
loc_cell = self.cell.game.get_cell(loc_row, loc_col)
if loc_cell is not None:
influence.append(loc_cell)
return influence
def tick_turn(self):
pass
def make_move(self, row, col):
pass
def get_hutu_image(self):
return None
def get_tutsi_image(self):
return None
def get_hutu_color(self):
return None
def get_tutsi_color(self):
return None
class TutsiPlayer(Token):
def __init__(self, cell):
super().__init__(cell)
self.move_deltas = ((-1, 0), (0, -1), (1, 0), (0, 1))
self.faction = 'tutsi'
def get_tutsi_image(self):
return url_for('static', filename='images/player.png')
def get_hutu_image(self):
if self.cell.game.is_done:
return url_for('static', filename='images/player.png')
elif self.is_village:
return url_for('static', filename='images/village.png')
else:
return None
def get_tutsi_color(self):
return FRIENDLY_COLOR
def get_hutu_color(self):
if self.cell.game.is_done:
return ENEMY_COLOR
else:
return None
def make_move(self, row, col):
target_cell = self.cell.game.get_cell(row, col)
if target_cell and target_cell in self.get_moves():
become_village = target_cell.token.is_village
is_organized_village = False
if isinstance(target_cell.token, Village):
is_organized_village = target_cell.token.is_organized
target_cell.token = self
if self.is_village:
self.cell.token = Village(self.cell)
self.is_village = False
else:
self.cell.token = Empty(self.cell)
self.cell = target_cell
if become_village:
self.is_village = True
if is_organized_village:
self.cell.game.is_done = True
self.cell.game.tutsi_game_message = f'{self.cell.game.player_tutsi.username} perished in a village engaging in organized violence'
self.cell.game.hutu_game_message = f'{self.cell.game.player_tutsi.username} perished in a village engaging in organized violence'
self.cell.game.switch_turn()
class Wall(Token):
def __init__(self, cell):
super().__init__(cell)
def get_hutu_color(self):
return '#000000'
def get_tutsi_color(self):
return '#000000'
class Empty(Token):
def __init__(self, cell):
super().__init__(cell)
class Roadblock(Token):
def __init__(self, cell, can_move=True):
super().__init__(cell)
self.can_move = can_move
self.faction = 'hutu'
def __str__(self):
return 'roadblock'
def get_moves(self):
if self.cell.game.is_done or not self.can_move:
return []
possible_moves = []
for row in self.cell.game.board:
for cell in row:
if isinstance(cell.token, Empty) or isinstance(cell.token, TutsiPlayer):
possible_moves.append(cell)
return possible_moves
def make_move(self, row, col):
target_cell = self.cell.game.get_cell(row, col)
if target_cell and target_cell in self.get_moves():
target_cell.token = Roadblock(target_cell, False)
self.cell.game.switch_turn()
def get_hutu_color(self):
return FRIENDLY_COLOR
def get_tutsi_color(self):
return ENEMY_COLOR
class RadioStation(Token):
def __init__(self, cell, can_move=True):
super().__init__(cell)
self.can_move = can_move
self.faction = 'hutu'
self.influence_deltas = []
influence_range = 4.5
for delta_x in range(-math.ceil(influence_range), math.ceil(influence_range)+1):
for delta_y in range(-math.ceil(influence_range), math.ceil(influence_range)+1):
if math.sqrt(delta_x**2 + delta_y**2) <= influence_range:
self.influence_deltas.append((delta_x, delta_y))
def __str__(self):
return 'radio station'
def get_moves(self):
if self.cell.game.is_done or not self.can_move:
return []
possible_moves = []
for row in self.cell.game.board:
for cell in row:
possible_moves.append(cell)
return possible_moves
def make_move(self, row, col):
target_cell = self.cell.game.get_cell(row, col)
if target_cell and target_cell in self.get_moves():
target_cell.token = RadioStation(target_cell, False)
self.cell.game.switch_turn()
def tick_turn(self):
empty_influence = []
for cell in self.get_influence():
if isinstance(cell.token, Empty) or (isinstance(cell.token, TutsiPlayer) or (isinstance(cell.token, Village)) and RADIO_CAN_KILL_PLAYER):
empty_influence.append(cell)
if empty_influence:
chosen_cell = random.choice(empty_influence)
chosen_cell.token = Roadblock(chosen_cell, False)
def get_hutu_image(self):
return url_for('static', filename='images/radiostation.png')
def get_tutsi_image(self):
return url_for('static', filename='images/radiostation.png')
def get_hutu_color(self):
return FRIENDLY_COLOR
def get_tutsi_color(self):
return ENEMY_COLOR
class Village(Token):
def __init__(self, cell):
super().__init__(cell)
self.move_deltas = [(0, 0)]
self.is_organized = False
self.is_village = True
self.faction = 'hutu'
def __str__(self):
return "village"
def make_move(self, row, col):
target_cell = self.cell.game.get_cell(row, col)
if target_cell == self.cell and not self.is_organized:
self.is_organized = True
self.cell.game.switch_turn()
def get_moves(self):
return [self.cell]
def get_hutu_image(self):
return url_for('static', filename='images/village.png')
def get_tutsi_image(self):
return url_for('static', filename='images/village.png')
def get_hutu_color(self):
if self.is_organized:
return FRIENDLY_COLOR
else:
return None
def get_tutsi_color(self):
if self.is_organized and self.cell.game.is_done:
return ENEMY_COLOR
else:
return None
def main():
pass
if __name__ == '__main__':
main()