Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You will be writing the game of Mastermind. If you are not familiar with this game, an explanation isgiven, and an example game is shown.
You will be writing the game of Mastermind. If you are not familiar with this game, an explanation isgiven, and an example game is shown. If you are, please note we are simplifying this game and notstrictly following the rules, so please read closely.
In Mastermind a player tries to guess the correct ordering of four colored pegs, out of eight possiblecolor choices. Another player gives them hints about their guess and tells them if it iscorrect In ourprogram, the computer will play the role of the player who selects the color combination and gives hintsto the player who is guessing. The computer will randomly select a sequence of colors and the user willenter a guess. Once a guess is entered, the computer will indicate if a color is correct and in the correctlocation with a red peg. A white peg indicates that a color is correct but in the wrong location. No pegindicates the color is not used. Colors cannot be repeated.
The code you have been given will generate a random sequence of colors this sequence is what theplayer will attempt to guess The code you write needs to get the user input, validate the user input,and produce the output for the pegs. An example game is now shown:
The available colors are Red, Blue, Green, Yellow, Violet, Orange, White, Magenta. The user will enterjust the first character of the color case does not matter and the computer will output an underscorefor no peg, R for a red peg, and W for a white peg. The user has guesses to try and guess thecombination. After guesses, they lose.Suppose the chosen combination is RBGO red blue, green, orangeThe user guesses VOGY violet orange, green, yellowThe computer would output WRunderscore white, red, underscore This is because violet is notpresent, orange is but its not in position green is and in position and then yellow is not present. The user then guesses BRGO blue red, green, orangeThe computer would output WWRR so first two are right colors, wrong spots, final two are correct Hopefully after this, the user would realize that the correct guess is now RBGO and would guess that.The computer will output You win! and end.If they made it to guesses, and the final, th guess was not correct, the computer will output You loseand the program will end.
Sample OutputThis is from a winning game:Possible colors are R G B Y W O M VPlease enter your guess with no spaces between colors. Colors cannot be repeatedGuess : RGBYWWGuess : WRYOWRWGuess : YRWMRRWGuess : YRVMRRRGuess : YRVWYou win!
And this is output from a losing game:Possible colors are R G B Y W O M VPlease enter your guess with no spaces between colors. Colors cannot be repeatedGuess : RGBYRWGuess : YRGBWRGuess : YGBRRWGuess : WOMVWWGuess : OMRYWYou lose!
Output showing invalid input and error messagesPossible colors are R G B Y W O M VPlease enter your guess with no spaces between colors. Colors cannot be repeatedGuess : RGBYRGuess : RRGBColors cannot be repeated, try againGuess : MNOPN is not a valid color, try againGuess : RMOVRRWGuess : RMVYRRRGuess : RMVWYou win!
Hints and Tips
You can override colors at the start of your code to help you debug. You can set it to anythingyou desire to validate your output based on the users guess.
You need to validate the user input. If the user enters in a letter that is not a valid color, youshould print out an error message and have them retry the input. This does not count againsttheir choices. Colors cannot be repeated in a guess, so this should also generate an errormessage if the user enters in something that has a color repeated. The user cannot enter inmore than characters, or less than These conditions should generate an error message.
User input can be uppercase or lower case. You should probably convert their input touppercase the randomly generated color sequence is all uppercase, so it will be easiest tovalidate against that if the user input is converted to uppercase.
There is a string method, called count that you can use to count the number of occurrences ofa letter in a string. As an example: data hello data.countl will return the number because there are occurrences of l in the string hello
Codes to start with:legalcolors RGBYWOMV
def generatecolorsequence:
import random
random.seed
sequence random.samplerangelenlegalcolors
return legalcolorsi for i in sequence
colors generatecolorsequence
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started