Answered step by step
Verified Expert Solution
Link Copied!

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 5 guesses to try and guess thecombination. After 5 guesses, they lose.Suppose the chosen combination is RBGO (red, blue, green, orange).The user guesses VOGY (violet, orange, green, yellow)The computer would output _WR_(underscore, white, red, underscore). This is because violet is notpresent, orange is, but its not in position 2, green is, and in position 3, and then yellow is not present. The user then guesses BRGO (blue, red, green, orange)The 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 5 guesses, and the final, 5th 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 1: RGBYW__WGuess 2: WRYOWRW_Guess 3: YRWMRRW_Guess 4: YRVMRRR_Guess 5: 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 1: RGBY_RW_Guess 2: YRGB__WRGuess 3: YGBR_RW_Guess 4: WOMV_W_WGuess 5: OMRYW___You 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 1: RGBYR___Guess 2: RRGBColors cannot be repeated, try againGuess 2: MNOPN is not a valid color, try againGuess 2: RMOVRR_WGuess 3: RMVYRRR_Guess 4: 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 5 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 4 characters, or less than 4. 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.count(l) will return the number 2, because there are 2 occurrences of l in the string hello!
Codes to start with:legal_colors =['R','G','B','Y','W','O','M','V']
def generate_color_sequence():
import random
random.seed()
sequence = random.sample(range(len(legal_colors)),4)
return [legal_colors[i] for i in sequence]
colors = generate_color_sequence()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Infrastructure For Medical Research In Databases

Authors: Thomas Heinis ,Anastasia Ailamaki

1st Edition

1680833480, 978-1680833485

More Books

Students also viewed these Databases questions

Question

2. Define identity.

Answered: 1 week ago

Question

1. Identify three communication approaches to identity.

Answered: 1 week ago

Question

4. Describe phases of majority identity development.

Answered: 1 week ago