How to Create Mastermind Game Using Python
Hello coder, welcome to the codewithrandom blog. It’s time to code a game in Python. In this article, we create a Mastermind Game Using Python. Mastermind is a classic code-breaking game that can be implemented in Python. The game is played with a board consisting of 10 rows and 4 columns.
The first player, also known as the codemaker, chooses a secret code consisting of 4 colors from a set of 6 colors. The colors can be repeated in the secret code. The second player, also known as the codebreaker, tries to guess the secret code. The codebreaker has 10 attempts to guess the secret code. If the code is not guessed within 10 attempts, the game is over and the codemaker wins. If the code is guessed correctly, the codebreaker wins.
Before Starting Mastermind Game Using Python coding, check this video to know what we are going to make:
Project Requirements
In this Mastermind Game Using Python project We need to import tkinter module.
from tkinter import *
To play the Mastermind Game Using Python, you can follow these steps:
- open any python code Editor.
- Import the required module .
- Copy the code for the Mastermind Game Using Python, which I provided Below in this article, and save it in a file named “mastermind.py” (or any other name you prefer).
- Run this mastermind.py to start the game.
That’s it! Have fun playing the Mastermind Game Using Python.
How to Build a Billing Software Application using python | Python Project
How to Build a YouTube Video Downloader using Python
How to Create A Number Guessing Game in python | python project
complete Mastermind Game Using Python project code (Copy the code and Run )👇👇
from tkinter import * class medium: def user(self,color): # takes user' choice self.color=color def __init__(self): # generates random palette a=['#270101', '#F08B33', '#776B04', '#F1B848', '#8F715B', '#0486DB', '#C1403D', '#F3D4A0'] import random self.b=[];n=4; while n!=0: p=random.choice(a) if p not in self.b: self.b.append(p) n-=1 def compare(self,g,l1): l=[] # hints for x in range(4): if l1[x]==g[x]: l.append('red') elif l1[x]in g: l.append('gray') return l class MasterMind(): def __init__(self, root): obj=medium() self.gen=obj.b # generated color combo self.colors = ['#270101', '#F08B33', '#776B04', '#F1B848', '#8F715B', '#0486DB', '#C1403D', '#F3D4A0'] root.geometry('390x600') for y in range(20): Grid.rowconfigure(root, y, weight=1) for x in range(8): Grid.columnconfigure(root, x, weight=1) self.palette = [] # display of palette n,c=0,0 for i in self.colors: self.palette.append(Button(root, bg=i, height=1, width=5, relief=SUNKEN)) self.palette[n].grid(row=20, column=c) n+=1;c+=1; self.palette[0].config(command=lambda: self.guess(root, self.palette[0]['bg'],obj)) # binding function to palette self.palette[1].config(command=lambda: self.guess(root, self.palette[1]['bg'],obj)) self.palette[2].config(command=lambda: self.guess(root, self.palette[2]['bg'],obj)) self.palette[3].config(command=lambda: self.guess(root, self.palette[3]['bg'],obj)) self.palette[4].config(command=lambda: self.guess(root, self.palette[4]['bg'],obj)) self.palette[5].config(command=lambda: self.guess(root, self.palette[5]['bg'],obj)) self.palette[6].config(command=lambda: self.guess(root, self.palette[6]['bg'],obj)) self.palette[7].config(command=lambda: self.guess(root, self.palette[7]['bg'],obj)) self.user_choice = [] # stores the widget self.code = [] # stores the colors self.key = [] # stores the hints global ccol, cro ccol,cro = 2,19 def guess(self, root, choice,obj): global ccol global cro f=True # boolean flag if cro != 1: self.user_choice.append(Button(root, bg=choice, height=1, width=5, relief=RAISED)) if len(self.user_choice) < 4: self.user_choice[-1].grid(row=cro, column=ccol) self.code.append(self.user_choice[-1]['bg']) ccol += 1 elif len(self.user_choice) == 4: self.user_choice[-1].grid(row=cro, column=ccol) self.code.append(self.user_choice[-1]['bg']) ccol += 1 ccol = 2 cro = cro-1 obj.user(self.code) # send the user's choice self.key=obj.compare(self.code,self.gen) #get the hints if self.key==['red','red','red','red']: f=False self.hint(root, self.key) l=Label(root,text="CONGRATULATIONS!!!") l.grid(row=0,columnspan=8) else: self.hint(root, self.key) self.code = [] self.user_choice = [] else: if f: l=Label(root,text="You are a LOSER!!!! ANSWER:") l.grid(row=0,columnspan=4) c=5 for i in self.gen: b=Button(root,bg=i,height=1, width=5, relief=SUNKEN) b.grid(row=0,column=c) c+=1 global hcol, hro hcol,hro = 8,19 def hint(self, root, key): global hcol, hro a = [] for i in key: a.append(Label(root, bg=i,relief=SUNKEN)) a[-1].grid(row=hro, column=hcol, sticky=E) hcol += 1 hro -= 1;hcol = 8; master = Tk() M = MasterMind(master) master.mainloop()
Output 👇👇
Summary
Hurray! You have successfully Create the a popular Mastermind Game Using Python . We learned to create amazing python project , Mastermind Game Using Python . but it’s also popular amongst the python developers to develop games in python. Hope you enjoyed building with us! Visit our homepage and you get lot’s of projects💝.