Mastermind Game Using Python

How to Create Mastermind Game Using Python

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.

Mastermind Game Using Python

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:

  1. open any python code Editor.
  2. Import  the required  module .
  3. Copy the code for the Mastermind Game Using Python, which I provided Below in this article, and save it in a file named “” (or any other name you prefer).
  4.  Run this 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

Mastermind Game Using Python

 complete Mastermind Game Using Python project code (Copy the code and Run )👇👇

from tkinter import *
class medium:
    def user(self,color): # takes user' choice
    def __init__(self): # generates random palette
        a=['#270101', '#F08B33', '#776B04', '#F1B848', '#8F715B', '#0486DB', '#C1403D', '#F3D4A0']
        import random
        while n!=0: 
            if p not in self.b:
    def compare(self,g,l1):
        l=[] # hints           
        for x in range(4):
            if l1[x]==g[x]:
            elif l1[x]in g:
        return l
class MasterMind():
    def __init__(self, root):
        self.gen=obj.b  # generated color combo
        self.colors = ['#270101', '#F08B33', '#776B04', '#F1B848', '#8F715B', '#0486DB', '#C1403D', '#F3D4A0']
        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
        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)
        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)
                    ccol += 1
                elif len(self.user_choice) == 4:
                    self.user_choice[-1].grid(row=cro, column=ccol)
                    ccol += 1
                    ccol = 2
                    cro = cro-1
                    obj.user(self.code) # send the user's choice
          ,self.gen) #get the hints
                    if self.key==['red','red','red','red']:
                        self.hint(root, self.key)
                        self.hint(root, self.key)
                        self.code = []
                        self.user_choice = []
                if f:
                    l=Label(root,text="You are a LOSER!!!!        ANSWER:")
                    for i in self.gen:                        
                        b=Button(root,bg=i,height=1, width=5, relief=SUNKEN)
    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)

Output 👇👇


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💝.

Leave a Reply