-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordleGame.py
52 lines (48 loc) · 2.13 KB
/
wordleGame.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
import wordsList
from entropyAnalysis import calculateWordEntropy, calculateInformation, calculateMatches
class WordleGame:
def __init__(self):
self.searchSpace = wordsList.words[:]
self.informationRemaining = calculateInformation(1.0/float(len(self.searchSpace)))
initialRecommendations = {}
with open('initial.txt', 'rb') as file:
for l in file:
parameters = l.decode('utf-8').split(' ')
initialRecommendations[parameters[0]] = float(parameters[1])
self.suggestions = initialRecommendations
self.writeSuggestions()
for i in range(6):
if len(self.suggestions) > 1: #More than 1 possible answer
self.enterGuess(i+1)
elif len(self.suggestions) == 1: #Only 1 answer
print(f"Answer must be: {list(self.suggestions.keys())[0].replace('ä', 'a*').replace('ö', 'o*').replace('ü', 'u*')}")
break
else:
print('Something went wrong!')
break
def enterGuess(self, i):
print(f'Info Remaining: {self.informationRemaining} bits')
print(f'{i}:')
wordGuess = input('Guess: ').lower()
patternInput = input('Pattern: ')
patternReturned = [int(i) for i in patternInput]
#Do new calculations
newSearchSpace = calculateMatches(wordGuess, self.searchSpace, patternReturned)
print(f'Expected Info: {self.suggestions[wordGuess]}')
print(f'Actual Info: {calculateInformation(float(len(newSearchSpace))/float(len(self.searchSpace)))}')
self.searchSpace = newSearchSpace
self.informationRemaining = calculateInformation(1.0/float(len(self.searchSpace)))
self.suggestions = {}
for w in self.searchSpace:
self.suggestions[w] = calculateWordEntropy(w, self.searchSpace)
sortedSuggestions = [i for i in self.suggestions]
def sortByEntropy(e):
return self.suggestions[e]
sortedSuggestions.sort(key=sortByEntropy, reverse=True)
self.suggestions = {i: self.suggestions[i] for i in sortedSuggestions}
self.writeSuggestions()
print()
def writeSuggestions(self):
with open('suggestions.txt', 'wb') as f:
for w in self.suggestions:
f.write(f'{w} {self.suggestions[w]}\n'.encode('utf-8'))