-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordle-bot.py
714 lines (406 loc) · 18.1 KB
/
wordle-bot.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
# Version 2
import statistics
import random
# Global variables
guess_hash = dict()
answer_hash = dict()
filler_hash = dict()
yellow_letters = []
green_letters = []
def playGame():
print("\nWelcome to WordleBot 2.0!\n")
#Preprocess
global guess_hash
global filler_hash
global green_letters
global yellow_letters
# We will play the game as long as the user has not guessed the word and still has guesses left
play_game = True
guess_count = 0
while play_game == True:
if guess_count == 6:
print("\nRan out of guesses! You lose!\n")
play_game = False
break
else:
guess_count += 1
# Check if only one word left (User wins)
if len(answer_hash) == 1: # Check if user won
print("\nCongrats! The word was " + str(list(answer_hash)[0]) + "\nYou won in " + str(guess_count) + " guesses!\n")
play_game = False
elif len(answer_hash) == 0:
print("No words left! Is it possible you typed the results incorrectly?")
play_game = False
break
else:
if guess_count == 6:
print("\nCareful: Last Guess!")
normalize()
normalize_filler()
print("Possible Words left: " + str(len(answer_hash)))
choice = printChoice()
if choice == '9':
play_game = False
break
# Get guess
guess = getGuess()
# Once we have valid guess, we can then see how the user did
result_dict, got_word = getResults()
if got_word == True: # User guessed word right
print("\nCongrats! The word was " + str(list(answer_hash)[0]) + "\nYou won in " + str(guess_count) + " guesses!\n")
play_game = False
interpretResults(result_dict, guess)
def interpretResults(result_dict, guess):
duplicates = {}
global yellow_letters
global green_letters
"""
Keep track of how many occurrences in word of each letter
This method will be very helpful for examining duplicate scenarios
"""
index = 0
for letter in guess:
if letter not in duplicates:
# Create parent dict
duplicates[letter] = {}
if result_dict[index] == 'g':
duplicates[letter]["g"] = 1
duplicates[letter]["y"] = 0
green_letters.append(letter)
elif result_dict[index] == 'y':
duplicates[letter]["y"] = 1
duplicates[letter]["g"] = 0
yellow_letters.append(letter)
else:
duplicates[letter]["y"] = 0
duplicates[letter]["g"] = 0
else:
if result_dict[index] == 'g':
duplicates[letter]["g"] += 1
green_letters.append(letter)
elif result_dict[index] == 'y':
duplicates[letter]["y"] += 1
yellow_letters.append(letter)
index += 1
# Need to narrow down words using results
yellow = 0
green = 0
passed = 0
count = 0
# Use copy in order to make real time deletions
for word, weight in guess_hash.copy().items():
count += 1
for index, result in result_dict.copy().items(): # iterate over result, compare to word
# Green
if result == 'g' and guess[index] != word[index]:
del guess_hash[word]
green += 1
break
# Yellow
elif result == 'y':
letter = guess[index]
# three Scenarios:
# if word doesnt contain the yellow letter, delete
# if word has the yellow letter in same position, delete
# if word has fewer letters than exist yellow duplicates (ex if 2 yellow e's),
# but only one in word
if guess[index] not in word or guess[index] == word[index]:
del guess_hash[word]
yellow += 1
break
elif word.count(letter) < duplicates[letter]["y"]:
del guess_hash[word]
yellow += 1
break
# Grey
elif result == 'x':
# In wordle bot 2.0, we are using our duplicates dict to manage grey letters
# To accomplish, we simply examine the count of yellow + green in duplicates
# if the examined word has a different number, delete word
# Ex. no yellow or greens for "e", delete word if come across an e
# However, if 1 yellow and 1 green "e", delete word if only 1 exists in word
# Also delete if the examined word has the grey letter in same location
letter = guess[index]
total_occurrences = duplicates[letter]["g"] + duplicates[letter]["y"]
if total_occurrences != word.count(letter):
del guess_hash[word]
passed +=1
break
elif guess[index] == word[index]:
del guess_hash[word]
passed +=1
break
# FILLER
# Need to narrow down words using results
yellow = 0
green = 0
passed = 0
count = 0
# Use copy in order to make real time deletions
for word, weight in filler_hash.copy().items():
count += 1
for index, result in result_dict.copy().items(): # iterate over result, compare to word
# Grey
if result == 'x':
# For the filler word, we want to actually KEEP words with misaligned yellow and greens
# in order to optimize the best possible filler word
letter = guess[index]
total_occurrences = duplicates[letter]["g"] + duplicates[letter]["y"]
if total_occurrences != word.count(letter):
del filler_hash[word]
passed +=1
break
elif guess[index] == word[index]:
del filler_hash[word]
passed +=1
break
# Answer Hash
yellow = 0
green = 0
passed = 0
count = 0
# Use copy in order to make real time deletions
for word, weight in answer_hash.copy().items():
count += 1
for index, result in result_dict.copy().items(): # iterate over result, compare to word
# Green
if result == 'g' and guess[index] != word[index]:
del answer_hash[word]
green += 1
break
# Yellow
elif result == 'y':
letter = guess[index]
if guess[index] not in word or guess[index] == word[index]:
del answer_hash[word]
yellow += 1
break
elif word.count(letter) < duplicates[letter]["y"]:
del answer_hash[word]
yellow += 1
break
# Grey
elif result == 'x':
letter = guess[index]
total_occurrences = duplicates[letter]["g"] + duplicates[letter]["y"]
if total_occurrences != word.count(letter):
del answer_hash[word]
passed +=1
break
elif guess[index] == word[index]:
del answer_hash[word]
passed +=1
break
def getResults():
# This function is used for getting the results of a guess
results = dict()
index = 0
got_word = True
while index < 5:
result = input("\nEnter result of position " + str(index+1) + "(g,y,x):\n")
if result in ['g','y','x']:
results[index] = result
index += 1
# Check if user got word
if result == 'y' or result == 'x':
got_word = False
else:
print("\nError: result must be g, y, or x\n")
return results, got_word
def printChoice():
# Helper function that prints out user-generated choice
choice = getChoice()
if choice == "1":
print("\n" + str(list(guess_hash)[0]))
elif choice == "2":
if len(guess_hash) >= 5:
print("\n")
for i in range(5):
print(str(list(guess_hash)[i]))
else:
for word, weight in guess_hash.items():
print(word)
elif choice == "3":
if len(answer_hash) >= 5:
print("\n")
for i in range(5):
print(str(list(answer_hash)[i]))
else:
for word, weight in answer_hash.items():
print(word)
elif choice == "4":
for word, weight in guess_hash.items():
print(word)
elif choice == "5":
for word, weight in answer_hash.items():
print(word)
elif choice == "6":
rand_index = random.randint(0, len(guess_hash)-1)
print("\n" + str(list(guess_hash)[rand_index]))
elif choice =="7":
if len(filler_hash) > 0:
print("\n" + str(list(filler_hash)[0]))
else:
print("No Filler words at this time!")
elif choice == "8":
print("\n" + str(list(guess_hash)[len(guess_hash)-1]))
elif choice == "9":
print("Quitting . . . ")
return choice
def getGuess():
# Function to extract guess from user
while True:
guess = input("\nEnter Guess taken, or enter 'back' to return to main menu\n").upper()
if guess.lower() == "back":
printChoice()
# Important to check if in dictionary
elif guess in guess_hash or guess in filler_hash:
return guess
else:
print("\nError: Guess not found in our Dictionary\n")
def getChoice():
# Method to extract user choice for guessing
while True:
choice = input("\nChoose an option: \n\n0) Guess!\n1) Show optimal guess\n2) Show top 5 guesses\n3) Show top 5 possible answers\n4) Show all possible guesses\n5) Show all possible answers\n6) Random Guess\n7) Filler word\n8) Worst guess\n9) Quit\n")
if choice in ["0","1","2","3","4","5","6", "7", "8", "9"]:
return choice
print("Error: Choose valid option!")
def readDict():
global guess_hash # make hashtable have global scope
global filler_hash
global answer_hash
with open("5LetterDictionary.txt", "r") as dictionary:
# Will use words to optimize guesses / Narrow down list
for word in dictionary:
word = word.upper().replace("\n", "") # Get rid of whitespace
guess_hash[word] = 0 # Store in hashtable for easy look up, stop here
filler_hash[word] = 0
with open("5_letter_possible_answers.txt", "r") as possible_words:
# For each word, we need to evaluate its letter frequency and location
# in order to to perform statistical analysis
for word in possible_words:
word = word.upper().replace("\n", "") # Get rid of whitespace
answer_hash[word] = 0 # Store in hashtable for easy look up, stop here
def getStats():
# Letter_location Array: keep track of where letters appear most often in words
# Letter_distribution Array: overall count of most common - appearing letters
letter_location = [[0]*26 for i in range(5)]
letter_distribution = [0]*26
for word, weight in answer_hash.items():
weight = 0 # Reset Weight
index_of_word = 0 # Keep track of whrere in word we are
for letter in word: # Iterate each letter
char_index = ord(letter) - 65 # Use ASCII keys to index array
letter_location[index_of_word][char_index] += 1
letter_distribution[char_index] += 1
index_of_word += 1
return letter_location, letter_distribution
def normalize():
"""
In this method, we normalize our letter_location and letter_distribution values
using Z-Scores. We then calculate "Weights" for each word by summing up the Z-Scores
of their letter components. This ranking system will allow us to optimally guess on each
turn during the game. The weight will be comprised of two components:
1. The frequency of their letters
2. The location of their letters
"""
global guess_hash
global answer_hash
letter_location, letter_distribution = getStats()
stdev_distribution = statistics.stdev(letter_distribution) # stdev of distribution
mean_distribution = (len(answer_hash) * 5) / 26 # Mean for letter distribution
# Create z-scores for distribution
for i in range(len(letter_distribution)):
letter_distribution[i] = (letter_distribution[i] - mean_distribution) / stdev_distribution
# Create z-scores for location
for index in letter_location:
stdev_location = statistics.stdev(index)
mean_location = len(answer_hash) / 26
for i in range(len(index)):
index[i] = ((index[i] - mean_location) / stdev_location)
# Now that we have Z-Scores, we can re-iterate through the words and assign weights
for word, weight in answer_hash.items():
index_of_word = 0
prev_letters = dict()
weight = 0
for letter in word:
char_index = ord(letter) - 65 # Get index
if letter in prev_letters: # punish duplicates, bad guesses
weight -= abs(letter_distribution[char_index] + letter_location[index_of_word][char_index])
else:
weight += letter_distribution[char_index] + letter_location[index_of_word][char_index]
index_of_word += 1
prev_letters[letter] = letter
answer_hash[word] = weight
# Sort dictionary by weight in Descending order for optimal guesses
answer_hash = dict(sorted(answer_hash.items(), key=lambda item: item[1], reverse = True))
# DO SAME FOR GUESS HASH
for word, weight in guess_hash.items():
index_of_word = 0
prev_letters = dict()
weight = 0
for letter in word:
char_index = ord(letter) - 65 # Get index
if letter in prev_letters: # punish duplicates, bad guesses
weight -= abs(letter_distribution[char_index] + letter_location[index_of_word][char_index])
else:
weight += letter_distribution[char_index] + letter_location[index_of_word][char_index]
index_of_word += 1
prev_letters[letter] = letter
guess_hash[word] = weight
# Sort dictionary by weight in Descending order for optimal guesses
guess_hash = dict(sorted(guess_hash.items(), key=lambda item: item[1], reverse = True))
"""
FILLER METHODS
"""
def fillerStats():
letter_location = [[0]*26 for i in range(5)]
letter_distribution = [0]*26
for word, weight in filler_hash.items():
weight = 0 # Reset Weight
index_of_word = 0 # Keep track of whrere in word we are
for letter in word: # Iterate each letter
char_index = ord(letter) - 65 # Use ASCII keys to index array
letter_location[index_of_word][char_index] += 1
letter_distribution[char_index] += 1
index_of_word += 1
return letter_location, letter_distribution
def normalize_filler():
global filler_hash
global green_letters
global yellow_letters
letter_location, letter_distribution = fillerStats()
stdev_distribution = statistics.stdev(letter_distribution) # stdev of distribution
mean_distribution = (len(filler_hash) * 5) / 26 # Mean for letter distribution
# Create z-scores for distribution
for i in range(len(letter_distribution)):
letter_distribution[i] = (letter_distribution[i] - mean_distribution) / stdev_distribution
# Create z-scores for location
for index in letter_location:
stdev_location = statistics.stdev(index)
mean_location = len(filler_hash) / 26
for i in range(len(index)):
index[i] = ((index[i] - mean_location) / stdev_location)
# Now that we have Z-Scores, we can re-iterate through the words and assign weights
for word, weight in filler_hash.items():
index_of_word = 0
prev_letters = dict()
weight = 0
for letter in word:
char_index = ord(letter) - 65 # Get index
if letter in prev_letters: # punish duplicates, bad guesses
weight -= abs(letter_distribution[char_index] + letter_location[index_of_word][char_index])
elif letter in green_letters:
weight -= 2* abs(letter_distribution[char_index] + letter_location[index_of_word][char_index])
elif letter in yellow_letters:
weight -= 2 * abs(letter_distribution[char_index] + letter_location[index_of_word][char_index])
else:
weight += letter_distribution[char_index] + letter_location[index_of_word][char_index]
index_of_word += 1
prev_letters[letter] = letter
filler_hash[word] = weight
# Sort dictionary by weight in Descending order for optimal guesses
filler_hash = dict(sorted(filler_hash.items(), key=lambda item: item[1], reverse = True))
if __name__ == "__main__":
readDict()
playGame()