Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Mastermind is a two-player board game that was popular in the 1970s and 1980s. T

ID: 3777449 • Letter: M

Question

Mastermind is a two-player board game that was popular in the 1970s and 1980s. The task is to write program that would play Mastermind against the user. We will use the following version of the game. The user thinks of a code consisting of four numbers, each number is between 1 and 6 inclusive (one of 1,2, 3, 4, 5, 6) (We will use numbers instead of 6 colors used in the original game). Digit repetition is allowed. For instance, possible codes include llll, 3544, 6225, 5432, ctc. The order of numbers in a code matters, that is, 2345 and 3425 are different codes. The goal of the game for the second player is to guess the code using as the smallest number of guesses. Your program should discover the code by making guesses and getting user responses. We will use integers for user responses, too. Responses will be represented as two digit numbers. The first digit is the number of digits in the guess that are comect and are in their correct positions. The second digit is the number of digits in the guess that are present in the code but are at different positions in the co For example, i the code is 5662 and the guess is 3656, then response is 2. This means that one digit (6) is correct and is in its correct position and two digits (5 and 6) are correct but are not in their correct positions. Response 40 means that the code is guessed correctly (4 digits are correct and are in their correct positions). Response 0 means that no digits in the guess are correct. How many different codes are their? There are 4 digits in the sequence, and each can be one of 6 possible numbers, so by the product rule, there are 6-6 6-6 l,296 possible codes. H do we get this cut down to one You need to implement the following strategy in your program. It consists of the following steps l. Generate all possible 4-digit sequences of numbers 1 through 6. There are 1,296 of them). You may store them in an array. Mark all of them as possible 2. Write a method that given a 4-digit code and a 4-digit g would return correct 2-digit response. It would be useful to have a separate method for that. One implementation of the method in Java is provided: getResponse. You may use the provided method or write your own method that does it. 3. Pick one possible sequence as a guess (et us denote the guess G). Output it and get user feedback for it 4. If the user response is 40, then the code is found. End of game 5. If the user response is not 40, then do the following: a. Go through all remaining possible sequences. b. For each possible sequence S check if it is still possible. Use the method from step 2 to compute the response given S as the code and the last guess G as the guess c. If the response computed in step b is different from the user response, then mark the sequence S as impossible. d. After processing all the remaining possible sequences go to step 3 You need to keep track of the number of the remaining possible sequences and output it after each unsuccessful gues Please keep in mind that it is possible for the user to make mistakes in responses. Then, at some point there remaining possible sequences. In such situations your program should output that the may be no ce does not exist (sec the sccond sample run below) A sample run of your program may look like the following (program output is in black, user input is in green) Please think of a 4-digit code and let me guess it.

Explanation / Answer

public category Mastermind spherical = 1;
int[] zahl = getZufallsZahl(difficulty);
int[] tries = Try(round,difficulty);
int[] hits = getHits(zahl,tries);
whereas (hits[0]!=difficulty)
int[] zahl = new int[difficulty];
int same=1;
for (int i=0; i<difficulty; i++) is kind of ugly owing to the suss out constant variable, however I did not wish to use a prospect with a label (outerloop: for [...] break outerloop;)
do
}
} whereas (same==1);
}
come back zahl;
}

// Below operate is copy pasted! :O (yes i'm ashamed for it, however it's "only" a niche-feature, therefore yea :/)
personal static String ordinal(int i) ;
switch (i the troubles 100) {
case 11:
case 12:
case 13:
come back i + "th";
default:
come back i + sufixes[i the troubles 10];
}
}
// on top of operate is copy pasted! :O

personal static int[] Try(int spherical, int difficulty) {
int[] intry = evalTry(getTry(round));
whereas (!verifyTry(intry) || intry.length<(difficulty-1) || intry.length>difficulty)
come back tries;
}
personal static String getTry(int round)
return in;
}
personal static int[] evalTry(String in) number.parseInt(in.substring(i,i+1));
}
come back intry;
}
personal static Boolean verifyTry(int[] intry) {
for (int i=0; i<intry.length; i++) {
for (int j=i-1; j>=0; j--) {
if (intry[i] == intry[j]) {
come back false;
}
}
}
come back true;
}
personal static int[] getHits(int[] zahl,int[] tries) {
// zero for direct, one for indirect.
int[] hits = new int[2];
for (int i=0; i<zahl.length; i++)
}
for (int i=0; i<zahl.length; i++)
}
}
come back hits;
}
personal static String[] motivator=