In the game of Mastermind, a player is only given a finite number of guesses to
ID: 3545137 • Letter: I
Question
In the game of Mastermind, a player is only given a finite number of guesses to try to identify the hidden combination (such as twelve guesses). Often that is the only constraint in playing the game.
But some players might make a competition with each other to see who can guess the other's combination in fewer tries. In this case, the problem is not only to come up with a strategy that can find the answer within a specified limit, but to find one that is likely to require the minimal number of guesses.
Here is where the computer comes in -- one can write a program that would try out different guessing strategies, and see how they work out. Since a computer can do the analysis and computations more rapidly than a person, it could just pretend to play Mastermind on our behalf using any strategy we choose, and tell us how long it took to do that.
Of course, it would be extremely difficult to teach the computer to reason along the same lines as a person. For example, if we guessed a combination 1111 and got one black peg, we would make a mental note that the answer has exactly one 1 in it, and then proceed to make other guesses with that one fact in mind. If we next guesses 1222 and got one white peg, we would know there were no 2's, and that the single 1 is not in the first position. But how to keep track of such information after a series of guesses would be rather hard.
Fortunately, for a computer simulation with an array, we can record all of our known facts in a different way. We just maintain a list of all possible answers that there could be, and then remove numbers from the list that could no longer be the solution. If our first guess tells us there is exactly one 1 digit, we would remove all the numbers that do not have that feature. When we find out there are no 2's, we eliminate all the values that contain 2's. Eventually, the only number left would be the correct answer.
With our list approach, which contain a whole lot of possibilities in order beginning with 1111, 1112, 1113, 1114, etc., our next guess would always be the first in the list.
To implement this one, let us just pick any three possible answers and guess them, temporarily ignoring how many black pegs and white pegs they earn us. Only after making those guesses will we trim the list of possibilities, then proceeding as the second strategy above.
These are the sample results from the current implementation:
NOTE: This program allows each digit to go up to 8 instead of 6. Even though there are 4096 possible answers, it still finds them rather rapidly.
The assigned program must implement all of the following functions. Additional ones are permitted as desired -- these below are required. Future assignments will not detail the functions as below -- but will instead require the students to design their own function descriptions in advance to writing the program.
Explanation / Answer
#include // belongs to the beginning of every program #include // header #include using namespace std; void playMastermind() // play Mastermind function for the whole game { int rollDie ( int sides ); // declaring the functions used void evaluate ( int answer, int combo ); bool play = true; // bool setting play to true while ( play == true ) // resets while loop if play is equal to true { int digitOne; // initialize digit variables int digitTwo; int digitThree; int digitFour; digitOne = rollDie (6); // finding random values for the digits by calling the rollDie function digitTwo = rollDie (6); digitThree = rollDie (6); digitFour = rollDie (6); digitOne *= 1000; // finding the actual number with the digits combined digitTwo *= 100; digitThree *= 10; digitFour *= 1; int correct; // initializing the correct answer that the computer decides on correct = digitOne + digitTwo + digitThree + digitFour; // the correct answer int combo; // initializing the variable that the player guesses int tries = 0; // initializing the number of tries to 0 bool answer = false; // bool for when the answer is false while ( tries < 12 && answer == false ) // while loop asking for the player to guess a number { cout = 12 ) // if-statement for when the user reaches his limit of tries { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.