-
Notifications
You must be signed in to change notification settings - Fork 1
/
anagrams_network.py
580 lines (444 loc) · 20.4 KB
/
anagrams_network.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
import itertools
import random
import twl
from collections import Counter
import root
import time
from network import Network
import ast
import datetime
import api
import pygame, sys
from pygame.locals import *
print_check = True
FPS = 30
WINDOWWIDTH = 640
WINDOWHEIGHT = 640
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
NAVYBLUE = (60, 60, 100)
BGCOLOR = WHITE
TEXTCOLOR = BLACK
print_check = False
letters = ['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', 'Z']
letter_freq = {'A': 13, 'B': 3, 'C': 3, 'D': 6, 'E': 18, 'F': 3, 'G': 4, 'H':3, 'I':12, 'J':2, 'K':2, 'L':5, 'M':3, 'N':8, 'O':11, 'P':3, 'Q':2, 'R':9, 'S':6, 'T':9, 'U': 6, 'V':3, 'W':3, 'X':2, 'Y':3, 'Z':2}
letter_keys = [K_a, K_b, K_c, K_d, K_e, K_d, K_e, K_f, K_g, K_h, K_i, K_j, K_k, K_l, K_m, K_n, K_o, K_p, K_q, K_r, K_s, K_t, K_u, K_t, K_v, K_w, K_x, K_y, K_z]
# ________
# | LAYOUT |
# --------
# 'Current'
font_current = 'freesansbold.ttf'
size_current = 32
color_current = BLACK
x_current = 10
y_current = 20
# Tiles
font_tile = 'freesansbold.ttf'
size_tile = 32
color_tile = BLACK
x_tile_0 = 150
y_tile_0 = 20
x_gap_tile = 30
y_gap_tile = 50
# 'Your Words'
font_your = 'freesansbold.ttf'
size_your = 32
color_your = BLACK
x_your = 10
y_your = 100
y_gap_your = 50
# Your Words
font_words = 'freesansbold.ttf'
size_words = 32
color_words = BLACK
x_words = 10
y_words = y_your + y_gap_your
x_gap_words = 100
y_gap_words = 50
# 'Opponent's Words'
font_opp = 'freesansbold.ttf'
size_opp = 32
color_opp = BLACK
x_opp = 300
y_opp = 100
y_gap_opp = 50
# Opponent's Words
font_opp_words = 'freesansbold.ttf'
size_opp_words = 32
color_opp_words = BLACK
x_opp_words = 300
y_opp_words = y_opp + y_gap_opp
x_gap_opp_words = 100
y_gap_opp_words = 50
# Guess
font_guess = 'freesansbold.ttf'
size_guess = 32
color_guess = BLACK
x_guess = 10
y_guess = 400
# Status
font_status = 'freesansbold.ttf'
size_status = 32
color_status = BLACK
x_status = 10
y_status = 500
class banana(object):
def __init__(self):
# -------- #
# BANANA #
# -------- #
self.tiles = []
for letter in letters:
self.tiles = self.tiles + list(itertools.repeat(letter, letter_freq[letter]))
random.shuffle(self.tiles)
self.current = []
self.playerwords = {}
self.playerwords_list = []
self.fontObj_current = pygame.font.Font(font_current, size_current)
self.fontObj_tile = pygame.font.Font(font_tile, size_tile)
self.fontObj_your = pygame.font.Font(font_your, size_your)
self.fontObj_words = pygame.font.Font(font_words, size_words)
self.fontObj_guess = pygame.font.Font(font_guess, size_guess)
self.fontObj_status = pygame.font.Font(font_status, size_status)
self.fontObj_opp = pygame.font.Font(font_your, size_your)
self.fontObj_opp_words = pygame.font.Font(font_words, size_words)
self.guess = ''
self.previous_guess = ''
self.fresh_take = False
self.middle_used = []
self.pre_take_word = ''
self.status = ''
self.last_update = datetime.datetime(1,1,1)
self.net = Network()
self.player2current = []
self.player2words = {}
self.player2words_list = []
def send_data(self):
data = str(self.net.id) + "|" + str(self.current) + "|" + str(self.playerwords) + "|" + str(self.playerwords_list) + "|" + str(self.player2words) + "|" + str(self.player2words_list) + "|" + str(self.last_update)
reply = self.net.send(data)
return reply
@staticmethod
def parse_data(data):
try:
split = data.split('|')
current = ast.literal_eval(split[1])
player2words = ast.literal_eval(split[2])
player2words_list = ast.literal_eval(split[3])
playerwords = ast.literal_eval(split[4])
playerwords_list = ast.literal_eval(split[5])
last_update = datetime.datetime.strptime(split[6], '%Y-%m-%d %H:%M:%S.%f')
return current, player2words, player2words_list, playerwords, playerwords_list, last_update
except:
return None, {}, [], None, None, datetime.datetime(1,1,1,0,0)
def flip(self):
self.updated = True
if not self.tiles:
self.status = 'Banana is empty!'
self.__display_text(self.status, x_status, y_status, self.fontObj_status, color_status)
return None
self.last_update = datetime.datetime.now()
last = self.tiles.pop()
self.current.append(last)
def current(self):
print(self.current)
def __superset(self, word1, word2, strict = False):
# Can word 1 take word 2?
word1_counter = Counter(word1)
word2_counter = Counter(word2)
if strict:
return (word1_counter - word2_counter and not (word2_counter - word1_counter))
else:
return (word1_counter - word2_counter and not(word2_counter - word1_counter)) or (not (word1_counter - word2_counter) and not (word2_counter - word1_counter))
def __subtract(self, word1, word2):
# Subtract word2 from word1 (e.g. 'test' - 'tst' = 'e')
list1 = list(word1)
for letter in word2:
list1.remove(letter)
# Turn list into string
str = ''
str = str.join(list1)
return str
def __display_text(self, text, x, y, fontObj, color):
textSurfaceObj = fontObj.render(text, True, color)
textRectObj = textSurfaceObj.get_rect()
textRectObj.topleft = (x, y)
DISPLAYSURF.blit(textSurfaceObj, textRectObj)
def __check_steal(self, candidate, merriam_candidate, word_dict, is_opponent):
# Check whether a steal happens
# Input a candidate word (i.e. guess), its merriam stripped version,
# a dictionary with the words to take from (plus their merriam stripped versions) and
# a Boolean indicating whether we're checking a steal from the opponent
# Returns whether stolen, what kind of steal or error, the taken word, and index of taken word
# Set event_type to 'tiles' arbitrarily
event_type = 'tiles'
for i, word in enumerate(word_dict):
# First, check if candidate is a superset of the current word
if self.__superset(candidate, word, strict=True):
# Then, check if the tiles needed to make candidate are in the middle
if not self.__superset(self.current, self.__subtract(candidate, word)):
event_type = 'tiles'
else:
merriam_word = word_dict[word]
# Look up the etymologies of both candidate and current word
root_candidate = root.lookup(merriam_candidate)
root_word = root.lookup(merriam_word)
# merriam_root_check = root.merriam_root_check(candidate, word)
try:
# Check for any common roots. If etymonline returns nothing, then assume there are no
# common roots
root_overlap = any(x in root_candidate for x in root_word)
except TypeError:
root_overlap = False
# If either one has no roots, we can't do the root comparison, so just check if their
# merriam-stripped versions are the same (plan B check).
if (root_candidate is None or root_word is None) and (merriam_candidate == merriam_word):
event_type = 'trivial'
elif root_overlap or (merriam_candidate == merriam_word):
event_type = 'trivial'
else:
event_type = 'steal'
taken_word = word
taken_i = i
return True, event_type, taken_word, taken_i
if not is_opponent and self.__superset(self.current, candidate, strict=False):
# This is to check middle steals. We have "is_opponent" because we're running this current __check_steal
# function twice, once for the opponent's words and once for your words, and we don't want it to check
# for a middle steal twice. So we say that in the first go-through (opponent's words), don't check middle
event_type = 'middle'
return True, event_type, None, None
else:
return False, event_type, None, None
# elif word in candidate and any(x in root.lookup(candidate) for x in root.lookup(word)):
# error_trivial_extension = True
# If all of those check out, then it's a steal and record which word is being stolen
def take(self, candidate):
# First check if has 3 letters
if len(candidate) < 3:
self.previous_guess = self.guess
self.status = "Word is too short! " + f"({self.previous_guess})"
self.guess = ''
return None
# Then check if a word
if len(candidate) < 10:
is_word = twl.check(candidate.lower())
else:
is_word = root.merriam_word_check(candidate)
if not is_word:
# self.__display_text("Not a word!", 200, 400)
self.previous_guess = self.guess
self.status = "Not a word! " + f"({self.previous_guess})"
self.guess = ''
return None
error_trivial_extension = False
error_tiles = False
# Get the merriam stripped version of the guess (e.g. TRAINED --> TRAIN)
merriam_candidate = root.merriam_strip(candidate)
# Check if can take the other player's words (not checking middle steal)
is_taken, event_type, taken_word, taken_i = self.__check_steal(candidate, merriam_candidate, self.player2words, True)
if is_taken:
# Get time of this steal
self.last_update = datetime.datetime.now()
if event_type == 'steal': # in theory, this if statement is unnecessary since there are no middle steals
# Make the candidate word into a list to remove individual letters.
candidate_list = list(candidate)
# Delete the taken word from opponent's list, add it to your own dictionary and corresponding list (list for ordering purposes)
del self.player2words[taken_word]
self.player2words_list.remove(taken_word)
self.playerwords.update({candidate: merriam_candidate})
self.playerwords_list.append(candidate)
# Figure out what tiles are used from the middle and remove those from self.current
for letter in taken_word:
candidate_list.remove(letter)
for letter in candidate_list:
self.current.remove(letter)
self.previous_guess = self.guess
self.status = "Success! " + f"({self.previous_guess})"
self.fresh_take = True
self.pre_take_word = taken_word
self.middle_used = candidate_list
else:
# If no steal was triggered above, then couldn't steal opponent's words for one of the reasons below
# (wait until you check whether you can take one of your own words to see if it was a failure overall)
if event_type == 'trivial':
error_trivial_extension = True
elif event_type == 'tiles':
error_tiles = True
# if could not take the other player's words, check if can take one's own
if not is_taken:
self_is_taken, event_type, taken_word, taken_i = self.__check_steal(candidate, merriam_candidate, self.playerwords, False)
if self_is_taken:
self.last_update = datetime.datetime.now()
self.updated = True
if event_type == 'steal':
candidate_list = list(candidate)
del self.playerwords[taken_word]
self.playerwords.update({candidate: merriam_candidate})
self.playerwords_list[taken_i] = candidate
for letter in taken_word:
candidate_list.remove(letter)
for letter in candidate_list:
self.current.remove(letter)
self.previous_guess = self.guess
self.status = "Success! " + f"({self.previous_guess})"
self.fresh_take = True
self.pre_take_word = taken_word
self.middle_used = candidate_list
elif event_type == 'middle':
candidate_list = list(candidate)
for letter in candidate_list:
self.current.remove(letter)
self.playerwords.update({candidate: merriam_candidate})
self.playerwords_list.append(candidate)
self.previous_guess = self.guess
self.status = "Success! " + f"({self.previous_guess})"
self.fresh_take = True
self.pre_take_word = taken_word
self.middle_used = candidate_list
elif error_trivial_extension:
self.previous_guess = self.guess
self.status = "Same root! " + f"({self.previous_guess})"
elif error_tiles:
self.previous_guess = self.guess
self.status = "Tiles aren't there! " + f"({self.previous_guess})"
else:
self.previous_guess = self.guess
self.status = "Tiles aren't there! " + f"({self.previous_guess})"
self.guess = ''
def printstatus(self):
# Send network stuff, outputs of this function are the stuff you receive from the other player
self.player2current, player2words_recv, player2words_list_recv, playerwords_recv, playerwords_list_recv, self.player2_last_update = self.parse_data(self.send_data())
# If it's a fresh take, check to see if the opponent's taken it first
"""
if self.fresh_take:
# Check if center tiles and taken word are still there:
if not self.__superset(self.player2current, self.middle_used, strict=False) or (not self.pre_take_word in player2words_list_recv and not self.pre_take_word in playerwords_list_recv):
print("OVERWRITING!!!!")
self.current = self.player2current
self.playerwords = playerwords_recv
self.playerwords_list = playerwords_list_recv
self.last_update = self.player2_last_update
self.player2words = player2words_recv
self.player2words_list = player2words_list_recv
# Reset self.fresh_take
self.fresh_take = False
"""
# Check if other player has made a more recent update, meaning you would need to update your lists
if self.player2_last_update > self.last_update:
if print_check:
print("UPDATING")
print(f"Old Words: {self.playerwords}")
print(f"New Words: {playerwords_recv}")
self.current = self.player2current
self.playerwords = playerwords_recv
self.playerwords_list = playerwords_list_recv
self.last_update = self.player2_last_update
self.player2words = player2words_recv
self.player2words_list = player2words_list_recv
y_tile = y_tile_0
x_tile = x_tile_0
self.__display_text('Current: ', x_current, y_current, self.fontObj_current, color_current)
"""
currentSurfaceObj = self.fontObj.render('Current:', True, BLACK)
currentRectObj = currentSurfaceObj.get_rect()
currentRectObj.topleft = (x_current, y_current)
DISPLAYSURF.blit(currentSurfaceObj, currentRectObj)
"""
for i, tile in enumerate(self.current):
x_tile = x_tile + x_gap_tile
self.__display_text(tile, x_tile, y_tile, self.fontObj_tile, color_tile)
"""
tileSurfaceObj = self.fontObj.render(tile, True, BLACK)
tileRectObj = tileSurfaceObj.get_rect()
tileRectObj.topleft = (x_tile, y_tile)
DISPLAYSURF.blit(tileSurfaceObj, tileRectObj)
"""
if i % 10 == 9:
y_tile = y_tile + y_gap_tile
x_tile = x_tile_0
self.__display_text('Your Words: ', x_your, y_your, self.fontObj_your, color_your)
"""
wordsSurfaceObj = self.fontObj.render('Your Words:', True, BLACK)
wordsRectObj = wordsSurfaceObj.get_rect()
wordsRectObj.topleft = (x_words, y_words)
DISPLAYSURF.blit(wordsSurfaceObj, wordsRectObj)
"""
x_words_local = x_words
y_words_local = y_words
for i, word in enumerate(self.playerwords_list):
self.__display_text(word, x_words_local, y_words_local, self.fontObj_words, color_words)
"""
wordSurfaceObj = self.fontObj.render(word, True, BLACK)
wordRectObj = wordSurfaceObj.get_rect()
wordRectObj.topleft = (x_words, y_words)
DISPLAYSURF.blit(wordSurfaceObj, wordRectObj)
"""
if i % 10 == 9:
x_words_local = x_words_local + x_gap_words
y_words_local = y_words_local + y_gap_words
self.__display_text('Take: ' + self.guess, x_guess, y_guess, self.fontObj_guess, color_guess)
"""
guessSurfaceObj = self.fontObj.render('Guess: ' + self.guess, True, BLACK)
guessRectObj = guessSurfaceObj.get_rect()
guessRectObj.topleft = (x_guess, y_guess)
DISPLAYSURF.blit(guessSurfaceObj, guessRectObj)
"""
self.__display_text('Opponent\'s Words: ', x_opp, y_opp, self.fontObj_opp, color_opp)
"""
wordsSurfaceObj = self.fontObj.render('Your Words:', True, BLACK)
wordsRectObj = wordsSurfaceObj.get_rect()
wordsRectObj.topleft = (x_words, y_words)
DISPLAYSURF.blit(wordsSurfaceObj, wordsRectObj)
"""
x_opp_words_local = x_opp_words
y_opp_words_local = y_opp_words
for i, word in enumerate(self.player2words_list):
self.__display_text(word, x_opp_words_local, y_opp_words_local, self.fontObj_opp_words, color_opp_words)
"""
wordSurfaceObj = self.fontObj.render(word, True, BLACK)
wordRectObj = wordSurfaceObj.get_rect()
wordRectObj.topleft = (x_words, y_words)
DISPLAYSURF.blit(wordSurfaceObj, wordRectObj)
"""
if i % 10 == 9:
x_opp_words_local = x_opp_words_local + x_gap_opp_words
y_opp_words_local = y_opp_words_local + y_gap_opp_words
self.__display_text(self.status, x_status, y_status, self.fontObj_status, color_status)
def main():
# Main game loop
global FPSCLOCK, DISPLAYSURF
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Anagrams')
game = banana()
while True: # main game loop
DISPLAYSURF.fill(BGCOLOR)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_BACKSPACE:
if game.guess == '':
pass
else:
# Delete one letter from the guess
game.guess = game.guess[:-1]
elif event.key == K_SPACE:
# Don't do anything if input is a space
pass
elif event.key == K_RETURN:
# if Return and no guess is present, then flip next tile. If guess is present, see if it's a take
if game.guess == '':
game.flip()
else:
game.take(game.guess.upper())
elif event.key in letter_keys:
# if letter is typed then add it to the current guess
game.guess = game.guess + event.unicode.upper()
game.printstatus()
pygame.display.update()
if __name__ == "__main__":
# execute only if run as a script
main()