the screenshot is the assignment requirements. the python files are given by my professor to make the work easier and shorter.
from PySide2.QtWidgets import * from PySide2.QtCore import * from PySide2.QtGui import * from PySide2.QtMultimedia import * class MasterMind(QWidget): def __init__(self,*args,**kwargs): super().__init__(*args,*kwargs) layout = QGridLayout() # Create GUI Components with self as parent # Connect button pressing to altering/set colors # -- addWidget to layout self.setLayout(layout) def submit(self): pass # Check the four colors - feedback # Update Colors for acurate/partial def click(self): pass #Update button colors app = QApplication() v = MasterMind() v.show() app.exec_() import random class Mastermind(): colors =["red", "green", "yellow", "blue", "black", "white"] def __init__(self): self.code=[None]*4 self.newGame() def guess(self, *args): if len(args)!=4: raise Exception("Not enough pegs guessed") for g in args: if g not in Mastermind.colors: raise Exception("Not a valid color") self.prev_guesses.append(",".join(args)) self.__createResponse() def __createResponse(self): lastGuess = self.prev_guesses[-1].split(",") numInRightSpot = 0 numInSome = 0 partialColors = [] exactSpotMatches = [] for i, guess in enumerate(lastGuess): if guess == self.code[i]: numInRightSpot+=1 exactSpotMatches.append(i) for i, guess in enumerate(lastGuess): if guess != self.code[i]: colorFound = False for j, c in enumerate(self.code): colorFound = colorFound or (guess == self.code[j] and j not in exactSpotMatches) # Set to true if somewhere in there if guess in partialColors: colorFound=False # do not double count a color match if colorFound: # correct color, wrong spot numInSome += 1 partialColors.append(guess) self.prev_responses.append({"exact":numInRightSpot,"partial":numInSome}) def newGame(self): self.__resetCode() self.prev_guesses=[] self.prev_responses=[] def getGuessCount(self): return len(self.prev_guesses) def getLastResponse(self): if len(self.prev_responses)==0: return {"exact":0,"partial":0} return self.prev_responses[-1] def __resetCode(self): self.code[0]=Mastermind.colors[random.randrange(6)] self.code[1]=Mastermind.colors[random.randrange(6)] self.code[2]=Mastermind.colors[random.randrange(6)] self.code[3]=Mastermind.colors[random.randrange(6)] def displayCurrentState(self): for i, guess in enumerate(self.prev_guesses): print("GUESS:",guess,"\n\t","Response:",self.prev_responses[i]) if __name__=="__main__": def lookupColor(letter): letter=letter.upper() if letter=="R": return "red" if letter=="G": return "green" if letter=="Y": return "yellow" if letter=="B": return "blue" if letter=="K": return "black" if letter=="W": return "white" # Full Game game = Mastermind() while game.getGuessCount()!=10 and game.getLastResponse()['exact']!=4: print("\n\n") game.displayCurrentState() print(10-game.getGuessCount(),"guesses left") print("\nColors: (R:red,G:green,B:blue,Y:yellow,W:white,K:black)") valid = False while not valid: guess = input("Enter your Guess as ####:") glist = [lookupColor(g) for g in guess] try: game.guess(*glist) except Exception as e: print(e,"\n") else: valid=True if game.getLastResponse()['exact']==4: print("Congrats YOU WIN!") else: print("YOU LOSE!!!")