from GUI import MasterMindGUI import random class MasterServiceLayer(object): colours = ('r', 'b', 'g', 'y', 'p', 'o', 'w', 'v') # tuple containing all possible colour values colournames = {'r' : 'Red', 'b' : 'Blue', 'g' : 'Green', 'y' : 'Yellow', 'p' : 'Purple', 'o' : 'Orange', 'w' : 'White', 'v' : 'Violet'} # dictionary to identify colours #---------------------------------------------GETCOLOURS------------------------------------------------------------------------------------------- def getcolours(number): # gets a list of colours depending on how many colours they select at the start return MasterServiceLayer.colours[:number] #--------------------------------------------------ERROR--------------------------------------------------------------------------------- def invalidMove(): # if an error comes up this is what is displayed print("Invalid Input") raise RuntimeError() #--------------------------------------------------CALCCOLOURLST-------------------------------------------------------------------------------------------- def colourlst(number): # function used to make a string with the names of all the colours being used for when the program starts colourString = "The colour avaliable are: " # intalise variables names = "" Colourlst = list(MasterServiceLayer.getcolours(number)) # get Colourlst e.g. [r,b,g] for letters in Colourlst: names = MasterServiceLayer.colournames[letters] # refrence dictionary to use key and get the value returned colourString = colourString + names + "," #concatenate the string together names = "" return colourString #---------------------------------------------------CALCULATEPEGS------------------------------------------------------------------------------------ def calculatepegs(NoOfPegs, NoOfColours, peglst): # calculate the pegs to be guessed Colourlst = list(MasterServiceLayer.getcolours(NoOfColours)) # gets the colourlst e.g. [r,b,g] for i in range(NoOfPegs): # for the number of pegs peglst.append(random.choice(Colourlst)) # append a random colour #-------------------------------------------------------STARTSEQUENCE----------------------------------------------------------------------------- def start(NoOfPegs, NoOfColours, peglst): MasterMindGUI.StartGame(MasterServiceLayer.colourlst(NoOfColours)) MasterMindGUI.DrawGUI(NoOfPegs) # draw the text based user interface MasterServiceLayer.calculatepegs(NoOfPegs, NoOfColours, peglst) # calculate the pegs colours to be guessed #--------------------------------------------------GETGUESS------------------------------------------------------------------ def getGuess(NoOfPegs): # get the users guess counter = 0 # intalise variable guess = input("Guess: ") # get input from user if guess == 'quit': # if quit print("quit") exit() # close aplication else: guess = guess.split(',') # split the expression by user using commas if len(guess) != NoOfPegs: # if to small or to large a guess from pegs they decided MasterServiceLayer.invalidMove() # call error else: for value in range(0,len(guess),1): # for the values in their guess if guess[value] in MasterServiceLayer.getcolours(NoOfPegs): # if the letters are in teh colour list counter = counter + 1 # increment counter else: MasterServiceLayer.invalidMove() # error value return guess # return guess #-------------------------------------------------CHECKGUESS------------------------------------------------------------- def checkGuess(usrlst, complst): # checks the guess to see how many black and whites should be awarded blacks = 0 # intalise variables whites = 0 lstblack = [] lstwhite = [] for value in range(0,len(usrlst),1): # for the values form 0 to the legnth of the userlst-1 if usrlst[value] == complst[value]: # if they are the same blacks = blacks + 1 # Inidcates black pegs, right colour in right spot, increment it lstblack.append(usrlst[value]) # add it to the list else: lstwhite.append(usrlst[value]) # append it to the white list for letters in lstwhite: # for each letter in the white list if letters in complst and not letters in lstblack: # if the letter is in the computer generated list and is not in the black list whites = whites + 1 # increment the white value, white is right colour wrong spot. return blacks,whites #-----------------------------------------------------CHECKWIN-------------------------------------------------------------- def haveWon(blackpeg, whitepeg, guessNumber, NoOfPegs,UserGuesslistload, blacklstload, whitelstload): # check to see if you have won if blackpeg == NoOfPegs: # if number of black pegs is the same as number of pegs then print that they win and return true print("you have WON") return True else: MasterMindGUI.updateGUI(blackpeg, whitepeg, guessNumber, NoOfPegs,UserGuesslistload, blacklstload, whitelstload) # update the gui with the necessry information return False