-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
258 lines (213 loc) · 6.81 KB
/
Copy pathmain.py
File metadata and controls
258 lines (213 loc) · 6.81 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
import getpass
from huepy import red, green, yellow, bold
from backend import *
from typing import Dict
SIZE = 1000
LIMIT = 750
WORDS_TO_DELETE = []
def get_gamemode() -> str:
print()
print("-"*80)
print("Choose between 'PvC' and 'PvP' mode.".center(80))
print("PvC mode:")
print("\tYou play against the computer in 'hangman' mode.")
print()
print("PvP mode: ")
print("\tYou play against other local players in two different modes.")
print()
gamemode = convert(input(": "))
if gamemode == "pvp":
return "pvp"
else:
return "pvc"
def pvc_round(secret):
guess_limit = 20
print()
print(bold(green("Player 1".center(80))))
print(f"Try to guess the word: {len(secret)*'*'} ({len(secret)} letters).")
print(f"The secret word has a score of {word_score(secret)}")
print()
right = set()
wrong = set()
counter = 0
while counter < guess_limit:
guess = input(f"guess {counter}/{guess_limit}: ")
# commands
if guess == "/true":
print()
print("The secret word contains:")
print(green(", ".join(list(sorted(right)))))
print()
continue
elif guess == "/false":
print()
print("The secret word does not contain:")
print(red(", ".join(list(sorted(wrong)))))
print()
continue
elif guess == "/solve":
print(str(green("The word was: ") + secret).center(80))
print()
return 0
guess = convert(guess)
if guess == secret:
print()
print(green("Congratulations, you guessed it.".center(80)))
print(str(green("The word was: ") + secret).center(80))
print()
return word_score(secret)
if len(guess) == 1:
guess = guess.upper()
if guess in secret.upper():
right.add(guess)
else:
wrong.add(guess)
else:
print(red("Sorry, but that was wrong.".center(80)))
continue
if right == set(secret.upper()):
print()
print(green("Congratulations, you guessed it.".center(80)))
print(str(green("The word was: ") + secret).center(80))
print()
return word_score(secret)
result = "".join(
[char if char in right else "*" for char in secret.upper()]
)
print()
print(result)
print()
counter += 1
print(str(red("The word was: ") + secret).center(80))
def pvp_round(player1: str, player2: str):
# player 2 phase
print(bold(red(player2.center(80))))
print("-"*80)
print("Input your secret word.")
secret = convert(getpass.getpass(": "))
# choose mode
print()
print(bold(green(player1)))
print("-"*80)
print("Choose between 'classic' and 'hangman' mode.".center(80))
print("Classic mode:")
print("\tThere is no limit but single characters aren't allowed.")
print()
print("Hangman mode: ")
print("\tSingle characters are allowed but there is a limit of 50 trys.")
print()
mode = convert(input(": "))
if mode == "hangman":
SINGLE_CHARS_ALLOWED = True
LIMIT = 50
else:
mode = "classic"
SINGLE_CHARS_ALLOWED = False
LIMIT = 0
print(f"You choosed '{mode}' mode.")
print()
# player 1 guess phase
print()
print(bold(green(player1.ljust(40) + mode.rjust(40))))
print(f"Try to guess the word: {len(secret)*'*'} ({len(secret)} letters).")
print(f"You choosed the '{mode}' mode.")
print("Tip: try out the '/best' and '/worst' command.")
print()
last_guesses: Dict = dict()
counter = 1
while True:
if LIMIT:
prompt = f"guess {counter}/{LIMIT}: "
else:
prompt = f"guess {counter}: "
guess = input(prompt)
# commands
if guess == "/best" or guess == "/worst":
rev = True if guess == "/best" else False
print()
if rev:
print("your best guesses: ")
else:
print("your worst guesses: ")
num = 1
tmp = sorted(last_guesses.items(), key=lambda t: t[1], reverse=rev)
for g, s in tmp:
if num <= 10:
print(f"{num}. {g:<10} score: {s:.2f}%")
num += 1
else:
break
print()
continue
elif guess == "/solve":
print(str(green("The word was: ") + secret).center(80))
print()
return 0
elif guess == "/reset_counter":
counter = 1
continue
guess = convert(guess)
if guess == secret:
print()
print(green("Congratulations, you guessed it.".center(80)))
print(str(green("The word was: ") + secret).center(80))
print()
return word_score(secret)
if not SINGLE_CHARS_ALLOWED and len(guess) == 1:
print()
print(red("Single chars aren't allowed").center(80))
print()
continue
sc = compare_score(secret, guess)
last_guesses[guess] = sc
if sc < 25.00:
print(red(f"score: {sc:.2f}%"))
elif sc > 75.00:
print(green(f"score: {sc:.2f}%"))
else:
print(yellow(f"score: {sc:.2f}%"))
print()
if LIMIT and counter == LIMIT:
print("This was your last turn.")
print(str(green("The word was: ") + secret).center(80))
return 0
else:
counter += 1
def PvP():
player1 = input("Player 1: ").capitalize()
player2 = input("Player 2: ").capitalize()
while True:
pvp_round(player1, player2)
if input("new round [y/n]?").lower() == "n":
break
player1, player2 = player2, player1
def PvC():
sample = word_sample(SIZE, LIMIT)
word_counter = 0
win_counter = 0
total_score = 0
for word, score in sample:
word_counter += 1
del score
print()
print(f"Your current score is: {total_score:.2f}%.")
print()
win_counter += bool(pvc_round(word))
total_score = (win_counter / word_counter) * 100
if input("Was the word valid [y/n]?").lower() == "n":
WORDS_TO_DELETE.append(word + "\n")
if input("new round [y/n]?").lower() == "n":
return
def main():
game_mode = get_gamemode()
if game_mode == "pvc":
PvC()
elif game_mode == "pvp":
PvP()
else:
raise Exception("Wrong input!")
if WORDS_TO_DELETE:
print("invalid words will now be deleted")
delete_words(WORDS_TO_DELETE)
if __name__ == "__main__":
main()