-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
75 lines (67 loc) · 3.77 KB
/
game.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
'''
Wordle is implemented in this class and returns the same outputs as online ones.
Having a local version is useful to avoid the use of an API or an extra-package to get faster input/outputs than the human/main.py interface.
'''
# The game is an object
class Wordle:
""" Class imitating WordleGame.org © 2022 input and outputs."""
def __init__(self):
self.word = None
def evaluate_guess(self, word, print_ = False):
'''str ---> str
From a word of 5 letters (in the dictionnary or not), returns the Wordle comparison to the secret word (stored in self.word).
'-', equivalent to grey, indicates that the letter isn't in the target word at all.
'y', equivalent to yellow, indicates that the letter is in the word but in the wrong spot.
'g', equivalent to green, indicates that the letter is in the word and in the correct spot.
The return can be print in the terminal if print_ = True.'''
# Stop evuluating if successful guess
if word == self.word:
if print_:
print('Success!')
return 'ggggg'
else:
word_split = list(word)
answer = ''
# Different method if two identical letters in the input
# Note that there is no 5-word with 3 identical letters.
assigned_double_letters = []
# Each input letter is scanned from left to right
for i,letter in enumerate(word_split):
# Dichotomy structure
if letter in self.word:
# Need to test the rest of the input word for identical letter inputs
word_elsewhere = word_split[:i]+word_split[i+1:]
# Immediate green case
if self.word[i] == letter:
answer += 'g'
else:
# Immediate yellow if single letter input
if not(letter in word_elsewhere): #ie if you give twice the same letter
answer += 'y'
# If not, impossible to assign colors independently
else:
# Case where the same letter is given twice but is not twice in the secret word
if self.word.count(letter)< word.count(letter):
# If the letter has already been searched for, the single 'y' allowed has already been used.
if letter in assigned_double_letters:
answer += '-'
# Otherwise, need to assign and record the assignment
else:
# Need to scan secret word but not where double letters are
word_out_letters = [self.word[i] for i in range(5) if word[i]!= letter ] #remove self.word indices corresponging to my double letters
# Case where the letter is not at any of both input spots
if letter in word_out_letters:
answer += 'y'
# Otherwise the other letter is green
else:
answer +='-'
assigned_double_letters.append(letter)
else:
answer += 'y'
# Immediate grey case
else:
answer+='-'
if print_:
print(answer)
return answer
'''Yoann Launay, University of Cambridge, 12/22.'''