-
Notifications
You must be signed in to change notification settings - Fork 2
/
wordle.py
88 lines (65 loc) · 2.31 KB
/
wordle.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
import time
import secrets
import lexicon
import frequencies
from termcolor import colored, cprint
def evaluate_guess(lexicon, guess, word):
result=[]
guess_spelling = lexicon.generate_spelling(guess)
word_spelling = lexicon.generate_spelling(word)
success=True
for ix in range(len(guess_spelling)):
ch_guess=guess_spelling[int(ix)]
ch_word=word_spelling[int(ix)]
if ch_guess==ch_word:
result.append((ch_guess, "white", "on_green"))
elif ch_guess.upper()==ch_word:
result.append((ch_guess, "white", "on_green"))
elif ch_guess in word:
result.append((ch_guess, "white", "on_yellow"))
success=False
else:
result.append((ch_guess, "", ""))
success=False
return success, result
def print_result(attempt, result):
print (str(attempt) + "/6 ", end='')
for letter, textcolor, backgroundcolor in result:
if textcolor=='' and backgroundcolor=='':
print(" " + letter + " ", end='')
else:
cprint(" " + letter + " ", textcolor, backgroundcolor, end='')
# gap rhwng lythrenau..
print(" ", end='')
print()
def choose_random_word(lexicon, frequencies):
candidates = l.get_spellings(5)
while True:
word = secrets.choice(candidates).replace(".","")
if "-" in word:
continue
freq = frequencies.get_frequency(word)
if freq>500:
return word
if __name__ == "__main__":
l=lexicon.Lexicon()
f=frequencies.Frequencies()
print ("Byddwch barod i chwarae Wordle Cymraeg....")
try:
word = choose_random_word(l,f)
#print (word)
success=False
for attempt in range(1,7):
if success:
print ("Llongyfarchiadau mawr!! %s/%s : %s" % (attempt -1, 6, word))
break
while True:
guess = input('%s > ' % attempt)
if l.is_word_length(guess, 5):
success, result = evaluate_guess(l, guess, word)
print_result (attempt, result)
break
else:
print("Rhaid i'r gair bodoli yn y lecsicon neu bod yn 5 lythyren")
except KeyboardInterrupt:
pass