Python Programming Assignment 3
For this assignment you have been given a starting file to use for the assignment. Some code is already in this file, and it provides functionality you’ll be using to complete this assignment. Your code needs to go in a specific place, marked
#### Your Code Here
You code must go under this comment, and you should not modify the other parts of the file, except to add a block comment at the top with your name, date, etc.
You will be writing the game of Mastermind. If you are not familiar with this game, an explanation is given and an example game is shown. If you are, please note we are simplifying this game and not strictly following the rules, so please read closely.
In mastermind a player tries to guess the correct ordering of four colored pegs, out of eight possible color choices. Another player gives them hints about their guess and also tells them if its correct. In our program the computer will play the role of the player who selects the color combination, and gives hints to the player who is guessing. The computer will randomly select a sequence of colors and the user will enter a guess. Once a guess is entered, the computer will indicate if a color is correct and in the correct location with a red peg. A white peg indicates that a color is correct, but in the wrong location. No peg indicates 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 the player 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 enter just the first character of the color (case does not matter) and the computer will output an underscore for no peg, R for a red peg, and W for a white peg. The user has 5 guesses to try and guess the combination. 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 not present, 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 lose” and the program will end.
Sample Output
This is from a winning game:
Possible colors are R, G, B, Y, W, O, M, V
Please enter your guess with no spaces between colors. Colors
cannot be repeated Guess 1: RGBY
W__W
Guess 2: WRYO
WRW_
Guess 3: YRWM
RRW_
Guess 4: YRVM
RRR_
Guess 5: YRVW
You win!
And this is output from a losing game:
Possible colors are R, G, B, Y, W, O, M, V
Please enter your guess with no spaces between colors. Colors
cannot be repeated Guess 1: RGBY
_RW_
Guess 2: YRGB
__WR
Guess 3: YGBR
_RW_
Guess 4: WOMV
_W_W
Guess 5: OMRY
W___
You lose!
Output showing invalid input and error messages
Possible colors are R, G, B, Y, W, O, M, V
Please enter your guess with no spaces between colors. Colors
cannot be repeated Guess 1: RGBY
R___
Guess 2: RRGB
Colors cannot be repeated, try again
Guess 2: MNOP
N is not a valid color, try again
Guess 2: RMOV
RR_W
Guess 3: RMVY RRR_
Guess 4: RMVW You win!
Hints and Tips
There are two pre-defined lists in the starter code, legal_colors, which has a list of colors that are allowed for input, and colors, which is the list 4 colors the user is trying to guess. The provided code generates a list with the random 4-character color sequence in it. Try printing it out at the start of your code to see what the values are and to verify your code produces the correct output for the guesses.
You can override colors at the start of your code to help you debug. You can set it to anything you desire to validate your output based on the user’s guess.
You need to validate the user input. If the user enters in a letter that is not a valid color, you should print out an error message and have them retry the input. This does not count against their 5 choices. Colors cannot be repeated in a guess, so this should also generate an error message if the user enters in something that has a color repeated.
User input can be uppercase or lower case. You should probably convert their input to uppercase – the randomly generated color sequence is all uppercase, so it will be easiest to validate against that if the user input is converted to uppercase.
Start with a basic program and add more features as you go. It will be easier to test and debug. Start with a loop that lets the user guess the color sequence 5 times, if its right prints “You win”. After that, add in the red/white peg hints. Once that’s working, try adding in the user input validation. If you start trying to do everything from the start you may become overwhelmed!
User input validation is not covered until next week (week 6) so leave that part for last!
To generate the peg sequence (the response to player input) you can build a string and output it, or you can print out the pegs one at a time. If you want to print them out one by one, you’ll
need to use the print function like this:
print(peg, end=’’)
by adding end=’’ to the end of the print function, you are indicating you do NOT want it to automatically insert a line break at the end. This will allow you to output letters/strings one at a time onto the line without adding the line break until you are ready.
The above question is implemented in python.(Code is explained using comments in the code)
OUTPUT1
OUTPUT2
Get Answers For Free
Most questions answered within 1 hours.