-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_handler.py
More file actions
48 lines (40 loc) · 1.39 KB
/
game_handler.py
File metadata and controls
48 lines (40 loc) · 1.39 KB
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
from letter import LetterModel
class Wordle:
MAX_ATTEMPTS = 6
WORD_LENGTH = 5
LETTER_CHECK = "*"
def __init__(self, secret: str):
self.secret: str = secret.upper()
self.attempts = []
pass
@property
def is_solved(self):
return len(self.attempts) > 0 and self.attempts[-1] == self.secret
@property
def attempts_left(self):
return self.MAX_ATTEMPTS - len(self.attempts)
@property
def can_try_again(self):
return self.attempts_left > 0 and not self.is_solved
def attempt(self, word: str):
word = word.upper()
self.attempts.append(word)
def guess(self, word: str):
word = word.upper()
result = [LetterModel(x) for x in word]
remaining_secret = list(self.secret)
for i in range(self.WORD_LENGTH):
letter = result[i]
if letter.character == remaining_secret[i]:
letter.validPosition = True
remaining_secret[i] = self.LETTER_CHECK
for i in range(self.WORD_LENGTH):
letter = result[i]
if letter.validPosition:
continue
for j in range(self.WORD_LENGTH):
if letter.character == remaining_secret[j]:
remaining_secret[j] = self.LETTER_CHECK
letter.validCharacter = True
break
return result