Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This program will simulate the game of Mastermind. Assignment Description This is a board game in which one player chooses a secret combination of four

image text in transcribedimage text in transcribedimage text in transcribed
This program will simulate the game of Mastermind. Assignment Description This is a board game in which one player chooses a secret combination of four pegs (usually choosing from six colors). The other player than makes a series of guesses at that combination, trying to deduce what the solution is. Each guess is evaluated for its degree of correctness. A black peg is awarded for each guessed peg that has the correct color in the correct location - a white peg is awarded for each guessed peg that has the correct color, but i Instead of having two human players compete. this program will have a user compete with the computer. The user guesses a combination chosen by the computer. and vice versa. For example, here is a sample of the game: Welcome to Mastermind! Try to guess my combinationE What would you like to a pegs {correct color 1 pegs (correct color that would you like to l pegs (correct color I pegs {correct color that would you like to 2 pegs {correct color a pegs (correct color that would you like to l pegs (correct color a pegs {correct color What would you like to 3 pegs {correct color 8 pegs (correct color that would you like to 3 pegs {correct color 8 pegs {correct color that would you like to 3 pegs (correct color B pegs (correct color that would you like to You got it'. guess? BBRR in correct location) in incorrect location) guess? (5656 in correct location) in incorrect location) guess? WEE in correct location) in incorrect location) guess? PHBP in correct location) in incorrect location) guess? RHPG in correct location) in incorrect location) guess? RN36 in correct location) in incorrect location) guess? RNYG in correct location) in incorrect location) guess? RN66 My turn, to guess at yours! My guess is GPR in an incorrect location. No guessed peg can have more than one reward peg. How ilany black pegs [correct color in correct location)? 1 How many white pegs (correct color in incorrect location)? 1 My guess is 6M5 How many black pegs (correct color in correct location)? B How many white pegs (correct color in incorrect location)? 1 My guess is RGYB How marry black pegs {correct color in correct location)? 1 How many white pegs (correct color in incorrect location)? 3 I think I have it! Your combination is DGRY Play again? n A Tour of the Given Program File A program of this size is complex enough to require functions, and the functional design has already been provided in a le named mastermindpy . Download this le as the rst step in implementing this code. (Do NOT expect to complete this on time if starting completely from scratch with an empty fe!) The rst part simply obtains access to the random module to keep things interesting, and then denes the colors to be used for the simulated pegs. Dening this list separately would allow for simple changes if someone wanted to use different colors or a different number of colors. There follow four functions that relate to the combinations themselves (already completed): - isivalid .. assures that a combination describes four pegs of existing colors [does not allow things like '8' or 'BZZZ') - randomicombination picks any one possible combination. primarily to be used when compute picks a combination for the user to guess - inputicombination .. a function that allows the user to make a guess, making sure it is a valid guess - achombinations .. produces a list of all possibilities, which the computer will use to pick from when guessing The next two functions are not really part of what this program is intended to do, but are only present to help with testing and debugging your work. They are both involved in testing one of the three functions you are assigned to complete [described further in the next section). They are at least worth skimming to understand what that function is supposed to accomplish {as far as the rules of the Mastermind game). Be sure to understand why each comparison is supposed to come up with the indicated number of black and white pegs. Finally, at the very bottom of the program le, some code has already been provided to run and test your code. It is set up in such a way that each function can be implemented and tested one at a time (instead of having to write all the code rst). Of course, a meaningful game of Mastermind would need to be able to correctly evaluate a test, which is why some functions were already provided to test that. The other two you can probably selfevaluate when running them. The Assigned Functions checkguess: Application Concept: Evaluate a guess at the hidden combination and report its accuracy. Compare the two given strings, one representing a guessed solution and one representing the answer, A black peg will indicate any time they have matching colors in the same position (which is easily examined by considering that position). Since each colored peg can only be correct in one way, also eliminate those from future consideration simply by removing them from both lists. White pegs indicate thata correctly~colored peg was in an incorrect position. One can just consider each remaining peg (that did not score a black peg) and see if it appears in the other combination. Again, it would be necessary to remove a matching colored peg so it does not count more than once. For best results, identify all possible black pegs before identifying white pegs. Some testing code has been provided to verify that this function works correctly. SOME REMINDER HINTS: The for loop recitation showed that it is much safer to remove values from a list when visiting it from back to front instead of front to back. There are two functions that can take values out of a list (remove and pop). Both can be useful, and thoughtful design (and knowing how they differ) will prevent wrong guesses about which to use. user "guesses: Application Concept: Let the keyboard user play Mastermind by guessing at a combination chosen by the computer. The program will pick a random combination and then allow the user to make a series of guesses, giving feedback for each incorrect response. Hint: This is very similar to a program example elsewhere in the course. computer_guesses: Application Concept: Let the program gure out a combination chosen by the keyboard user. Of course, the user will not actually type their combination into the programo They will just evaluate the computer's proposed solutions according to the rules of the game. Also, the computer's guesses will not be completely random (there are over 1000 possibilities). Instead, it will maintain a list of viable solutions, and trim that list after each guess. It will be able to gure out which combinations cannot be correct if they woufd have produced a different outcome than the user proposes. For example, if the computer tried RYBB and got back one white peg, it would know that RYBB cannot be the correct solution (that would have 4 black pegs). It would also know that RYBG cannot be the correct solution (that would have three black pegs]. There is no need for any complex thought here .. simply check the proposed guess against all the viable candidates and see which have a matching evaluation. Be careful in trimming the list of viable candidates, as demonstrated in the for lop recitation. If successful, each loop cycle should always eliminate over 50% of the possibilities as being incorrect. and deduce the correct combination in under ten trieso

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

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions