-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
278 lines (221 loc) · 9.51 KB
/
main.py
File metadata and controls
278 lines (221 loc) · 9.51 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
import random
import time
import textwrap
import shutil
import string
import re
# Vérifie que le module Colorama est présent
try:
from colorama import init, Fore, Back, Style
init()
except ModuleNotFoundError:
raise ModuleNotFoundError("""
////////////////////////////////////////////////////////////////////////////////
Il vous faut le module Colorama pour lancer ce programme. (pip install colorama)
////////////////////////////////////////////////////////////////////////////////
""")
SIZE = shutil.get_terminal_size()
TERMINAL_WIDTH = SIZE.columns
COLORS = {
"B": Fore.BLUE + "B" + Style.RESET_ALL, # BLUE
"G": Fore.GREEN + "G" + Style.RESET_ALL, # GREEN
"R": Fore.RED + "R" + Style.RESET_ALL, # RED
"Y": Fore.YELLOW + "Y" + Style.RESET_ALL, # YELLOW
"C": Fore.CYAN + "C" + Style.RESET_ALL, # CYAN
"P": Fore.MAGENTA + "P" + Style.RESET_ALL # PURPLE
}
TEAM_NAMES = {
Fore.BLUE + "PROJECT COORDINATOR" + Style.RESET_ALL: Fore.BLUE + "Maryse Pilote" + Style.RESET_ALL,
Fore.GREEN + "QUALITY CONTROL" + Style.RESET_ALL: Fore.GREEN + "Sam Sebille" + Style.RESET_ALL,
Fore.YELLOW + "LEAD DESIGNER" + Style.RESET_ALL: Fore.YELLOW + "Yanni Haddar" + Style.RESET_ALL,
Fore.RED + "LEAD PROGRAMMER" + Style.RESET_ALL: Fore.RED + "Quentin Gastaldo" + Style.RESET_ALL
}
CODE_SIZE = 4
allowed_chars = string.printable + " "
# Centrage du texte pour l 'affichage
def center_print(txt, offset = 0, jumpline = True):
#definition et supression des codes de style ansi
regex = re.compile("\x1b\[([0-9,A-Z]{1,2}(;[0-9]{1,2})?(;[0-9]{3})?)?[m|K]?", re.UNICODE)
cleaned_string = re.sub(regex, '', txt)
#calcul de la longueur finale de la string
length = len(cleaned_string)
#ajout d'espaces pour centrer le texte dans la console, en ne prenant en compte que les charactères qui seront affichés
whitespace = ""
for space in range((TERMINAL_WIDTH // 2) - (length//2) + offset):
whitespace = whitespace + " "
#print les espaces, la string, et enlever les couleurs a la fin
print(whitespace + txt + Style.RESET_ALL, end = "")
if jumpline:
print()
# Affichage du Titre
def start():
print()
txt_logo = (Fore.BLUE + " ██\ ██\ ██████\ ██████\ ████████\ ████████\ ███████\ ██\ ██\ ██████\ ██\ ██\ ███████\ \n" +
Fore.BLUE + " ███\ ███ |██ __██\ ██ __██\ \__██ __|██ _____|██ __██\ ███\ ███ |\_██ _|███\ ██ |██ __██\ \n" +
Fore.GREEN + " ████\ ████ |██ / ██ |██ / \__| ██ | ██ | ██ | ██ |████\ ████ | ██ | ████\ ██ |██ | ██ |\n" +
Fore.GREEN + " ██\██\██ ██ |████████ |\██████\ ██ | █████\ ███████ |██\██\██ ██ | ██ | ██ ██\██ |██ | ██ |\n" +
Fore.YELLOW + " ██ \███ ██ |██ __██ | \____██\ ██ | ██ __| ██ __██< ██ \███ ██ | ██ | ██ \████ |██ | ██ |\n" +
Fore.YELLOW + " ██ |\█ /██ |██ | ██ |██\ ██ | ██ | ██ | ██ | ██ |██ |\█ /██ | ██ | ██ |\███ |██ | ██ |\n" +
Fore.RED + " ██ | \_/ ██ |██ | ██ |\██████ | ██ | ████████\ ██ | ██ |██ | \_/ ██ |██████\ ██ | \██ |███████ |\n" +
Fore.RED + " \__| \__|\__| \__| \______/ \__| \________|\__| \__|\__| \__|\______|\__| \__|\_______/\n")
for line in textwrap.wrap(txt_logo, width=116, drop_whitespace=False):
center_print(line)
print()
center_print(Fore.BLACK + Style.BRIGHT + "Developed by" + Fore.YELLOW + " StackNoodles™")
center_print(Style.BRIGHT + Fore.BLACK + "This work is licensed under a GNU General Public License version 3.0 (or later version)")
print()
# Lancement du programme
def game():
start()
main_menu()
# Menu principal + choix
def main_menu():
while True:
print()
center_print("Press P to PLAY, Q to QUIT, C for the credits")
center_print(Style.BRIGHT + Fore.YELLOW + ">>> ", -20, False)
reponse = input().upper()
print(Style.RESET_ALL, end = "")
if reponse == "P":
play()
elif reponse == "KILL":
quick_quit()
elif reponse == "Q":
slow_quit()
elif reponse == "C":
credit()
# partie de Mastermind
def play():
# Code a deviner
secret_code = []
try_meter = 0
for i in range(CODE_SIZE):
secret_code.append(random.choice(list(COLORS.items())))
play_menu()
# Boucle des tours
while True:
center_print(Style.BRIGHT + Fore.YELLOW + ">>> ", -21, False)
query = input().upper()
print(Style.RESET_ALL, end = "")
if query == "KILL":
quick_quit()
elif query == "GIVE UP":
center_print(Fore.YELLOW + "Chicken")
return
elif query == "HACK":
str_cheat = ""
for key, value in secret_code:
str_cheat = str_cheat + value
center_print(Fore.RED + "ans:" + str_cheat, -19)
print()
continue
elif query == '':
continue
submit = verify_query(query)
if submit == "erreur":
center_print(Fore.RED + "Wrong input!", -13)
print()
continue
try_meter += 1
# Copie du code secret pour pouvoir le manipuler
code_copy = secret_code.copy()
submit_copy = submit.copy()
output = []
# On verifie d'abord les bonnes couleurs au bon endroit
for i, (k, v) in enumerate(code_copy):
if v == submit_copy[i]:
output.append('!')
# Valeurs tempon pour ne pas interferer avec d'autre comparaisons
code_copy[i] = (k, 'X')
submit_copy[i] = ''
# On verifie ensuite les bonnes couleurs au mauvais endroit
for i, digit in enumerate(submit_copy):
for j, (k, v) in enumerate(code_copy):
if digit == v:
output.append('?')
# Valeurs tempon pour ne pas interferer avec d'autre comparaisons
code_copy[j] = (k, 'X')
submit_copy[i] = ''
break
victory = True
result_chain = ''
for i in range(CODE_SIZE):
if i < len(output):
result_chain += output[i]
if output[i] != '!':
victory = False
else:
# On complete la chaine de resultat avec des espaces pour les mauvaises couleurs
result_chain += ' '
victory = False
attempt = ''
for color in submit:
attempt += color
if (victory):
center_print(attempt + " was the secret code.")
center_print(Fore.GREEN + Style.BRIGHT +"Congrats!! " + Style.RESET_ALL + "It took you " + str(try_meter) + " step(s).")
return
else:
center_print(attempt + " --> [" + result_chain + "] (?: Color; !: Color + Index)", 3)
print()
# Entrée dans la partie
def play_menu():
print()
center_print(Style.BRIGHT + Fore.GREEN + "Game Start!")
print()
center_print("Try a " + str(CODE_SIZE) + " char code [" + COLORS["B"] + ", " + COLORS["G"] + ", " + COLORS["R"] +
", " + COLORS["Y"] + ", " + COLORS["C"] + ", " + COLORS["P"] + "] or " + Fore.GREEN + "GIVE UP")
# Verification et traduction de l'entrée de l'utilisateur
def verify_query(query):
attempt = list(query)
output = []
if len(attempt) != CODE_SIZE:
return "erreur"
for letter in attempt:
try:
output.append(COLORS[letter])
except KeyError:
return "erreur"
return output
# Sortie lente du programme
def slow_quit():
center_print("Unplugging")
center_print("", -5, False)
for period in range(5):
time.sleep(0.2)
print(". ", end="")
time.sleep(0.5)
print()
center_print(" BRAIN UNPLUGGED!\n")
time.sleep(0.5)
center_print("Goodbye.")
quit()
# Sortie rapide du programme
def quick_quit():
print()
center_print(Fore.RED + "Goodbye")
quit()
# Affichage des crédit
def credit():
print()
center_print(Style.BRIGHT + "Developed in 2022 by" + Fore.YELLOW + " StackNoodles™", False)
print(":\n")
for title, name in TEAM_NAMES.items():
center_print(title)
center_print(name)
print()
time.sleep(0.5)
center_print(Style.BRIGHT +Fore.BLACK + "Licensed under GNU General Public License v3.0")
center_print(Style.BRIGHT +Fore.BLACK + "https://github.com/StackNoodles/Labo1-Mastermind")
print()
# Demarrage du programme
if __name__ == '__main__':
try:
game()
except KeyboardInterrupt:
quick_quit()
except Exception as e:
print(Fore.RED)
print (" > Fatal Error:")
print(" > " + str(e))
quick_quit()