Codemaker class Update method signature checkAttemptedCode () to match the modif
ID: 3589393 • Letter: C
Question
Codemaker class
Update method signature checkAttemptedCode () to match the modified method signature in interface ICodemaker
Create local variables to store the number of red and white pegs scored by the codebreaker
Create a local variable to store which pegs have been evaluated of the codebreaker’s guess
Create a local variable to convert the secret code Set member variable to an ArrayList object
If true, then red pegs should equal 4
If true, then white pegs should equal 0
If true, increment the red peg counter
Add the codebreaker’s guess at this index to the Set that stores the evaluated pegs
If true, then increment the white pegs counter
Add the current index of the codebreaker’s attempt to the ArrayList that stores the evaluated pegs
For each red peg, add to the member variable codemakerResponse Color.RED
For each white peg, add to the member variable codemakerResponse Color.WHITE
Red is always first, then white!
Use an enhanced for loop to output to the console the codemaerker’s response
my code so far
package constants;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
/**
*
*/
public class Constants
{
// peg color options
public static final ArrayList<Color> codeColors = new ArrayList<Color>(Arrays.asList(Color.BLUE,
Color.BLACK, Color.ORANGE, Color.WHITE,
Color.YELLOW, Color.RED, Color.GREEN, Color.PINK));
// response color options
public static final ArrayList<Color> responseColors = new ArrayList<Color>(Arrays.asList(Color.RED, Color.WHITE));
public static final int MAX_ATTEMPTS = 10;
public static final int MAX_PEGS = 4;
public static final int COLORS = 8;
}
=======================
package core;
import java.awt.Color;
import static java.lang.System.console;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/**
*
*/
public class Codebreaker implements ICodebreaker
{
// member variables
public ArrayList<Color> codebreakerAttempt;
public Codebreaker()
{
// instanatiate the member variables
codebreakerAttempt = new ArrayList();
codebreakerAttempt.removeAll(new ArrayList());
consoleAttempt();
}
public void consoleAttempt() {
List<String> colors = Arrays.asList("black", "blue", "yellow","orange", "white", "green","red", "pink");
Scanner scan=new Scanner(System.in);
List<String> attempts= new ArrayList<>();
int maxPegs=10,console=0; //defined in constants file
System.out.println(" Enter your colors in left to right order " + "Use BLUE, BLACK, ORANGE, WHITE, YELLOW, RED, GREEN, PINK");
while(console<=maxPegs)//while no of attempts less than max tries
{
String str=scan.next();//taking color input
if(colors.contains(str) && !attempts.contains(str))//if its a valid color and if already not entered
{
attempts.add(str);//add it to the guessed ones
for(String y:attempts)//enhanced for loop printing attempts
{
System.out.println(" you entered: " +y);
System.out.println("next color");
}
}
console++;//increase no of attempts
if(attempts.size()>=4)//if 4 colors entered, break
break;
}
}
/**
* @return the codebreakerAttempt
*/
public ArrayList<Color> getCodebreakerAttempt()
{
return codebreakerAttempt;
}
/**
* @param codebreakerAttempt the codebreakerAttempt to set
*/
public void setCodebreakerAttempt(ArrayList<Color> codebreakerAttempt)
{
this.codebreakerAttempt = codebreakerAttempt;
}
/**
*
* @param attempt
*/
@Override
public void checkCode(ArrayList<Color> attempt)
{
}
}
===================
package core;
import constants.Constants;
import java.awt.Color;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
/**
*
*/
public class Codemaker implements ICodemaker
{
// member variables
private Set<Color> secretCode;
private ArrayList<Color> codemakerResponse;
public Codemaker()
{
// instantiate the member variable objects
secretCode = new HashSet();
codemakerResponse = new ArrayList();
// call the method to generate the secret code
generateSecretCode();
}
public void generateSecretCode()
{
Random random = new Random();
//randomly select four of the eight colors to be the secret code, only
// use each color once
while(secretCode.size() < Constants.MAX_PEGS)
{
// randomly select an index into the ArrayList of the 8 Colors
int index = random.nextInt(Constants.COLORS);
// get that color from the ArrayList of colors
Color selectedColor = Constants.codeColors.get(index);
// add it to the Set
secretCode.add(selectedColor);
}
System.out.println("generated the secret code! ");
for(Color color : secretCode)
{
System.out.println(color.toString());
}
}
public ArrayList<Color> checkAttemptedCode()
{
/**
* @return the secretCode
*/
public Set<Color> getSecretCode()
{
return secretCode;
}
/**
* @param secretCode the secretCode to set
*/
public void setSecretCode(Set<Color> secretCode)
{
this.secretCode = secretCode;
}
/**
* @return the codemakerResponse
*/
public ArrayList<Color> getCodemakerResponse()
{
return codemakerResponse;
}
/**
* @param codemakerResponse the codemakerResponse to set
*/
public void setCodemakerResponse(ArrayList<Color> codemakerResponse)
{
this.codemakerResponse = codemakerResponse;
}
}
=====
package core;
/**
*
*/
public class Game implements IGame
{
private int attempt;
private Codebreaker codebreaker;
private Codemaker codemaker;
public Game()
{
// instantiate the instances of the member variables
codemaker = new Codemaker();
codebreaker = new Codebreaker();
attempt = 0;
}
public void play()
{
}
/**
* @return the attempt
*/
public int getAttempt() {
return attempt;
}
/**
* @param attempt the attempt to set
*/
public void setAttempt(int attempt) {
this.attempt = attempt;
}
/**
* @return the codebreaker
*/
public Codebreaker getCodebreaker() {
return codebreaker;
}
/**
* @param codebreaker the codebreaker to set
*/
public void setCodebreaker(Codebreaker codebreaker) {
this.codebreaker = codebreaker;
}
/**
* @return the codemaker
*/
public Codemaker getCodemaker() {
return codemaker;
}
/**
* @param codemaker the codemaker to set
*/
public void setCodemaker(Codemaker codemaker) {
this.codemaker = codemaker;
}
}
====
package core;
import java.awt.Color;
import java.util.ArrayList;
/**
*
*/
public interface ICodebreaker
{
public void checkCode(ArrayList<Color> attempt);
}
=======
package core;
import java.awt.Color;
import java.util.ArrayList;
/**
*
*/
public interface ICodemaker
{
public void generateSecretCode();
public ArrayList<Color> checkAttemptedCode();
}
====
package core;
/**
*
*/
public interface IGame
{
public void play();
}
===
package mastermind;
import core.Game;
import javax.swing.JOptionPane;
import userinterface.MasterMindUi;
/**
*
*/
public class MasterMind
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
System.out.println("Welcome to MasterMind!");
JOptionPane.showMessageDialog(null, "Let's Play MasterMind!");
Game game = new Game();
//MasterMindUi ui = new MasterMindUi(game);
}
}
Codemaker class
Update method signature checkAttemptedCode () to match the modified method signature in interface ICodemaker
Implement method checkAttemptedCode() to do the following:Create local variables to store the number of red and white pegs scored by the codebreaker
Create a local variable to store which pegs have been evaluated of the codebreaker’s guess
Create a local variable to convert the secret code Set member variable to an ArrayList object
Check if the codebreaker’s guess is exactly equal to the codemaker’s attempt (Hint: class ArrayList has a handy method .equals())If true, then red pegs should equal 4
If true, then white pegs should equal 0
If item d. fails, then determine which pegs are the correct color in the correct position or the correct color in the wrong positionLoop through the max pegs as defined in our Constants.java classCheck if a correct color is in the correct position by comparing index to index of the two ArrayLists representing the secret code and the codebreaker’s guessIf true, increment the red peg counter
Add the codebreaker’s guess at this index to the Set that stores the evaluated pegs
Use an enhanced for loop using the codebreaker’s attempt as the collection objectCheck if the secret code ArrayList contains the current element of the codebreaker’s attempt (Hint: class ArrayList has a handy method .contains())Loop through the max pegs as defined in our Constants.java classCheck if the secret code ArrayList is NOT equal to the codebreaker’s attempt at the specific index AND the secret code ArrayList contains the codebreaker’s attempt at the specific index AND the pegs evaluated Set does NOT include the current codebreaker’s attempt at the specific indexIf true, then increment the white pegs counter
Add the current index of the codebreaker’s attempt to the ArrayList that stores the evaluated pegs
Evaluate the red and white peg counters to populate the codemaker’s responseFor each red peg, add to the member variable codemakerResponse Color.RED
For each white peg, add to the member variable codemakerResponse Color.WHITE
Red is always first, then white!
Use an enhanced for loop to output to the console the codemaerker’s response
Explanation / Answer
package constants;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
/**
*
*/
public class Constants
{
// peg color options
public static final ArrayList<Color> codeColors = new ArrayList<Color>(Arrays.asList(Color.BLUE,
Color.BLACK, Color.ORANGE, Color.WHITE,
Color.YELLOW, Color.RED, Color.GREEN, Color.PINK));
// response color options
public static final ArrayList<Color> responseColors = new ArrayList<Color>(Arrays.asList(Color.RED, Color.WHITE));
public static final int MAX_ATTEMPTS = 10;
public static final int MAX_PEGS = 4;
public static final int COLORS = 8;
}
=======================
package core;
import java.awt.Color;
import static java.lang.System.console;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/**
*
*/
public class Codebreaker implements ICodebreaker
{
// member variables
public ArrayList<Color> codebreakerAttempt;
public Codebreaker()
{
// instanatiate the member variables
codebreakerAttempt = new ArrayList();
codebreakerAttempt.removeAll(new ArrayList());
consoleAttempt();
}
public void consoleAttempt() {
List<String> colors = Arrays.asList("black", "blue", "yellow","orange", "white", "green","red", "pink");
Scanner scan=new Scanner(System.in);
List<String> attempts= new ArrayList<>();
int maxPegs=10,console=0; //defined in constants file
System.out.println(" Enter your colors in left to right order " + "Use BLUE, BLACK, ORANGE, WHITE, YELLOW, RED, GREEN, PINK");
while(console<=maxPegs)//while no of attempts less than max tries
{
String str=scan.next();//taking color input
if(colors.contains(str) && !attempts.contains(str))//if its a valid color and if already not entered
{
attempts.add(str);//add it to the guessed ones
for(String y:attempts)//enhanced for loop printing attempts
{
System.out.println(" you entered: " +y);
System.out.println("next color");
}
}
console++;//increase no of attempts
if(attempts.size()>=4)//if 4 colors entered, break
break;
}
}
/**
* @return the codebreakerAttempt
*/
public ArrayList<Color> getCodebreakerAttempt()
{
return codebreakerAttempt;
}
/**
* @param codebreakerAttempt the codebreakerAttempt to set
*/
public void setCodebreakerAttempt(ArrayList<Color> codebreakerAttempt)
{
this.codebreakerAttempt = codebreakerAttempt;
}
/**
*
* @param attempt
*/
@Override
public void checkCode(ArrayList<Color> attempt)
{
}
}
===================
package core;
import constants.Constants;
import java.awt.Color;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
/**
*
*/
public class Codemaker implements ICodemaker
{
// member variables
private Set<Color> secretCode;
private ArrayList<Color> codemakerResponse;
public Codemaker()
{
// instantiate the member variable objects
secretCode = new HashSet();
codemakerResponse = new ArrayList();
// call the method to generate the secret code
generateSecretCode();
}
public void generateSecretCode()
{
Random random = new Random();
//randomly select four of the eight colors to be the secret code, only
// use each color once
while(secretCode.size() < Constants.MAX_PEGS)
{
// randomly select an index into the ArrayList of the 8 Colors
int index = random.nextInt(Constants.COLORS);
// get that color from the ArrayList of colors
Color selectedColor = Constants.codeColors.get(index);
// add it to the Set
secretCode.add(selectedColor);
}
System.out.println("generated the secret code! ");
for(Color color : secretCode)
{
System.out.println(color.toString());
}
}
public ArrayList<Color> checkAttemptedCode()
{
/**
* @return the secretCode
*/
public Set<Color> getSecretCode()
{
return secretCode;
}
/**
* @param secretCode the secretCode to set
*/
public void setSecretCode(Set<Color> secretCode)
{
this.secretCode = secretCode;
}
/**
* @return the codemakerResponse
*/
public ArrayList<Color> getCodemakerResponse()
{
return codemakerResponse;
}
/**
* @param codemakerResponse the codemakerResponse to set
*/
public void setCodemakerResponse(ArrayList<Color> codemakerResponse)
{
this.codemakerResponse = codemakerResponse;
}
}
=====
package core;
/**
*
*/
public class Game implements IGame
{
private int attempt;
private Codebreaker codebreaker;
private Codemaker codemaker;
public Game()
{
// instantiate the instances of the member variables
codemaker = new Codemaker();
codebreaker = new Codebreaker();
attempt = 0;
}
public void play()
{
}
/**
* @return the attempt
*/
public int getAttempt() {
return attempt;
}
/**
* @param attempt the attempt to set
*/
public void setAttempt(int attempt) {
this.attempt = attempt;
}
/**
* @return the codebreaker
*/
public Codebreaker getCodebreaker() {
return codebreaker;
}
/**
* @param codebreaker the codebreaker to set
*/
public void setCodebreaker(Codebreaker codebreaker) {
this.codebreaker = codebreaker;
}
/**
* @return the codemaker
*/
public Codemaker getCodemaker() {
return codemaker;
}
/**
* @param codemaker the codemaker to set
*/
public void setCodemaker(Codemaker codemaker) {
this.codemaker = codemaker;
}
}
====
package core;
import java.awt.Color;
import java.util.ArrayList;
/**
*
*/
public interface ICodebreaker
{
public void checkCode(ArrayList<Color> attempt);
}
=======
package core;
import java.awt.Color;
import java.util.ArrayList;
/**
*
*/
public interface ICodemaker
{
public void generateSecretCode();
public ArrayList<Color> checkAttemptedCode();
}
====
package core;
/**
*
*/
public interface IGame
{
public void play();
}
===
package mastermind;
import core.Game;
import javax.swing.JOptionPane;
import userinterface.MasterMindUi;
/**
*
*/
public class MasterMind
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
System.out.println("Welcome to MasterMind!");
JOptionPane.showMessageDialog(null, "Let's Play MasterMind!");
Game game = new Game();
//MasterMindUi ui = new MasterMindUi(game);
}
}