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
| try:
from Tkinter import *
except:
from tkinter import *
import random
from functools import partial
def mastermind(taille_code=4, nombre_chiffre=6):
for widget in root.winfo_children():
widget.destroy()
global utilisateur
global code_secret
global essais
global nombre_bien_place
global nombre_mal_place
global nombre_essais
global secret
global indication_ou_message_erreur
nombre_essais = 0
secret = [str(random.randint(1,nombre_chiffre)) for i in range (taille_code)]
bouton_menu = Button(root, text='Menu', command=partial(main), width='8', height='1', foreground="#2113C0", background="#D1D0CC", activeforeground="#2113C0", activebackground="#848489", font=("Comic sans MS",12))
bouton_menu.pack(pady="10")
titre = Label(root, text="Le Mastermind", font=("Comic sans MS", 20), background="#009DE0", foreground="#F8F41F")
titre.pack()
consigne = Label(root, text="Vous devez trouver le code secret de 4 chiffres compris entre 1 et 6. Vous avez 12 tentatives.", font=("Comic sans MS", 15), background="#009DE0")
consigne.pack()
indication_ou_message_erreur = Label(root, text="Veuillez entrer votre tentative : ", font=("Comic sans MS", 15), background="#009DE0")
indication_ou_message_erreur.pack()
utilisateur = Entry(root, justify='center', background="#D1D0CC")
utilisateur.pack(pady='20')
bouton_valider = Button(root, text='Valider', command=partial(valider_mastermind), justify='center', width='20', height='1', foreground="#2113C0", background="#D1D0CC", activeforeground="#2113C0", activebackground="#848489", font=("Comic sans MS",15))
bouton_valider.pack(pady='20')
nombre_bien_place = Label(root, text="", justify="center", font=("Arial", 15), background="#009DE0")
nombre_bien_place.pack()
nombre_mal_place = Label(root, text="", background="#009DE0")
nombre_mal_place.pack(pady="20")
essais = Label(root, text="", justify="center", font=("Comic sans MS", 15), background="#009DE0")
essais.pack()
root.mainloop()
def valider_mastermind():
global nombre_essais
code_utilisateur = list(utilisateur.get())
code_secret=list(secret)
nombre_essais += 1
try:
int(utilisateur.get())
indication_ou_message_erreur.config(text="Veuillez entrer votre tentative :")
except:
indication_ou_message_erreur.config(text="Veuillez entrer un code correct avant de valdier")
nombre_essais -= 1
nombre_bienplace = 0
nombre_malplace = 0
#Nombre de bien placé ou victoire de l'utilisateur :
#On remplace chaque bien placé dans le code de l'utilisateur par x et dans le code secret par * pour ne pas
#les recompter ensuite dans les nombres de mal placé. .
for position, valeur in enumerate (code_utilisateur):
if valeur == code_secret[position]:
nombre_bienplace +=1
code_utilisateur[position] = "x"
code_secret[position] = "*"
if nombre_bienplace == 4 :
gagne_mastermind()
#Nombre de mal placé :
#On remplace chaque mal placé dans le code dans le code secret par * pour ne pas
#recompter un mal placé s'il y a de nouveau la même valeur dans la suite du code.
for position, valeur in enumerate (code_utilisateur):
if valeur in code_secret :
nombre_malplace += 1
code_secret[code_secret.index(valeur)] = "*"
if nombre_essais == 12 :
perdu_mastermind()
nombre_bien_place.config(text="Le nombre de bien placé est : "+str(nombre_bienplace)+"/4", justify='center', borderwidth ="2px", relief=SUNKEN, font=("Arial", 15), background="#26F053",pady='5', padx='5')
nombre_mal_place.config(text="Le nombre de mal placé est : "+str(nombre_malplace)+"/4", justify='center', borderwidth ="2px", relief=SUNKEN, font=("Arial", 15), background="#F81F67",pady='5', padx='5')
essais.config(text="Nombre de tentatives effectuées : " + str(nombre_essais) + "/12")
def gagne_mastermind():
for widget in root.winfo_children():
widget.destroy()
bouton_menu = Button(root, text='Menu', command=partial(main), width='8', height='1', foreground="#2113C0", background="#D1D0CC", activeforeground="#2113C0", activebackground="#848489", font=("Comic sans MS",12))
bouton_menu.pack(pady="10")
titre = Label(root, text="Le Mastermind", font=("Comic sans MS", 20), background="#009DE0", foreground="#F8F41F")
titre.pack()
message_gagne = Label(root, text="Vous avez gagné !", justify='center', borderwidth ="2px", relief=SUNKEN, font=("Arial", 15), background="#26F053",pady='5', padx='5')
message_gagne.pack(pady='20')
reponse = Label(root, text="Le code est effectivement " + str("".join(secret)), borderwidth ="2px", relief=SUNKEN, font=("Times", 20, "bold"), background="#4CC7FB", pady='5', padx='5')
reponse.pack()
essais = Label(root, text="Nombre de tentatives effectuées : " + str(nombre_essais) + "/12", justify='center', font=("Comic sans MS", 15), background="#009DE0")
essais.pack(pady="20")
root.mainloop()
def perdu_mastermind():
for widget in root.winfo_children():
widget.destroy()
bouton_menu = Button(root, text='Menu', command=partial(main), width='8', height='1', foreground="#2113C0", background="#D1D0CC", activeforeground="#2113C0", activebackground="#848489", font=("Comic sans MS",12))
bouton_menu.pack(pady="10")
titre = Label(root, text="Le Mastermind", font=("Comic sans MS", 20), background="#009DE0", foreground="#F8F41F")
titre.pack()
message_perdu = Label(root, text="Vous avez perdu !", justify='center', borderwidth ="2px", relief=SUNKEN, font=("Arial", 15), background="#F81F67",pady='5', padx='5')
message_perdu.pack(pady='20')
reponse = Label(root, text="Le code était " + str("".join(secret)), borderwidth ="2px", relief=SUNKEN, font=("Times", 20, "bold"), background="#4CC7FB", pady='5', padx='5')
reponse.pack()
root.mainloop()
def main():
for widget in root.winfo_children():
widget.destroy()
titre = Label(root, text="menu", font=("Comic sans MS", 20), background="#009DE0", foreground="#F8F41F")
titre.pack()
titre_menu = Label(root, text="Menu : ", font=("Comic sans MS", 15), background="#009DE0")
titre_menu.pack()
bouton_mastermind = Button(root, text='Le Mastermind', command=partial(mastermind), justify='center', width='20', height='1', foreground="#2113C0", background="#D1D0CC", activeforeground="#2113C0", activebackground="#848489", font=("Comic sans MS",15)) #Bouton valider
bouton_mastermind.pack()
root.mainloop()
root = Tk()
root.geometry("1000x600")
root.title("Mastermind")
root.config(background="#009DE0")
main() |
Partager