-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordle.py
240 lines (169 loc) · 6.29 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
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
import random
import string
from Round import Round
from UI_manager import UIManager
def main():
"""Starts Main Menu"""
choice = "1"
title = "Welcome to Wordle!"
# Valid choices
while choice in ["1", "2", "3", "4", "5"]:
choice = show_main_menu(title)
if choice == "1":
title = "Welcome to Wordle!"
game = play_wordle_round()
while game:
game = play_wordle_round()
elif choice == "2":
see_scoreboards()
elif choice == "3":
see_profiles()
elif choice == "4":
title = add_or_remove_word("add")
elif choice == "5":
title = add_or_remove_word("remove")
else:
choice = "Stop"
def show_main_menu(title) -> str:
"""Shows the main menu with a title"""
ui = UIManager()
return ui.show_main_menu(title)
def play_wordle_round() -> bool:
"""Plays a repeatable round of Wordle"""
game_mode = choose_game_mode()
if game_mode:
word = get_starting_word(game_mode["Wordlength"])
new_round = Round(word, game_mode["Wordlength"], game_mode["Max Guesses"])
UIManager().clear_screen()
new_round.play_round()
print("Play Again? (Y / N)")
repeat = input()
if repeat.lower() == "y":
return True
def choose_game_mode():
"""Choose a game mode"""
# UI
ui = UIManager()
mode = ui.game_mode()
# Change setting according to user choice
settings = ""
if mode == "1":
settings = {"Wordlength": 5, "Max Guesses": 5}
elif mode == "2":
settings = {"Wordlength": 4, "Max Guesses": 6}
elif mode == "3":
settings = {"Wordlength": 7, "Max Guesses": 7}
if settings:
return settings
return None
def get_starting_word(wordlength) -> str:
"""Gets a random word from the word list"""
# file names differ only by a single number, the wordlength
file_name = f"Wordlists/{wordlength}_letter_words.txt"
with open(file_name, "r") as word_list:
words = word_list.read().splitlines()
ret_word = random.choice(words)
return ret_word.lower()
def see_scoreboards():
"""Show high scores"""
# UI
ui = UIManager()
choice = ui.see_scoreboards()
# Open the correct folder based on user input
file_name, title = None, None
if choice == "1":
file_name = "high_scores/5wordle_scores.txt"
title = "NORMAL MODE"
elif choice == "2":
file_name = "high_scores/squadrant_scores.txt"
title = "SQUADRANT MODE"
elif choice == "3":
file_name = "high_scores/rule_of_7_scores.txt"
title = "RULE OF 7"
# Show highscores if user input is valid
if file_name and title:
with open(file_name) as word_data:
scoreboard = word_data.read().splitlines()
# Sort scoreboard by points
scoreboard = sorted(
scoreboard, key=lambda user: int(user.split(":")[1]), reverse=True
)
number_of_lines = len(scoreboard)
ui.see_specified_scoreboard(scoreboard, title, number_of_lines)
def see_profiles():
"""See profiles"""
ui = UIManager()
ui.see_profiles()
def add_or_remove_word(operation):
"""Takes in an operation and either calls to remove or add a word"""
# Show UI
ui = UIManager()
ui.add_remove_word(operation)
# Get word length and error check
word_length = 0
while word_length not in ["4", "5", "7"]:
unsafe_word_length = input()
if unsafe_word_length not in ["4", "5", "7"]:
print("Word length must be '4', '5', or '7' (number). Try Again")
else:
word_length = unsafe_word_length
# Set word length and get filename
word_length = int(unsafe_word_length)
file_name = f"Wordlists/{word_length}_letter_words.txt"
if operation == "add":
return add_word_to_wordlist(word_length, file_name)
else:
return remove_word_from_list(file_name)
def add_word_to_wordlist(word_length, file_name):
"""Add word to the specified wordlist"""
# We're going to append and read the file, so open it in r+ mode
with open(file_name, "r+") as word_list:
words = word_list.read().splitlines()
# Get new word and error check
new_word = ""
while new_word == "":
print("Enter new word:")
unsafe_new_word = input()
unsafe_new_word = unsafe_new_word.strip().strip(string.punctuation)
if len(unsafe_new_word) != word_length:
print(f"Word must be {word_length} letters long\n")
elif any([letter.isnumeric() for letter in unsafe_new_word]):
print("Letter cannot include numbers\n")
elif " " in unsafe_new_word:
print("Word cannot include spaces\n")
# The last thing we check on to hopefully save some time
elif unsafe_new_word in words:
print("Word already in wordlist\n")
else:
new_word = unsafe_new_word
word_list.write(new_word.lower() + "\n")
return f"{new_word} added to wordlist!"
def remove_word_from_list(file_name):
"""Remove word from the specified wordlist"""
with open(file_name, "r") as word_list_file:
old_word_list = word_list_file.read().splitlines()
# Get new word and error check
word_amount = len(old_word_list)
while len(old_word_list) == word_amount:
print("Enter the word you want to remove:")
bad_word = input().lower()
# If the word is in the list, remove it
if bad_word in old_word_list:
old_word_list.remove(bad_word)
response = f"{bad_word} removed from wordlist"
# The user might have expected the word to be there,
# so give them a chance to exit here
else:
print(f"{bad_word} not in wordlist. Try Again (Y / N)?")
repeat = input()
if repeat.lower() != "y":
response = "Word removal cancelled"
break
# Rewrite word file
if response != "Word removal cancelled":
with open(file_name, "w") as new_word_list:
for word in old_word_list:
new_word_list.write(word + "\n")
return response
if __name__ == "__main__":
main()