This is the third part of the program I HAVE TO WRITE ALL TH

This is the third part of the program. I HAVE TO WRITE ALL THESE CLASSES!!!!

MasterMindAIConsistent

Use the following algorithm to create a decent AI that will decipher the secret code in 8 attempts or fewer. The first Guess is randomly generated just as is done for MasterMindAIRandom. The second Guess is initially random, but the Guess must satisfy a test before it is returned as the official second Guess. This \"trial\" Guess is compared to the first, randomly generated Guess. The comparison involves computing the black and white buttons as if the second Guess was the secret code. If the number of black and white buttons in the test comparison does not match the actual black and white buttons returned by MasterMind after the first Guess (obtained by comparison to the secret code), then it is impossible for the second Guess to be the secret code, and a new randomly generated trial Guess is obtained. The process repeats in this manner until a suitable second Guess is found and returned to MasterMind.

As the number of Guesses increases, the constraints on new Guesses also increases as a new Guess must be compared to all of the previous Guesses, generating the identical black and white button results that MasterMind assigned to each previous Guess by comparing them to the secret code. Eventually, there are only a few Guesses that can satisfy all of the constraints, one of which is the secret code.

MasterMindAIMiniMax

Another approach that never takes more than 6 attempts but takes more time for early Guesses is called MiniMax. The idea is to create a List containing all possible Guesses (how many are there?). The first Guess in the List has all possible combinations of black and white buttons (how many are there of these?) assigned to it, one at a time. Since we don\'t know what button combination would be returned for that Guess, we must consider them all. A pass through all other Guesses for each of the black and white button assignments is made, counting up the number of other Guesses in the List that would be eliminated as possible secret codes. That is, we increment the count if comparing the two Guesses does not generate the specific black and white button count under consideration. Thesmallest of these eliminations (the mini part) is stored, along with the Guess that generated it. The AI is keeping track of the worst case that can occur for that particular Guess.

The second Guess in the List goes through the same process. If the smallest number of eliminations for this Guess is larger (the max part) than the previous smallest number of eliminations, store this new value and the Guess that generated it, otherwise keep the previous values. The AI is keeping track of the best of the worst cases for all of the Guesses.

At the end of the process, the computer player knows which Guess will eliminate the most other Guesses in the worst case. Make this Guess. Now remove from the List of Guesses those that are not consistent with the black and white buttons returned from MasterMind (as well as the Guess just made). Repeat the entire process for the second Guess.

The first Guess takes a long time to compute, but it is always the same (why?). To save a substantial amount of time, hardcode the first Guess to be red, blue, green, purple (or any four different colors).

MasterMindAI Switching

As long as your computer players have access to all of the Guesses made so far (and they do), they should be able to pick up from any point, allowing the user to switch between AIs and between human player and any of the AIs without any problems. A commented out method, int changeAI(int x, int y), can be used to detect when the user wants to change AI/human player mode.

Solution

import java.util.*;
import java.text.*;

