import tkinter as tk from random import * def init_game(): global code code=[] while len(code)<4: nb=randint(1,8) if nb not in code: code.append(nb) def check_play(code_entre): global black global white black=0 white=0 for el in code_entre : if el in code: if code.index(el) == code_entre.index(el): black+=1 else: white+=1 def start(): tentatives = 0 init_game() print(code) while tentatives <12 : entree = input("jouez 4 couleurs parmi [1,8] séparéés d'un espace : ") possible=["1","2","3","4","5","6","7","8"," "] code_entre=[] for ele in entree: if ele not in possible or entree.count(ele) > 1 : entree= input(" entrer des valeurs valides svp ") elif ele != " ": ele=int(ele) code_entre.append(ele) check_play(code_entre) if black == 4: print("gagné ! tu es très intelligent ! tu as utilisé ", tentatives, "essais") break print("=> black " ,black , "white ", white) tentatives+=1 print("tu as utilisé ", tentatives ," essais sur 12") tkcolors=["white", "black", "red", "blue", "green", "yellow", "purple", "orange"] def choix(event,couleur): canvas.itemconfigure(carre,fill=couleur) def send(): print("send") def reset_row(): print("reset") app=tk.Tk() app.title("Mastermind") new_game=tk.Button(app,text="new game",command=init_game,width=10) new_game.grid(row=0,column=0,columnspan=8,sticky=tk.W) canvas=tk.Canvas(app,width=300,height=450,bg="white") canvas.grid(row=1 ,column=0,columnspan=8) send=tk.Button(app, text="send",command=send,width=10) send.grid(row=40 , column=0,columnspan=4 ,sticky=tk.E) reset = tk.Button(app,text="reset", command=reset_row,width=10) reset.grid(row=40,column=4 , columnspan=4,sticky=tk.E) for i, color in enumerate(tkcolors): b = tk.Button(app, text='', width=3, bg=color, activebackground=color) b.grid(row=20, column=i) def click_bt(event,couleur = color): return choix(event,couleur) b.bind("",click_bt) ##creation grille carré: x0=10 y0=10 x1=40 y1=40 for a in range(11): for c in range(4): carre=canvas.create_rectangle(x0,y0,x1,y1,fill="grey") x0+=40 x1+=40 y0+=40 y1+=40 x0=10 x1=40 choix(event,couleur) ## creation cercles: i=170 j=10 k=180 l=20 for a in range(10): for c in range(4): cercle = canvas.create_oval(i,j,k,l) i+=15 k+=15 j+=40 l+=40 k=180 i=170 ligne= canvas.create_line(0,405,300,405) init_game() app.mainloop()