-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle.py
56 lines (46 loc) · 1.63 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
import random
class Wordle:
def __init__(self, words):
self.words = words
self.answer = random.choice(self.words).strip()
def choose_word(self):
self.answer = random.choice(self.words).strip()
''' Returns a list of flags
G: the letter is in the right position
Y: the letter is included in the word but in another position
-: the letter is not in the word '''
def compare_words(self, input_word):
length = len(self.answer)
flags = [0] * (length)
result = [0] * (length)
for i in range(length):
if input_word[i] == self.answer[i]:
result[i] = "G"
i_answer = 0
i_input = 0
while i_input < length:
if result[i_input] == "G":
i_input += 1
continue
i_answer = 0
while i_answer < length:
if flags[i_answer] == "Y" or result[i_input] == "Y" or result[i_answer] == "G":
i_answer += 1
continue
if self.answer[i_answer] == input_word[i_input]:
flags[i_answer] = "Y"
result[i_input] = "Y"
break
i_answer += 1
if result[i_input] != "Y":
result[i_input] = "-"
i_input += 1
return result
# Use colors here
def show_result(self, result):
print("{}\n".format("".join(result)))
def check_victory(self, result):
for flag in result:
if flag != 'G':
return False
return True