-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
175 lines (124 loc) · 4.52 KB
/
core.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This is the core simulator for games like Wordle, Quordle, Octordle, Term.ooo and etc...
"""
from enum import Enum
__author__ = "Lucas Hohmann"
__email__ = "[email protected]"
__user__ = "@lfhohmann"
__date__ = "2022/04/14"
__status__ = "Production"
__version__ = "2.1.0"
__license__ = "MIT"
class Hit(Enum):
"""Class representing the status of a hit"""
CORRECT = "c" # Letter is correct
MISPLACED = "m" # Letter is misplaced
INCORRECT = "_" # Letter is incorrect
class GameState(Enum):
"""Class representing the game state"""
RUNNING = "r" # Game running
LOST = "l" # Game lost
WON = "w" # Game won
class AttemptValidness(Enum):
"""Class representing the attempt validness"""
INVALID = "i" # Attempt is invalid
VALID = "v" # Attempt is valid
class GameCore:
"""This is the main class for the Game Core"""
def __init__(
self,
max_attempts: int,
game_solution: str,
valid_guesses: list,
valid_answers: list,
) -> None:
"""
Wordle game constructor
ARGUMENTS:
-----------
max_attempts: int (required)
The maximum number of attempts the player can make
game_solution: str (required)
A word for the game's solution
valid_guesses: list (required)
Valid guesses for the game
valid_answers: list (required)
Valid answers for the game
RETURNS:
--------
output: None
"""
# Store arguments
self.max_attempts = max_attempts
self.game_solution = game_solution
self.valid_guesses = valid_guesses
self.valid_answers = valid_answers
# Store length of the game solution in a variable, so we won't have to call len() every time
self.words_length = len(self.game_solution)
# Init attempt validness and game state
self.attempt_validness = AttemptValidness.INVALID
self.game_state = GameState.RUNNING
# Init attempt counter
self.attempt_number = 0
def __response(self) -> dict:
"""
Standardize the response of the game
RETURNS:
--------
output: dict
The values that need to be returned by the 'play()' method
"""
return {
"game_state": self.game_state.value,
"attempt_validness": self.attempt_validness.value,
"attempt_number": self.attempt_number,
"attempt_hits": [hit.value for hit in self.attempt_hits],
}
def play(self, attempt_guess: str) -> dict:
"""
Execute an attempt at guessing the correct solution
ARGUMENTS:
-----------
attempt_guess: str (required)
The word guessed by the player
RETURNS:
--------
output: dict
Returns the value returned by the '__response()' method
"""
# Init attempt hits
self.attempt_hits = [Hit.INCORRECT for _ in range(self.words_length)]
# Check whether game is over or attempt guess is invalid
if (
self.game_state != GameState.RUNNING
or attempt_guess not in self.valid_guesses + self.valid_answers
):
self.attempt_validness = AttemptValidness.INVALID
return self.__response()
# The attempt guess is valid, increment attempt counter
self.attempt_validness = AttemptValidness.VALID
self.attempt_number += 1
# Init letters counter
counter = {}
for letter in set(attempt_guess):
counter[letter] = self.game_solution.count(letter)
# Compute attempt hits
for i in range(self.words_length):
if attempt_guess[i] == self.game_solution[i]:
self.attempt_hits[i] = Hit.CORRECT
counter[attempt_guess[i]] -= 1
elif counter[attempt_guess[i]] > 0:
self.attempt_hits[i] = Hit.MISPLACED
counter[attempt_guess[i]] -= 1
# Check if the number of attempts is over
if self.attempt_number >= self.max_attempts:
self.game_state = GameState.LOST
return self.__response()
# Check if the correct word was guessed
if self.attempt_hits == [Hit.CORRECT for _ in range(self.words_length)]:
self.game_state = GameState.WON
return self.__response()
# Standard response for when the guess is valid
return self.__response()