├── README.md ├── Mastermind game.py ├── Mastermind Game with UI.py └── Mastermind game with UI.py /README.md: -------------------------------------------------------------------------------- 1 | # Mastermind-Game 2 | Mastermind number game using Tkinter 3 | -------------------------------------------------------------------------------- /Mastermind game.py: -------------------------------------------------------------------------------- 1 | import random 2 | num=[1,2,3,4,5,6,7,8,9] 3 | code=random.sample(num,4) 4 | guess=10 5 | f=0 6 | while(guess!=0): 7 | if(f==4): 8 | points=(10*(guess+1)) 9 | if (10-guess==1): 10 | print("You've successfully cracked the code in 1 guess and have recieved 100 points!") 11 | break 12 | else: 13 | print("You've successfully cracked the code in",10-guess,"guesses and have recieved",points,"points!") 14 | break 15 | f=0 16 | if(f!=4): 17 | user_guess=int(input("enter code with 4 unique digits ranging from 1 to 9: ")) 18 | 19 | user_code=[int(digit) for digit in str(user_guess)] 20 | 21 | for i in range (0,4): 22 | if user_code[i] in code: 23 | if user_code[i]==code[i]: 24 | print(user_code[i],"is correctly placed") 25 | f+=1 26 | else: 27 | print(user_code[i],"is present in the code,but incorrectly placed") 28 | else: 29 | print(user_code[i],"is not in the required code") 30 | guess-=1 31 | print("guesses left:",guess) 32 | while(guess==0): 33 | if(f==4): 34 | points=(10*(guess+1)) 35 | if (10-guess==1): 36 | print("You've successfully cracked the code in 1)guess and have recieved 100 points!") 37 | break 38 | else: 39 | print("You've successfully cracked the code in",10-guess,"guesses and have recieved",points,"points!") 40 | break 41 | else: 42 | print("you've failed to guess the code, the required code was",code[0],code[1],code[2],code[3]) 43 | break 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /Mastermind Game with UI.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | import random 3 | import collections 4 | 5 | class Mastermind: 6 | def __init__(self, parent): 7 | self.parent = parent 8 | self.canvas = tk.Canvas(parent) 9 | self.status = tk.Label(parent) 10 | self.draw_board() 11 | def draw_board(self, event=None): 12 | self.example=['r','o','y','g','b','p'] 13 | 14 | self.canvas.destroy() 15 | self.status.destroy() 16 | self.canvas = tk.Canvas(self.parent,bg='thistle',width=1050, height=400) 17 | self.canvas.pack() 18 | self.bag = {'r':self.canvas.create_oval(960, 0, 1027, 66, fill='red', outline='black'), 19 | 'o':self.canvas.create_oval(960, 66, 1027, 131, fill='orange', outline='black'), 20 | 'y':self.canvas.create_oval(960, 131, 1027, 200, fill='yellow', outline='black'), 21 | 'g':self.canvas.create_oval(960, 200, 1027, 267, fill='green', outline='black'), 22 | 'b':self.canvas.create_oval(960, 267, 1027, 334, fill='blue', outline='black'), 23 | 'p':self.canvas.create_oval(960, 334, 1027, 400, fill='purple', outline='black') 24 | } 25 | self.ids = {a:b for b,a in self.bag.items()} 26 | self.colors = {'r':'red', 'o':'orange', 'y':'yellow', 27 | 'g':'green', 'b':'blue', 'p':'purple'} 28 | self.guesses = [''] 29 | self.status = tk.Label(self.parent) 30 | self.status.pack() 31 | self.canvas.bind('<1>', self.check) 32 | self.parent.bind('', self.draw_board) 33 | self.parent.bind('', self.draw_board) 34 | self.pattern=random.sample(self.example,4) 35 | 36 | self.counted = collections.Counter(self.pattern) 37 | def check(self, event=None): 38 | id = self.canvas.find_withtag("current")[0] 39 | guess = self.ids[id] 40 | self.guesses[-1] += guess 41 | y_offset = (len(self.guesses[-1]) - 1) * 80 42 | x_offset = (len(self.guesses) - 1) * 80 43 | self.canvas.create_oval(x_offset, y_offset, 44 | x_offset+80, y_offset+80, 45 | fill=self.colors[guess], 46 | outline='black') 47 | if len(self.guesses[-1]) < 4: 48 | return 49 | guess_count = collections.Counter(self.guesses[-1]) 50 | close = sum(min(self.counted[k], guess_count[k]) for k in self.counted) 51 | exact = sum(a==b for a,b in zip(self.pattern, self.guesses[-1])) 52 | close -= exact 53 | colors = exact*['black'] + close*['white'] 54 | key_coordinates = [(x_offset, 320, x_offset+40, 360), 55 | (x_offset, 360, x_offset+40, 400), 56 | (x_offset+40, 320, x_offset+80, 360), 57 | (x_offset+40, 360, x_offset+80, 400)] 58 | for color, coord in zip(colors, key_coordinates): 59 | self.canvas.create_oval(coord, fill=color, outline='black') 60 | if exact == 4: 61 | self.status.config(text='YOU ARE A WINNER!') 62 | self.canvas.unbind('<1>') 63 | elif len(self.guesses) > 11: 64 | self.status.config( 65 | text='Oops,out of guesses. The required code is {}.'.format( 66 | ''.join(self.pattern))) 67 | self.canvas.unbind('<1>') 68 | else: 69 | self.guesses.append('') 70 | 71 | 72 | root = tk.Tk() 73 | root.title('Mastermind') 74 | game = Mastermind(root) 75 | root.mainloop() 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /Mastermind game with UI.py: -------------------------------------------------------------------------------- 1 | import random 2 | from tkinter import * 3 | num = [1, 2, 3, 4, 5, 6, 7, 8, 9] 4 | code = random.sample(num, 4) 5 | guess = 10 6 | f = 0 7 | 8 | 9 | def tkinter_version(): 10 | root = Tk() 11 | e = Entry(root, width=35, borderwidth=5) 12 | e.grid(row=0, column=0, columnspan=3, padx=10, pady=10) 13 | 14 | def Button_Click(number): 15 | current = e.get() 16 | e.delete(0, END) 17 | e.insert(0, str(current) + str(number)) 18 | 19 | def Button_Clear(): 20 | e.delete(0, END) 21 | 22 | def Button_Equal(): 23 | global user_guess 24 | user_guess = int(e.get()) 25 | e.delete(0, END) 26 | root.destroy() 27 | 28 | Button_1 = Button(root, text="1", padx=40, pady=20, 29 | command=lambda: Button_Click(1)) 30 | Button_2 = Button(root, text="2", padx=40, pady=20, 31 | command=lambda: Button_Click(2)) 32 | Button_3 = Button(root, text="3", padx=40, pady=20, 33 | command=lambda: Button_Click(3)) 34 | Button_4 = Button(root, text="4", padx=40, pady=20, 35 | command=lambda: Button_Click(4)) 36 | Button_5 = Button(root, text="5", padx=40, pady=20, 37 | command=lambda: Button_Click(5)) 38 | Button_6 = Button(root, text="6", padx=40, pady=20, 39 | command=lambda: Button_Click(6)) 40 | Button_7 = Button(root, text="7", padx=40, pady=20, 41 | command=lambda: Button_Click(7)) 42 | Button_8 = Button(root, text="8", padx=40, pady=20, 43 | command=lambda: Button_Click(8)) 44 | Button_9 = Button(root, text="9", padx=40, pady=20, 45 | command=lambda: Button_Click(9)) 46 | Button_0 = Button(root, text="0", padx=40, pady=20, 47 | command=lambda: Button_Click(0)) 48 | # Button_add=Button(root,text="+",padx=40,pady=20,command=Button_Click) 49 | Button_equal = Button(root, text="=", padx=40, 50 | pady=20, command=Button_Equal) 51 | Button_clear = Button(root, text="C", padx=40, 52 | pady=20, command=Button_Clear) 53 | 54 | # put buttons on the screen 55 | 56 | Button_1.grid(row=3, column=0) 57 | Button_2.grid(row=3, column=1) 58 | Button_3.grid(row=3, column=2) 59 | 60 | Button_4.grid(row=2, column=0) 61 | Button_5.grid(row=2, column=1) 62 | Button_6.grid(row=2, column=2) 63 | 64 | Button_7.grid(row=1, column=0) 65 | Button_8.grid(row=1, column=1) 66 | Button_9.grid(row=1, column=2) 67 | 68 | Button_0.grid(row=4, column=1) 69 | 70 | # Button_add.grid(row=5,column=0) 71 | Button_equal.grid(row=4, column=2) 72 | Button_clear.grid(row=4, column=0) 73 | 74 | root.mainloop() 75 | 76 | 77 | def main_code(): 78 | global guess 79 | global f 80 | while(guess != 0): 81 | if(f == 4): 82 | points = (10*(guess+1)) 83 | if (10-guess == 1): 84 | print( 85 | "You've successfully cracked the code in 1 guess and have recieved 100 points!") 86 | break 87 | else: 88 | print("You've successfully cracked the code in", 10 - 89 | guess, "guesses and have recieved", points, "points!") 90 | break 91 | 92 | f = 0 93 | if(f != 4): 94 | tkinter_version() 95 | user_code = [int(digit) for digit in str(user_guess)] 96 | 97 | for i in range(0, 4): 98 | if user_code[i] in code: 99 | if user_code[i] == code[i]: 100 | print(user_code[i], "is correctly placed") 101 | f += 1 102 | else: 103 | print( 104 | user_code[i], "is present in the code,but incorrectly placed") 105 | else: 106 | print(user_code[i], "is not in the required code") 107 | guess -= 1 108 | print("guesses left:", guess) 109 | 110 | while(guess == 0): 111 | if(f == 4): 112 | points = (10*(guess+1)) 113 | if (10-guess == 1): 114 | print( 115 | "You've successfully cracked the code in 1)guess and have recieved 100 points!") 116 | break 117 | else: 118 | print("You've successfully cracked the code in", 10 - 119 | guess, "guesses and have recieved", points, "points!") 120 | break 121 | else: 122 | print("you've failed to guess the code, the required code was", 123 | code[0], code[1], code[2], code[3]) 124 | break 125 | 126 | 127 | main_code() 128 | --------------------------------------------------------------------------------