-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
394 lines (358 loc) · 15 KB
/
helper.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
##########################################################################
## helper.py
##
## Contains several misc. helper classes.
##
## by Andrew Francis
##########################################################################
import pygame, os, sys
from pygame.locals import *
from constants import *
def is_game_over(players, b):
"""Determines if the game is over"""
flags_captured = []
pieces_disabled = []
# Initialize lists
for i in range(NUM_PLAYERS):
pieces_disabled.append(True)
flags_captured.append(0)
for i in range(len(players)):
player = players[i]
# Go through each player's pieces
for piece in player.pieces:
# and check if the flag is trapped
if piece.type == 'F' and piece.trapped:
flags_captured[i] += 1
# and check, if the piece is alive, if it can move
elif not(piece.killed):
moves = possible_moves(piece, b, players)
if len(moves) > 0:
pieces_disabled[i] = False
# if both red flags are captured, blue wins
if flags_captured[0] + flags_captured[2] is 2:
return 'Blue'
# if both blue flags are captured, red wins
elif flags_captured[1] + flags_captured[3] is 2:
return 'Red'
# if none of red's pieces can move, blue wins
if pieces_disabled[0] and pieces_disabled[2]:
return 'Blue'
# if none of blue's pieces can move, red wins
elif pieces_disabled[1] and pieces_disabled[3]:
return 'Red'
return None
def fight(attacker, defender):
"""Determines the outcome of combat between the pieces attacker and defender
Returns 1 if attacker wins, 0 if defender wins"""
try:
# if the pieces are numbers, higher number wins, attacker wins ties
aType = int(attacker.type)
dType = int(defender.type)
if aType >= dType:
return 1
else:
return 0
except:
if attacker.type is 'B': # Bombs
return 1 # kill everything
elif attacker.type is 'S': # Spies
if defender.type in ['F', 'S', '10', 'B']:
return 1 # kill flags, spies, marshals, and bombs
else:
return 0 # die otherwise
elif attacker.type is 'F': # Flags
return 0 # shouldn't be able to attack
if defender.type is 'B': # Bombs
return 1 # always lose
elif defender.type is 'S': # Spies
return 1 # always lose
elif defender.type is 'F': # Flags
return 1 # always lose
return 1
def starting_tiles(color, b):
"""Returns all starting tiles for given color"""
moves = []
for i in range(4,11):
if color is 'red':
moves.append((i,13))
moves.append((i,12))
elif color is 'dred':
moves.append((i,1))
moves.append((i,2))
elif color is 'blue':
moves.append((1,i))
moves.append((2,i))
elif color is 'dblue':
moves.append((13,i))
moves.append((12,i))
for x,y in moves:
if not b.is_legal(x,y):
moves.remove((x,y))
return moves
def starting_moves(selected, b, player):
"""Finds all available starting tiles for the selected piece"""
moves = starting_tiles(selected.color, b)
toRemove = []
# mark tiles for removal that already have pieces on them
if player.color is selected.color:
for i,j in moves:
tileRect = b.tiles[i][j].rect
for piece in player.pieces:
if piece.click_check(tileRect) is not None:
toRemove.append((i,j))
# remove marked tiles
for pos in toRemove:
moves.remove(pos)
return moves
def is_occupied(p, b, x, y, players):
"""Checks if there is a piece from player p or their ally in the space (x,y)"""
tileRect = b.tiles[x][y].rect
if p.color in RED_TEAM:
for pl in players:
if pl.color in RED_TEAM:
for piece in pl.pieces:
collision = piece.click_check(tileRect)
if collision is not None:
return True
elif p.color in BLUE_TEAM:
for pl in players:
if pl.color in BLUE_TEAM:
for piece in pl.pieces:
collision = piece.click_check(tileRect)
if collision is not None:
return True
return False
def is_occupied_excluding(p, players, b, x, y):
"""Checks if there is a piece from a player other than p in the space (x,y)"""
tileRect = b.tiles[x][y].rect
for pl in players:
if pl.color is p.color:
continue
for piece in pl.pieces:
collision = piece.click_check(tileRect)
if collision is not None:
return True
return False
def is_occupied_enemy(p, players, b, x, y):
"""Checks if there is a piece from an enemy player in the space (x,y)"""
tileRect = b.tiles[x][y].rect
if p.color in RED_TEAM:
for pl in players:
if pl.color in BLUE_TEAM:
for piece in pl.pieces:
collision = piece.click_check(tileRect)
if collision is not None:
return True
elif p.color in BLUE_TEAM:
for pl in players:
if pl.color in RED_TEAM:
for piece in pl.pieces:
collision = piece.click_check(tileRect)
if collision is not None:
return True
return False
def possible_moves(selected, board, players):
"""Finds all possible moves for the selected piece"""
moves = []
x = selected.x
y = selected.y
p = None
for pl in players:
if pl.color == selected.color:
p = pl
if p is None:
print 'You are not a player in the game'
pygame.event.post(Event(QUIT))
# Append the 4 basic move directions to the list of moves
if board.is_legal(x-1, y) and not(is_occupied(p, board, x-1, y, players)):
if selected.type in ['B', 'F']:
if not(is_occupied_excluding(p, players, board, x-1, y)):
moves.append((x-1,y))
else:
moves.append((x-1,y))
if board.is_legal(x, y-1) and not(is_occupied(p, board, x, y-1, players)):
if selected.type in ['B', 'F']:
if not(is_occupied_excluding(p, players, board, x, y-1)):
moves.append((x,y-1))
else:
moves.append((x,y-1))
if board.is_legal(x+1, y) and not(is_occupied(p, board, x+1, y, players)):
if selected.type in ['B', 'F']:
if not(is_occupied_excluding(p, players, board, x+1, y)):
moves.append((x+1,y))
else:
moves.append((x+1,y))
if board.is_legal(x, y+1) and not(is_occupied(p, board, x, y+1, players)):
if selected.type in ['B', 'F']:
if not(is_occupied_excluding(p, players, board, x, y+1)):
moves.append((x,y+1))
else:
moves.append((x,y+1))
# Append special moves for 2's, 2's can move any distance in a straight line
if selected.type is '2':
if board.is_legal(x+1, y) and not(is_occupied_excluding(p, players, board, x+1, y)) and not(is_occupied(p, board, x+1,y, players)):
for i in range(x+2, BOARD_SIZE):
if board.is_legal(i, y) and not(is_occupied(p, board, i, y, players)):
moves.append((i,y))
if is_occupied_excluding(p, players, board, i, y):
break
else:
break
if board.is_legal(x-1, y) and not(is_occupied_excluding(p, players, board, x-1, y)) and not(is_occupied(p, board,x-1, y, players)):
for i in range(x-2, -1, -1):
if board.is_legal(i,y) and not(is_occupied(p, board, i, y, players)):
moves.append((i,y))
if is_occupied_excluding(p, players, board, i, y):
break
else:
break
if board.is_legal(x, y+1) and not(is_occupied_excluding(p, players, board, x, y+1)) and not(is_occupied(p, board, x, y+1, players)):
for i in range(y+2, BOARD_SIZE):
if board.is_legal(x,i) and not(is_occupied(p, board, x, i, players)):
moves.append((x,i))
if is_occupied_excluding(p, players, board, x, i):
break
else:
break
if board.is_legal(x, y-1) and not(is_occupied_excluding(p, players, board, x, y-1)) and not(is_occupied(p, board, x, y-1, players)):
for i in range(y-2, -1, -1):
if board.is_legal(x,i) and not(is_occupied(p, board, x, i, players)):
moves.append((x,i))
if is_occupied_excluding(p, players, board, x, i):
break
else:
break
# Append special moves for 6's, 6's can move 2 squares in any direction
elif selected.type is '6':
extraMoves = []
for i,j in moves:
if is_occupied_excluding(p, players, board, i, j) or is_occupied(p, board, i, j, players):
continue
if (i-1 is not x or j is not y) and board.is_legal(i-1, j) and not(is_occupied(p, board, i-1, j, players)):
if extraMoves.count((i-1,j)) is 0:
extraMoves.append((i-1,j))
if (i is not x or j-1 is not y) and board.is_legal(i, j-1) and not(is_occupied(p, board, i, j-1, players)):
if extraMoves.count((i,j-1)) is 0:
extraMoves.append((i,j-1))
if (i+1 is not x or j is not y) and board.is_legal(i+1, j) and not(is_occupied(p, board, i+1, j, players)):
if extraMoves.count((i+1,j)) is 0:
extraMoves.append((i+1,j))
if (i is not x or j+1 is not y) and board.is_legal(i, j+1) and not(is_occupied(p, board, i, j+1, players)):
if extraMoves.count((i,j+1)) is 0:
extraMoves.append((i, j+1))
for move in extraMoves:
moves.append(move)
# Append special moves for bombs, bombs can shoot over 2 squares if the 3rd is occupied by an enemy piece
elif selected.type is 'B':
for i in range(x+1, x+4):
if not (board.is_legal(i,y) or board.is_legal_lake(i,y)):
break
if not(is_occupied_excluding(p, players, board, i, y)) and not(is_occupied(p, board, i,y, players)):
continue
elif is_occupied_excluding(p, players, board, i, y) and i == x+3:
moves.append((i,y))
else:
break
for i in range(x-1, x-4, -1):
if not(board.is_legal(i,y) or board.is_legal_lake(i,y)):
break
if not(is_occupied_excluding(p, players, board, i, y)) and not(is_occupied(p, board, i,y, players)):
continue
elif is_occupied_excluding(p, players, board, i, y) and i == x-3:
moves.append((i,y))
else:
break
for i in range(y+1, y+4):
if not(board.is_legal(x,i) or board.is_legal_lake(x,i)):
break
if not(is_occupied_excluding(p, players, board, x, i)) and not(is_occupied(p, board, x,i, players)):
continue
elif is_occupied_excluding(p, players, board, x, i) and i == y+3:
moves.append((x,i))
else:
break
for i in range(y-1, y-4, -1):
if not(board.is_legal(x,i) or board.is_legal_lake(x,i)):
break
if not(is_occupied_excluding(p, players, board, x, i)) and not(is_occupied(p, board, x,i, players)):
continue
elif is_occupied_excluding(p, players, board, x, i) and i == y-3:
moves.append((x,i))
else:
break
# Remove any moves that would move onto friendly pieces
toRemove = []
for i,j in moves:
tileRect = board.tiles[i][j].rect
for piece in p.pieces:
if piece.click_check(tileRect) is not None:
toRemove.append((i,j))
for pos in toRemove:
moves.remove(pos)
return moves
def getPos(x,y):
"""Converts indices to pixel coordinates on the board"""
i = BOARD_OFFSET_X + TILE_SIZE * x
j = BOARD_OFFSET_Y + TILE_SIZE * y
return (i,j)
def get_color_id(color):
"""Returns the id number of the given team color"""
if color == 'red':
return 0
if color == 'blue':
return 1
if color == 'dred':
return 2
if color == 'dblue':
return 3
def render_nick(font, nick, color):
"""Renders the given nickname into a pygame font object to be drawn to the screen"""
if color in ['red', 'dred']:
nick_text = font.render(nick, 1, (90, 10, 10))
if color in ['blue', 'dblue']:
nick_text = font.render(nick, 1, (10, 10, 90))
if color == 'red':
nick_pos = nick_text.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT - (BOARD_OFFSET_Y - 65)))
elif color == 'blue':
nick_pos = nick_text.get_rect(right=BOARD_OFFSET_X - 60, centery=SCREEN_HEIGHT/2)
elif color == 'dred':
nick_pos = nick_text.get_rect(center=(SCREEN_WIDTH/2, BOARD_OFFSET_Y - 65))
elif color == 'dblue':
nick_pos = nick_text.get_rect(left=SCREEN_WIDTH - (BOARD_OFFSET_X - 60), centery=SCREEN_HEIGHT/2)
return nick_text, nick_pos
def load_image(file_name, colorkey=None):
"""Loads image with file name: file_name, if colorkey is specified
all pixels that are the same color as the specified colorkey will be
rendered as invisible
From Pygame tutorial"""
full_name = os.path.join('assets', file_name)
try:
image = pygame.image.load(full_name)
except pygame.error, message:
print 'Cannot load image:', full_name
raise SystemExit, message
# Converts image to SDL's internal format, increases blit speed
image = image.convert()
if colorkey is not None:
# If specefied colorkey is -1, the color of the top left pixel is used
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
# Loads sound with file name: file_name
def load_sound(file_name):
"""Loads sound with file name: file_name
From Pygame Tutorial"""
# Dummy Class to return on error
class No_Sound:
def play(self): pass
# If mixer was imported or initialized incorrectly, return dummy class
if not pygame.mixer or not pygame.mixer.get_init():
return No_Sound()
full_name = os.path.join('assets', file_name)
if os.path.exists(full_name) == False:
sound = pygame.mixer.Sound(full_name)
else:
print 'File does not exist:', full_name
return No_Sound
return sound