class MasterMind {

private final static int MAX_ATTEMPTS = 10; // maximum number of attempts
  
// Convert an integer to its corresponding colour letter
private static char intToColour(int i) {
char c = \' \';

switch (i) {
case 0: c = \'R\'; break; // red
case 1: c = \'G\'; break; // green
case 2: c = \'B\'; break; // blue
case 3: c = \'C\'; break; // cyan
case 4: c = \'Y\'; break; // yellow
case 5: c = \'M\'; break; // magenta
default: System.err.println(\"Error: no such colour!\");
System.exit(1);
}
return c;
}

// Generate four pegs for the secret code
private static MMCode generateSecretCode() {
MMCode code = new MMCode();
Random random = new Random();

code.setColour(1, intToColour(random.nextInt(6)));
code.setColour(2, intToColour(random.nextInt(6)));
code.setColour(3, intToColour(random.nextInt(6)));
code.setColour(4, intToColour(random.nextInt(6)));

return code;
}

// Convert 4-character string to MMCode object
private static MMCode stringToMMCode(String str) {
MMCode code = new MMCode();

code.setColour(1, str.charAt(0));
code.setColour(2, str.charAt(1));
code.setColour(3, str.charAt(2));
code.setColour(4, str.charAt(3));

return code;
}

// Instruction
private static void printInstruction() {
System.out.println(\"Welcome to CS1101 MasterMind!\");
System.out.println(\"Enter a string consisting of 4 letters, which can be \");
System.out.println(\"R (Red), B (Blue), G (Green), Y (Yellow), C (Cyan) or M (Magenta).\");   
System.out.println(\"Enjoy!\");
}
  
//----------------------------------------------------------
// main method
//----------------------------------------------------------
public static void main (String [] args) {

MasterMindGUI start = new MasterMindGUI(); // -- GUI enhancement

Scanner scanner = new Scanner(System.in);
DecimalFormat df = new DecimalFormat(\"00\");

MMCode secretPegs, secretPegsCopy, guessPegs;
String guess;
int attempt = 0;
int sinks = 0, hits = 0;

printInstruction();
  
// generate a secret code
secretPegs = generateSecretCode();

// The following two statements are used to display the secret code
// so that we can verify the computation of hits and sinks
// Once testing is done, we may remove these 2 statements.
/*
System.out.println(\"\\t\\t\\tSecret code: \" + secretPegs);
start.addSecretCode(secretPegs); // -- GUI enhancement
*/

do {
attempt++;

// make a copy of secret code
secretPegsCopy = new MMCode(secretPegs);

System.out.print(\"Enter your guess: \");
guess = scanner.nextLine();

while (guess.length() != 4) {
System.out.print(\"Enter your guess: \");
guess = scanner.nextLine();
}
guessPegs = new MMCode();
guessPegs = stringToMMCode(guess);

System.out.print(\"\\t\\t\\t Guess #\" + df.format(attempt)
+ \": \" + guessPegs);

start.addGuessCode(attempt, guessPegs); // -- GUI enhancement

sinks = secretPegs.countSinks(guessPegs);
hits = secretPegs.countHits(guessPegs);

System.out.println(\"\\t Sinks = \" + sinks + \"; Hits = \" + hits);

start.addSinksAndHits(attempt, sinks, hits); // -- GUI enhancement

// revert secret code
secretPegs = new MMCode(secretPegsCopy);

} while (attempt < MAX_ATTEMPTS && sinks < 4);

System.out.println();
if (sinks == 4)
System.out.println(\"Congratulations!\");
else
System.out.println(\"Sorry you didn\'t get it. The secret code is \"
+ secretPegs + \".\");

start.addSecretCode(secretPegs); // -- GUI enhancement
System.out.println(\"Hope you have enjoyed the game. Bye!\");
}

}

or

import java.util.Random;

public class MastermindGame {

// Variables
private int[][] guesses;
private int[][] evaluations;
private int[] correctCode;

private int numberOfColours;
private int numberOfGuesses;
private int pegsPerRow;
private int attemptNumber;

public final int CORRECT_COLOUR = 1;
public final int CORRECT_POSITION = 2;

// Constructor
public MastermindGame(int aNumberOfColours, int aPegsPerRow,
int aNumberOfGuesses) {

numberOfColours = aNumberOfColours;
numberOfGuesses = aNumberOfGuesses;
pegsPerRow = aPegsPerRow;

// Size arrays appropriately
guesses = new int[numberOfGuesses][pegsPerRow];
evaluations = new int[numberOfGuesses][pegsPerRow];
correctCode = new int[pegsPerRow];

// Generate a random code
createRandomCode();

}

// Generate a random code to solve
private void createRandomCode() {
Random random = new Random();

for (int i = 0; i < correctCode.length; i++) {
correctCode[i] = random.nextInt(numberOfColours) + 1;
}

}

// Enter a guess at the code
public void enterGuess(int[] userGuess) {
if (attemptNumber < numberOfGuesses) {
if (userGuess.length == pegsPerRow) {
for (int i = 0; i < userGuess.length; i++) {
guesses[attemptNumber][i] = userGuess[i];
}
}

evaluateGuess();
attemptNumber++;
}
}

// Check the user\'s guess against the correctCode
private void evaluateGuess() {
for (int i = 0; i < guesses[attemptNumber].length; i++) {
if (guesses[attemptNumber][i] == correctCode[i]) {
addResult(CORRECT_POSITION);
break;
} else {
for (int j = 0; j < guesses[attemptNumber].length; j++) {
if (guesses[attemptNumber][j] == correctCode[i]) {
addResult(CORRECT_COLOUR);
break;
}
}
}
}
}

// Add the result of the guess to the first available position
private void addResult(int resultToAdd) {
for (int i = 0; i < evaluations[1].length; i++) {
if (evaluations[attemptNumber][i] == 0) {
evaluations[attemptNumber][i] = resultToAdd;
break;
}
}
}


// Check to see if the code is solved
public boolean isCodeSolved() {
boolean codeCorrect = true;

for (int i = 0; i < correctCode.length; i++) {
if (correctCode[i] != guesses[attemptNumber][i]) {
codeCorrect = false;
}
}

return codeCorrect;
}

// Check to see if max attempts have been reached
public boolean isGameOver() {
return (attemptNumber == numberOfGuesses);
}


// Return the guess grid
public int[][] getGuesses(){
return guesses;
}

// Return the evaluations
public int[][] getEvaluations(){
return evaluations;
}

//Return correct code
public int[] getCorrectCode(){
return correctCode;
}

// Return the attempt number
public int getAttemptNumber(){
return attemptNumber;
}


}

This is the third part of the program. I HAVE TO WRITE ALL THESE CLASSES!!!! MasterMindAIConsistent Use the following algorithm to create a decent AI that will
This is the third part of the program. I HAVE TO WRITE ALL THESE CLASSES!!!! MasterMindAIConsistent Use the following algorithm to create a decent AI that will
This is the third part of the program. I HAVE TO WRITE ALL THESE CLASSES!!!! MasterMindAIConsistent Use the following algorithm to create a decent AI that will
This is the third part of the program. I HAVE TO WRITE ALL THESE CLASSES!!!! MasterMindAIConsistent Use the following algorithm to create a decent AI that will
This is the third part of the program. I HAVE TO WRITE ALL THESE CLASSES!!!! MasterMindAIConsistent Use the following algorithm to create a decent AI that will

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site