Question

Python Programming Assignment 3 For this assignment you have been given a starting file to use...

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.

Homework Answers

Answer #1

The above question is implemented in python.(Code is explained using comments in the code)

OUTPUT1

OUTPUT2

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please make a Python program where the computer chooses a number and the player guesses the...
Please make a Python program where the computer chooses a number and the player guesses the number. And at the end, please report the number of attempts. You need an input, a random number, if statement, and a while loop. Please reference the pre-lab document in the Blackboard. Please prepare a Word document that has the Python code and the screen shot of your output. Here is a sample of what the output screen should look like. What is your...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program that comes up with a random number and the player has to guess it. The program output can be like this: I am thinking of a number between 1 and 20. Take a guess. 10 Your guess is too low. Take a guess. 15 Your guess is too low. Take a guess. 17 Your guess is too high. Take a guess. 16 Good job!...
use repl.it intro to C-programing no advance code also put good comment that i can know...
use repl.it intro to C-programing no advance code also put good comment that i can know whats this code use for  thank you use repl.it intro to C-programing no advance code also put good comment that i can know whats this code use for  thank you program for a game of hangman. Store the word to be guessed as individual characters of an array called word. The player must guess the letters belonging to word. If the user enters a valid letter,...
This is an intro to Java question. Please include pseudo code for better understanding. I am...
This is an intro to Java question. Please include pseudo code for better understanding. I am hoping to use this to do some reviewing. We are mainly focusing on input and output declarations and invoking API methods. Problem 6: Log It (10 points) Use API (Data Structure Algorithms) High-Low is a simple number guessing game where one player thinks of a random integer number between 0 to some maximum value and another player attempts to guess that number. With each...
You have been given the following lists of Color and their specific codes: Colors = ['Red','Blue','Green','Black','Yellow',‘Orange’,‘Purple’,‘Nocolor’]...
You have been given the following lists of Color and their specific codes: Colors = ['Red','Blue','Green','Black','Yellow',‘Orange’,‘Purple’,‘Nocolor’] Codes = [97,17,19,128,66,111,231,00] 1. Using codeDict, find the score for 'Nocolor'. 2. Add a code of 333 for 'Nocolor'. 3. Create a sorted list of all the codes in codeDict. 4. Update the name for 'Nocolor' to be ‘white’ 5. White was not a part of original colors list so, just Delete it and its code from codeDict. USE PYTHON CONSOLE AND SUBMIT THE...
C# Lab 7A: Thank you, Grace Hopper. You may have heard the story, but the programming...
C# Lab 7A: Thank you, Grace Hopper. You may have heard the story, but the programming term “bug” is attributed to the computer legend Grace Hopper who, after diagnosing the problem with a program, determined it was because a bug (in this case, a moth) physically wedged itself under a vacuum tube – causing the machine to no longer work. We’ve come a long way since then... For this part of the lab, you are going to write a program,...
Here is my problem: In Swift I, you used your knowledge at the time to create...
Here is my problem: In Swift I, you used your knowledge at the time to create a High / Low Guessing Game.   This week, you will update (or create a new version of) the game using the knowledge you've now attained. You will be creating a High/Low guessing game as outlined below: In this game: Your app will think of a random number between 0 and 999, and you (the player) will have to guess the number. You can use...
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance:...
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance: ⦁   A base class called Pet ⦁   A mix-in class called Jumper ⦁   A Dog class and a Cat class that each inherit from Pet and jumper ⦁   Two classes that inherit from Dog: BigDog and SmallDog ⦁   One classes that inherit from Cat: HouseCat The general skeleton of the Pet, Dog, and BigDog classes will be given to you (see below, "Skeleton", but...
Assignment 2 Programming Language: Python 2 Preface: Create a new python file with a name of...
Assignment 2 Programming Language: Python 2 Preface: Create a new python file with a name of your choosing (ex: “assignment2.py”). At the top of the file paste the following: import numpy as np import random class Base_Obstacle_Map(object):     def __init__(self, length, width):         self.map = np.zeros((length, width), dtype=np.uint8)     def add_obstacle(self, x, y):         self.map[x, y] = 1     def remove_obstacle(self, x, y):         self.map[x, y] = 0     def display_map(self):         try:             import matplotlib.pyplot as plt             plt.imshow(self.map)...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT