-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
493 lines (352 loc) · 13.2 KB
/
Game.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
from Team import Team
import warnings, random
from VocabList import VocabList
import random, os, socket, sys, pickle
import pygame
import tkinter
import tkinter.filedialog
import matplotlib.pyplot as plt
import pandas as pd
root = tkinter.Tk()
root.withdraw()
pygame.init()
## get size of an
def sizeof(obj):
size = sys.getsizeof(obj)
if isinstance(obj, dict): return size + sum(map(sizeof, obj.keys())) + sum(map(sizeof, obj.values()))
if isinstance(obj, (list, tuple, set, frozenset)): return size + sum(map(sizeof, obj))
return size
class Game:
KEYS = ('PROPERTY', 'BOOL_GUESS', 'WORD')
PROPERTIES = ('BLUE', 'RED', 'BLANK', 'MINE')
TEAMS = ('BLUE', 'RED')
PROPERTY_COLOR_DICT = {'BLUE': "#9FE9EE",
'RED': "#EE9F9F",
'BLANK': 'W',
'MINE': '#DADADA'}
VOCAB_DIR = './VocabList/'
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
DARK_GREEN = (0, 128, 0)
DARK_RED = (128, 0, 0)
DARK_BLUE = (0, 0, 128)
BEIGE = (255, 243, 202)
LIGHT_GREY = (220, 220, 220)
BLOCK_X_SIZE = 250
BLOCK_Y_SIZE = 120
SPACE_SIZE = 10
SCREEN_X_SIZE = 1500
SCREEN_Y_SIZE = 1000
KEYS_ORDER = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y']
KEYS_DICT = {'A': pygame.K_a,
'B': pygame.K_b,
'C': pygame.K_c,
'D': pygame.K_d,
'E': pygame.K_e,
'F': pygame.K_f,
'G': pygame.K_g,
'H': pygame.K_h,
'I': pygame.K_i,
'J': pygame.K_j,
'K': pygame.K_k,
'L': pygame.K_l,
'M': pygame.K_m,
'N': pygame.K_n,
'O': pygame.K_o,
'P': pygame.K_p,
'Q': pygame.K_q,
'R': pygame.K_r,
'S': pygame.K_s,
'T': pygame.K_t,
'U': pygame.K_u,
'V': pygame.K_v,
'W': pygame.K_w,
'X': pygame.K_x,
'Y': pygame.K_y}
KEY_LABEL_FONT = pygame.font.Font('freesansbold.ttf', 18)
SCORE_FONT = pygame.font.Font('freesansbold.ttf', 32)
WORD_FONT = pygame.font.Font('../fonts/NotoSerifSC-Bold.otf', 28)
HINT_FONT = pygame.font.Font('../fonts/MaShanZheng-Regular.ttf', 30)
HELP_FONT = pygame.font.Font('freesansbold.ttf', 18)
def __init__(self, spymaster=False):
self.team_red = Team('RED')
self.team_blue = Team('BLUE')
self.set_empty_board()
self._vocab = VocabList()
self.vocab_list = []
## pygame display
self.size = [Game.SCREEN_X_SIZE, Game.SCREEN_Y_SIZE]
self.screen = pygame.display.set_mode(self.size)
self.set_rect()
self.screen.fill(Game.WHITE)
pygame.display.set_caption("Codename")
self.host_socket = None
self.client_socket = None
self.spymaster = spymaster
self.team_dict = {'RED': self.team_red, 'BLUE': self.team_blue}
self.round_counter = 0
def set_socket(self, host_socket, client_socket):
self.host_socket = host_socket
self.client_socket = client_socket
def add_vocab_doc_to_vocab_list(self, doc):
success_flag = self._vocab.add_vocab_doc(doc)
self.vocab_list = list(self._vocab.vocab_list)
random.shuffle(self.vocab_list)
return success_flag
def set_empty_board(self):
self.board = []
self.simple_board = []
for i in range(5):
self.board.append([])
self.simple_board.append([])
for j in range(5):
board_ij = {k:None for k in Game.KEYS}
self.board[i].append(board_ij)
self.simple_board[i].append(board_ij.copy())
def reset_vocab_list(self):
self.vocab_list = list(self._vocab.vocab_list)
random.shuffle(self.vocab_list)
def reset_board(self):
if len(self.vocab_list) < 25:
self.load_vocab(msg='Not enough words loaded.')
#self.reset_vocab_list()
teams = list(Game.TEAMS)
random.shuffle(teams)
team_words = random.sample(list(range(25)), 18)
self.team_dict[teams[0]].set_current_word_count(9)
self.team_dict[teams[1]].set_current_word_count(8)
# clear board
for i in range(5):
for j in range(5):
self.board[i][j]['PROPERTY'] = 'BLANK'
for i in range(18):
if i < 9:
self.board[team_words[i]//5][team_words[i]%5]['PROPERTY'] = teams[0]
elif i < 17:
self.board[team_words[i]//5][team_words[i]%5]['PROPERTY'] = teams[1]
else:
self.board[team_words[i]//5][team_words[i]%5]['PROPERTY'] = 'MINE'
for i in range(5):
for j in range(5):
self.board[i][j]['BOOL_GUESS'] = False
self.board[i][j]['WORD'] = self.vocab_list.pop()
self.simple_board[i][j]['PROPERTY'] = self.board[i][j]['PROPERTY']
self.simple_board[i][j]['WORD'] = self.board[i][j]['WORD']
self.simple_board[i][j]['BOOL_GUESS'] = self.board[i][j]['BOOL_GUESS']
self.print_board_by_key('WORD')
print('')
self.print_board_by_key('PROPERTY')
def plot_current_board(self):
words_df = self.get_board_words_as_df()
color_list = self.get_board_colors()
fig, ax = plt.subplots()
# hide axes
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')
ax.table(cellText=words_df.values, loc='center', cellColours=color_list)
fig.tight_layout()
plt.show()
return
def get_board_colors(self):
color_list = []
for i in range(5):
color_list.append([])
for j in range(5):
color_list[i].append(Game.PROPERTY_COLOR_DICT[self.board[i][j]['PROPERTY']])
return color_list
def get_board_words_as_df(self):
word_list = []
for i in range(5):
word_list.append([])
for j in range(5):
word_list[i].append(self.board[i][j]['WORD'])
return pd.DataFrame(word_list)
def set_simple_board(self, new_board):
self.simple_board = new_board
for i in range(5):
for j in range(5):
self.board[i][j]['PROPERTY'] = self.simple_board[i][j]['PROPERTY']
self.board[i][j]['WORD'] = self.simple_board[i][j]['WORD']
self.board[i][j]['BOOL_GUESS'] = self.simple_board[i][j]['BOOL_GUESS']
def set_rect(self):
boarder_x = (Game.SCREEN_X_SIZE - (Game.BLOCK_X_SIZE*5) - (Game.SPACE_SIZE*4))//2
boarder_y = (Game.SCREEN_Y_SIZE - (Game.BLOCK_Y_SIZE*5) - (Game.SPACE_SIZE*4))//2
for i in range(5):
for j in range(5):
rect = pygame.Rect(boarder_x+i*Game.BLOCK_X_SIZE+i*Game.SPACE_SIZE,
boarder_y+j*Game.BLOCK_Y_SIZE+j*Game.SPACE_SIZE,
Game.BLOCK_X_SIZE, Game.BLOCK_Y_SIZE)
label = Game.KEYS_ORDER[i*5+j]
self.board[i][j]['LABEL'] = label
label_text = Game.KEY_LABEL_FONT.render(label, True, Game.BLACK)
label_position = (boarder_x+i*Game.BLOCK_X_SIZE+i*Game.SPACE_SIZE,
boarder_y+j*Game.BLOCK_Y_SIZE+j*Game.SPACE_SIZE)
self.board[i][j]['RECT'] = rect
self.board[i][j]['LABEL_TEXT'] = label_text
self.board[i][j]['LABEL_POSITION'] = label_position
self.board[i][j]['RECT_KEY'] = Game.KEYS_DICT[label]
def draw_board(self):
self.screen.fill(Game.WHITE)
if not self.spymaster:
red_score_text = Game.SCORE_FONT.render('{}'.format(self.team_red.current_word_count), True,
Game.DARK_RED)
blue_score_text = Game.SCORE_FONT.render('{}'.format(self.team_blue.current_word_count), True,
Game.DARK_BLUE)
score_mid_text = Game.SCORE_FONT.render('-', True, Game.BLACK)
self.screen.blit(red_score_text, (20, 20))
self.screen.blit(score_mid_text, (20+red_score_text.get_width()+2, 20))
self.screen.blit(blue_score_text, (20+red_score_text.get_width()+4+score_mid_text.get_width(), 20))
for i in range(5):
for j in range(5):
color = Game.LIGHT_GREY
font_color = Game.BLACK
if self.board[i][j]['BOOL_GUESS']:
if self.board[i][j]['PROPERTY'] == 'BLANK':
color = Game.BEIGE
elif self.board[i][j]['PROPERTY'] == 'RED':
color = Game.DARK_RED
font_color = Game.WHITE
elif self.board[i][j]['PROPERTY'] == 'BLUE':
color = Game.DARK_BLUE
font_color = Game.WHITE
elif self.board[i][j]['PROPERTY'] == 'MINE':
color = Game.BLACK
font_color = Game.WHITE
elif self.spymaster:
if self.board[i][j]['PROPERTY'] == 'RED':
font_color = Game.DARK_RED
elif self.board[i][j]['PROPERTY'] == 'BLUE':
font_color = Game.DARK_BLUE
elif self.board[i][j]['PROPERTY'] == 'MINE':
color = (150, 150, 150)
pygame.draw.rect(self.screen, color, self.board[i][j]['RECT'])
self.screen.blit(self.board[i][j]['LABEL_TEXT'],
self.board[i][j]['LABEL_POSITION'])
word_text = Game.WORD_FONT.render(self.board[i][j]['WORD'], True, font_color)
word_position = (self.board[i][j]['LABEL_POSITION'][0]+(Game.BLOCK_X_SIZE-word_text.get_width())//2,
self.board[i][j]['LABEL_POSITION'][1]+(Game.BLOCK_Y_SIZE-word_text.get_height())//2)
self.screen.blit(word_text, word_position)
## Display
pygame.display.update()
return
def play(self):
self.reset_board()
#self.plot_current_board()
ingame_flag = True
status_flag = True
while ingame_flag:
self.draw_board()
for event in pygame.event.get():
if event.type == pygame.QUIT: # Usually wise to be able to close your program.
msg = pickle.dumps(False)
self.client_socket.send(msg)
raise SystemExit
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
ingame_flag = False
elif event.key in Game.KEYS_DICT.values():
found = False
for i in range(5):
for j in range(5):
if self.board[i][j]['RECT_KEY'] == event.key:
status_flag = True
self.board[i][j]['BOOL_GUESS'] = True
self.simple_board[i][j]['BOOL_GUESS'] = True
if self.board[i][j]['PROPERTY'] == 'MINE':
ingame_flag = False
if self.board[i][j]['PROPERTY'] == 'RED':
self.team_red.decrease_current_word_count()
if self.team_red.current_word_count == 0:
ingame_flag = False
if self.board[i][j]['PROPERTY'] == 'BLUE':
self.team_blue.decrease_current_word_count()
if self.team_blue.current_word_count == 0:
ingame_flag = False
if status_flag:
msg = pickle.dumps(self.simple_board)
self.client_socket.send(msg)
status_flag = False
self.round_counter += 1
end_flag = False
continue_flag = False
while not end_flag:
self.screen.fill(Game.WHITE)
text = Game.HINT_FONT.render('Next round?', True, Game.BLACK)
self.screen.blit(text, ((self.size[0]-text.get_width())//2, (self.size[1]-text.get_height())//2))
output_string = 'Press [N] for next round. Press [ESC] to end game.'
help_text = Game.HELP_FONT.render(output_string, True, Game.BLACK)
self.screen.blit(help_text, ((self.size[0]-help_text.get_width())//2,
(self.size[1]+text.get_height())//2+20))
## Display
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT: # Usually wise to be able to close your program.
msg = pickle.dumps(False)
self.client_socket.send(msg)
raise SystemExit
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
msg = pickle.dumps(False)
self.client_socket.send(msg)
end_flag = True
elif event.key == pygame.K_n:
continue_flag = True
end_flag = True
if continue_flag and self.round_counter <= 25:
self.play()
return
def load_vocab_list_dialog(self):
try:
root.filename = tkinter.filedialog.askopenfilename(initialdir = Game.VOCAB_DIR,
title = "Select vocabulary lists",
filetypes = [("Text file",'*.txt')])
return self.add_vocab_doc_to_vocab_list(root.filename)
except Exception:
return False
def load_vocab(self, msg=''):
end_flag = False
success_flag = False
help_font = pygame.font.Font('freesansbold.ttf', 18)
vocab_font = pygame.font.Font('../fonts/MaShanZheng-Regular.ttf', 48)
msg = msg+' ' if msg else msg
while not end_flag:
self.screen.fill(Game.WHITE)
text = Game.HINT_FONT.render('{}Let\'s Load!'.format(msg), True, Game.BLACK)
self.screen.blit(text, ((self.size[0]-text.get_width())//2, (self.size[1]-text.get_height())//2))
help_text = Game.HELP_FONT.render('Press [L] to load vocabulary list. Press [P] to play.', True, Game.BLACK)
self.screen.blit(help_text, ((self.size[0]-help_text.get_width())//2,
(self.size[1]+text.get_height())//2+20))
help_text2 = Game.HELP_FONT.render('Number of successfully loaded vocabulary lists: {}'.format(self._vocab.get_number_of_lists()), True, Game.DARK_RED)
self.screen.blit(help_text2, ((self.size[0]-help_text2.get_width())//2,
(self.size[1]+text.get_height())//2+40+help_text.get_height()))
## Display
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT: # Usually wise to be able to close your program.
raise SystemExit
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_l:
success_flag = self.load_vocab_list_dialog()
elif event.key == pygame.K_p and self._vocab.get_number_of_lists() > 0:
end_flag = True
def print_board_by_key(self, key):
for i in range(5):
print_str = ''
for j in range(5):
print_str += self.board[i][j][key] + '\t\t'
print(print_str)
if __name__ == '__main__':
game = Game()
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(1) # Now wait for client connection.
c, addr = s.accept() # Establish connection with client.
print('Got connection from', addr)
game.set_socket(s, c)
#game.reset_board()
game.play()