|
| 1 | +from tkinter import * |
| 2 | +class medium: |
| 3 | + def user(self,color): # takes user' choice |
| 4 | + self.color=color |
| 5 | + def __init__(self): # generates random palette |
| 6 | + a=['#270101', '#F08B33', '#776B04', '#F1B848', '#8F715B', '#0486DB', '#C1403D', '#F3D4A0'] |
| 7 | + import random |
| 8 | + self.b=[];n=4; |
| 9 | + while n!=0: |
| 10 | + p=random.choice(a) |
| 11 | + if p not in self.b: |
| 12 | + self.b.append(p) |
| 13 | + n-=1 |
| 14 | + def compare(self,g,l1): |
| 15 | + l=[] # hints |
| 16 | + for x in range(4): |
| 17 | + if l1[x]==g[x]: |
| 18 | + l.append('red') |
| 19 | + elif l1[x]in g: |
| 20 | + l.append('gray') |
| 21 | + return l |
| 22 | +class MasterMind(): |
| 23 | + def __init__(self, root): |
| 24 | + obj=medium() |
| 25 | + self.gen=obj.b # generated color combo |
| 26 | + self.colors = ['#270101', '#F08B33', '#776B04', '#F1B848', '#8F715B', '#0486DB', '#C1403D', '#F3D4A0'] |
| 27 | + root.geometry('390x600') |
| 28 | + for y in range(20): |
| 29 | + Grid.rowconfigure(root, y, weight=1) |
| 30 | + for x in range(8): |
| 31 | + Grid.columnconfigure(root, x, weight=1) |
| 32 | + self.palette = [] # display of palette |
| 33 | + n,c=0,0 |
| 34 | + for i in self.colors: |
| 35 | + self.palette.append(Button(root, bg=i, height=1, width=5, relief=SUNKEN)) |
| 36 | + self.palette[n].grid(row=20, column=c) |
| 37 | + n+=1;c+=1; |
| 38 | + self.palette[0].config(command=lambda: self.guess(root, self.palette[0]['bg'],obj)) # binding function to palette |
| 39 | + self.palette[1].config(command=lambda: self.guess(root, self.palette[1]['bg'],obj)) |
| 40 | + self.palette[2].config(command=lambda: self.guess(root, self.palette[2]['bg'],obj)) |
| 41 | + self.palette[3].config(command=lambda: self.guess(root, self.palette[3]['bg'],obj)) |
| 42 | + self.palette[4].config(command=lambda: self.guess(root, self.palette[4]['bg'],obj)) |
| 43 | + self.palette[5].config(command=lambda: self.guess(root, self.palette[5]['bg'],obj)) |
| 44 | + self.palette[6].config(command=lambda: self.guess(root, self.palette[6]['bg'],obj)) |
| 45 | + self.palette[7].config(command=lambda: self.guess(root, self.palette[7]['bg'],obj)) |
| 46 | + self.user_choice = [] # stores the widget |
| 47 | + self.code = [] # stores the colors |
| 48 | + self.key = [] # stores the hints |
| 49 | + global ccol, cro |
| 50 | + ccol,cro = 2,19 |
| 51 | + def guess(self, root, choice,obj): |
| 52 | + global ccol |
| 53 | + global cro |
| 54 | + f=True # boolean flag |
| 55 | + if cro != 1: |
| 56 | + self.user_choice.append(Button(root, bg=choice, height=1, width=5, relief=RAISED)) |
| 57 | + if len(self.user_choice) < 4: |
| 58 | + self.user_choice[-1].grid(row=cro, column=ccol) |
| 59 | + self.code.append(self.user_choice[-1]['bg']) |
| 60 | + ccol += 1 |
| 61 | + elif len(self.user_choice) == 4: |
| 62 | + self.user_choice[-1].grid(row=cro, column=ccol) |
| 63 | + self.code.append(self.user_choice[-1]['bg']) |
| 64 | + ccol += 1 |
| 65 | + ccol = 2 |
| 66 | + cro = cro-1 |
| 67 | + obj.user(self.code) # send the user's choice |
| 68 | + self.key=obj.compare(self.code,self.gen) #get the hints |
| 69 | + if self.key==['red','red','red','red']: |
| 70 | + f=False |
| 71 | + self.hint(root, self.key) |
| 72 | + l=Label(root,text="CONGRATULATIONS!!!") |
| 73 | + l.grid(row=0,columnspan=8) |
| 74 | + else: |
| 75 | + self.hint(root, self.key) |
| 76 | + self.code = [] |
| 77 | + self.user_choice = [] |
| 78 | + else: |
| 79 | + if f: |
| 80 | + l=Label(root,text="You are a LOSER!!!! ANSWER:") |
| 81 | + l.grid(row=0,columnspan=4) |
| 82 | + c=5 |
| 83 | + for i in self.gen: |
| 84 | + b=Button(root,bg=i,height=1, width=5, relief=SUNKEN) |
| 85 | + b.grid(row=0,column=c) |
| 86 | + c+=1 |
| 87 | + global hcol, hro |
| 88 | + hcol,hro = 8,19 |
| 89 | + def hint(self, root, key): |
| 90 | + global hcol, hro |
| 91 | + a = [] |
| 92 | + for i in key: |
| 93 | + a.append(Label(root, bg=i,relief=SUNKEN)) |
| 94 | + a[-1].grid(row=hro, column=hcol, sticky=E) |
| 95 | + hcol += 1 |
| 96 | + hro -= 1;hcol = 8; |
| 97 | +master = Tk() |
| 98 | +M = MasterMind(master) |
| 99 | +master.mainloop() |
0 commit comments