-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
433 lines (349 loc) · 11.6 KB
/
main.py
File metadata and controls
433 lines (349 loc) · 11.6 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import os
import random
import time
import sys
from enum import StrEnum
from collections import Counter
from dataclasses import dataclass
class PlayStatusCode(StrEnum):
"""
The Enum for the status code returned from `play()`
"""
NORMAL = "normal"
GIVE_UP = "give up"
WON = "won"
class Result(StrEnum):
"""
The Enum for the correct/wrong/place of a letter
"""
CORRECT = "C"
WRONG_PLACE = "W"
WRONG = "X"
@dataclass
class LetterRequest:
"""
Return type of `request_letters()`. Contains `letters`,
`valid_words` and `dev`
"""
letters: int
valid_words: list[str]
dev: bool
@dataclass
class GameConfig:
"""
Configuration for whole game
"""
min_letters: int
max_letters: int
colors: bool
@dataclass
class RoundConfig:
"""
Configuration for specific round
"""
dev: bool
letters: int
WORD_FILE = "allWords.txt" # Default word file
CORRECT_ANSI = "\033[42m" # Green background
WRONG_PLACE_ANSI = "\033[43m" # Yellow background
WRONG_ANSI = "\033[40m" # Black background
RESET_ANSI = "\033[0m" # Reset colour
COLOR_KEY = {
Result.CORRECT: CORRECT_ANSI,
Result.WRONG_PLACE: WRONG_PLACE_ANSI,
Result.WRONG: WRONG_ANSI
}
NONCOLOR_KEY = {
Result.CORRECT: ["<", ">"],
Result.WRONG_PLACE: ["?", "?"],
Result.WRONG: ["-", "-"]
}
def clear_console() -> None:
"""
Clear the whole console
"""
os.system('cls' if os.name == 'nt' else 'clear')
def clear_line(n: int = 1) -> None:
"""
Clear the last `n` lines from the console
:param n: Amount of lines to clear
:type n: int
"""
print("\033[1A\033[2K\r"*n, end="")
def get_words(file_path: str) -> list[str]:
"""
Read words from word file
:param file_path: Word file location
:type file_path: str
:return: List of words read from word file
:rtype: list[str]
"""
with open(file_path, "r") as file:
words: list[str] = [word.strip() for word in file]
if len(words) == 0:
raise SystemExit("No words in word file.")
return words
def resource_path(relative_path: str) -> str:
"""
Get absolute path to resource, works for PyInstaller EXE
:param relative_path: The relative path to the words file
"""
if getattr(sys, 'frozen', False):
# temporary folder for PyInstaller
base_path: str = sys._MEIPASS # type: ignore
else:
base_path: str = os.path.dirname(os.path.abspath(__file__))
return os.path.join(base_path, relative_path)
def get_word_file_path(default_file_path: str) -> str:
"""
Get the file_path of the words file
:param default_file_path: The default file path this should assume
exists, if it doesnt exist, it will ask the user to select a
different .txt file.
:type default_file_path: str
:return: file_path of words file
:rtype: str
"""
if os.path.exists(resource_path(default_file_path)):
return resource_path(default_file_path)
print(f"No word file found. The default words file name is {WORD_FILE}")
path = "."
files = [f for f in os.listdir(path)
if f.endswith(".txt")
and os.path.isfile(os.path.join(path, f))
]
print("All text files found in current directory:")
for i, file in enumerate(files):
print(f"{i}. {file}")
file = input("If your word file is one of these, please enter the "
"number, if it is not, please put it into the current "
"folder and retry (press enter).\n> ")
if file.isdigit():
file = int(file)
if not 0 <= file < len(files):
raise SystemExit("Please type a number in the list.")
elif file.strip() != "":
raise SystemExit("Please type a number in the list.")
else:
raise SystemExit("Please add word file to directory.")
return files[file]
def request_letters(
words: list[str],
config: GameConfig
) -> LetterRequest:
"""
Ask user for amount of letters in word, will also determin dev mode
on/off
:param words: The words in the words file
:type words: list[str]
:param config: Info about the game configuration
:type config: GameConfig
:return: Instance of `LetterRequest` dataclass containing
`letters`, `valid_words`, and `dev`.
:rtype: LetterRequest
"""
while True:
letters_input: str = input(
"Please enter the amount of letters in the word you would like "
f"to guess ({config.min_letters}-{config.max_letters}):\n> "
)
if letters_input == "dev":
print("Dev mode activated.")
letters: int = int(input(
"Please enter the amount of letters in the word you would"
" like to guess:\n> "
))
return LetterRequest(letters=letters, valid_words=[], dev=True)
if not letters_input.isdigit():
print("Please put a number.")
continue
letters: int = int(letters_input)
if letters < config.min_letters or letters > config.max_letters:
print(
f"Please enter a value between {config.min_letters} and "
f"{config.max_letters}."
)
continue
valid_words: list[str] = [x for x in words
if len(x) == letters]
if len(valid_words) == 0:
print(f"There are no words {letters} letters long")
continue
return LetterRequest(letters=letters,
valid_words=valid_words,
dev=False
)
def validate_guess(
letters: int,
valid_words: list[str],
dev: bool
) -> tuple[str, bool]:
"""
Get user's guess, and ensure it is valid. If it is not valid, ask
until it is. Will also tell if user has given up.
:param letters: The amount of letters in the word the user is
trying to guess
:type letters: int
:param valid_words: The guessable words
:type valid_words: list[str]
:param dev: Whether the user is in dev mode or not
:type dev: bool
:return: Guess, and give up status
:rtype: tuple[str, bool]
"""
while True:
guess: str = input().strip().lower()
if guess == "giveup":
return guess, True
if len(guess) != letters:
print("Please guess the correct amount of letters!")
time.sleep(0.75)
clear_line(2)
continue
if guess not in valid_words and not dev:
print("Please guess an existing word!")
time.sleep(0.75)
clear_line(2)
continue
return guess, False
def format_letter(result: Result, letter: str, colors: bool) -> str:
"""
Format a letter with background colour according to the
correct/wrong/place indicator
:param result: The correct/wrong/place of the letter
:type result: Result
:param letter: The letter that is being formatted
:type letter: str
:param colors: Whether colors exist in the console
:type colors: bool
:return: The formatted letter
:rtype: str
"""
if colors:
return COLOR_KEY[result] + letter + RESET_ANSI
toPrint: str = ""
toPrint += NONCOLOR_KEY[result][0]
toPrint += letter
toPrint += NONCOLOR_KEY[result][1]
return toPrint
def render_round(
game_config: GameConfig,
guess: str,
truth: list[Result]
) -> None:
"""
Render and print the stuff in the round
:param game_config: Description
:type game_config: GameConfig
:param round_config: Description
:type round_config: RoundConfig
:param guess: Description
:type guess: str
:param truth: Description
:type truth: list[Result]
"""
toPrint: list[str] = [""] * len(truth)
for i, letter in enumerate(guess):
result = truth[i]
toPrint[i] = format_letter(result, letter, game_config.colors)
clear_line()
# add a space between if no colors
print((" "*abs(game_config.colors-1)).join(toPrint))
def play_round(
game_config: GameConfig,
round_config: RoundConfig,
word: str,
valid_words: list[str],
) -> PlayStatusCode:
"""
The main part of the game. Returns status code 0-2.
- 0: Everything is normal, the user has not won or given up.
- 1: The user has given up.
- 2: The user has won.
:param game_config: Info about the game configuration
:type game_config: GameConfig
:param round_config: Info about the round configuration
:type round_config: RoundConfig
:param word: The word the user is trying to guess
:type word: str
:param valid_words: The guessable words
:type valid_words: list[str]
:return: Status code
:rtype: PlayStatusCode
"""
guess, giveup = validate_guess(
round_config.letters,
valid_words,
round_config.dev
)
if giveup:
return PlayStatusCode.GIVE_UP
truth: list[Result] = [Result.WRONG] * round_config.letters
corrects: int = 0
# useful for yellow marking
# if word has only 1 of a letter only mark it once, etc.
num_of_letters: Counter = Counter(word)
for i, letter in enumerate(guess):
if word[i] == letter:
truth[i] = Result.CORRECT
corrects += 1
num_of_letters[letter] -= 1
for i, letter in enumerate(guess):
if truth[i] is Result.CORRECT:
continue
if (letter in word and num_of_letters[letter] > 0):
truth[i] = Result.WRONG_PLACE
num_of_letters[letter] -= 1
render_round(game_config, guess, truth)
if corrects == round_config.letters:
return PlayStatusCode.WON
return PlayStatusCode.NORMAL
def main():
file_path: str = get_word_file_path(WORD_FILE)
words: list[str] = get_words(file_path)
max_letters: int = max(len(word) for word in words)
min_letters: int = min(len(word) for word in words)
min_letters = 3 if max_letters > 3 else min_letters
colors: bool = input(CORRECT_ANSI + "Can you see the background color? (y"
"/n)" + RESET_ANSI + "\n> ").lower() in ["y", "yes"]
clear_console()
game_config: GameConfig = GameConfig(
max_letters=max_letters,
min_letters=min_letters,
colors=colors
)
playing: bool = True
while playing:
lettersInfo: LetterRequest = request_letters(words, game_config)
valid_words: list[str] = lettersInfo.valid_words
round_config: RoundConfig = RoundConfig(
letters=lettersInfo.letters,
dev=lettersInfo.dev
)
clear_console()
print("--------- Python Wordle ---------")
print(f"Selected letters: {round_config.letters}")
if round_config.dev:
print("Dev mode")
print("Type 'giveup' to give up.")
if not game_config.colors:
print("<> = Correct place ? = Wrong place - = Not in word")
print("\n")
word: str = input(
"Word: ") if round_config.dev else random.choice(valid_words)
status: PlayStatusCode = PlayStatusCode.NORMAL
while status is PlayStatusCode.NORMAL:
status = play_round(game_config, round_config, word, valid_words)
if status is PlayStatusCode.GIVE_UP:
print("Word was:", word)
else:
print("Well done!")
playing = input("Again? (y/n)\n> ") in ["y", "yes"]
clear_console()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
clear_console()
print("Goodbye!")
raise SystemExit(0)