Question
Java Assignment (Code listed below is code before doing the following task. Should only have to follow steps in Tasks and Rubric below.) The goal
Java Assignment (Code listed below is code before doing the following task. Should only have to follow steps in "Tasks and Rubric" below.)
The goal is to simulate the foundation of the components of the game MasterMind by creating a Codemaker, Codebreaker, and the Game; this includes:
1. Allow the code breaker to attempt guessing the secret code.
2. Have the code maker respond to the code breakers attempt.
a. If the correct color is in the correct position, the code makers response should include a red peg
b. If the correct color is in the wrong position, the code makers response should include a white peg
c. If the color is not correct no pegs are used
d. Red pegs are presented first, then the white pegs
3. Limit the number of attempts to 10 guesses, if the code breaker guesses the code, they win the game, if the code breaker does not guess the code within 10 guesses, they lose the game.
Tasks and Rubric
Activity | |
MasterMind project | |
MasterMind class | |
Constants package | |
Constants class | |
core package | |
Codebreaker class | 1. Add private method consoleAttempt() that will allow for backend testing of the logic, it shall do the following: a. Remove previous data stored in member variable codebreakerAttempt (Hint: class ArrayList has a handy method . removeAll()) b. Instantiate an instance of class Scanner to take input from the console c. Prompt the user to enter their guess, my implementation was as follows:i. System.out.println(" Enter your colors in left to right order " + ii. "Use BLUE, BLACK, ORANGE, WHITE, YELLOW, RED, GREEN, PINK:"); d. Loop until the user enters four valid colors with no duplicates i. Instantiate an instance of class String set equal to Scanners method next() 1. Evaluate the users input (Hint: recommend using either String method .toUpperCase() or .toLowerCase() to alleviate issues with case sensitivity) A. If the entered color matches one of the eight valid colors of the game, do the following: I. Output to the console the selected color II. Add the color to the member variable codebreakerAttempt B. If the size of the codebreakerAttempt is less than the max pegs specified in our Constants.java file then add a prompt to enter for the user their next color e. Using an enhanced for loop output to the console the codebreakers attempt 2. Update method getCodebreakerAttempt() to call method consoleAttempt() |
Codemaker class | 1. Update method signature checkAttemptedCode () to match the modified method signature in interface ICodemaker 2. Implement method checkAttemptedCode() to do the following:a. Create local variables to store the number of red and white pegs scored by the codebreaker b. Create a local variable to store which pegs have been evaluated of the codebreakers guess c. Create a local variable to convert the secret code Set member variable to an ArrayList object d. Check if the codebreakers guess is exactly equal to the codemakers attempt (Hint: class ArrayList has a handy method .equals())i. If true, then red pegs should equal 4 ii. If true, then white pegs should equal 0 e. If item d. fails, then determine which pegs are the correct color in the correct position or the correct color in the wrong position i. Loop through the max pegs as defined in our Constants.java class 1. Check if a correct color is in the correct position by comparing index to index of the two ArrayLists representing the secret code and the codebreakers guess A. If true, increment the red peg counter B. Add the codebreakers guess at this index to the Set that stores the evaluated pegs 2. Use an enhanced for loop using the codebreakers attempt as the collection object A. Check if the secret code ArrayList contains the current element of the codebreakers attempt (Hint: class ArrayList has a handy method .contains()) i. Loop through the max pegs as defined in our Constants.java class 1. Check if the secret code ArrayList is NOT equal to the codebreakers attempt at the specific index AND the secret code ArrayList contains the codebreakers attempt at the specific index AND the pegs evaluated Set does NOT include the current codebreakers attempt at the specific index A. If true, then increment the white pegs counter B. Add the current index of the codebreakers attempt to the ArrayList that stores the evaluated pegs f. Evaluate the red and white peg counters to populate the codemakers responsei. For each red peg, add to the member variable codemakerResponse Color.RED ii. For each white peg, add to the member variable codemakerResponse Color.WHITE iii. Red is always first, then white! iv. Use an enhanced for loop to output to the console the codemaerkers response |
Game class | 1. Modify custom constructor a. Call method play() 2. Method play() a. Loop for the max number of attempts as defined in our Constants.java class AND the codebreaker hasnt guessed the secrete code i. Instantiate an instance of class ArrayList specified as using class Color, set equal to calling method getCodebreakerAttempt() in class Codebreaker ii. Call method checkAttemptedCode() in class Codemaker, passing the local variable from step a. as an argument iii. Instantiate an instance of class ArrayList specified as using class Color, set equal to calling method getCodemakerResponse () in class Codemaker iv. Use an enhanced for loop to output to the console the codemakers response for each codebreakers attempt |
ICodebreaker interface | |
ICodemaker interface | 1. Modify method signature public void checkAttemptedCode() a. Add parameter ArrayList attempt to the method signature |
IGame interface | |
userInterface package | |
MasterMindUi class | |
MasterMind application | |
Test Case 1 | Test Case 1 passes regression testing |
Source compiles with no errors | |
Source runs with no errors | |
Source includes comments |
======================================================================================
OUTPUT
======================================================================================
Figure 1 JOptionPane.showMessageDialog()
Figure 2 Generate Secret Code
Figure 3 Prompt the User for Four Colors
Figure 4 Prompting user for color selections
Figure 5 Codemaker's Response
=========================================================================
Current Code
=========================================================================
Constants,java
================
package constants;
import java.awt.Color; import java.util.ArrayList; import java.util.Arrays;
public class Constants { // peg color options public static final ArrayList codeColors = new ArrayList(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 responseColors = new ArrayList(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; }
================================================================================
Codebreaker.java
==================
package core;
import java.awt.Color; import java.util.ArrayList; import constants.Constants;
public class Codebreaker implements ICodebreaker { // member variables private ArrayList codebreakerAttempt; public Codebreaker() { // instanatiate the member variables codebreakerAttempt = new ArrayList(); } /** * @return the codebreakerAttempt */ public ArrayList getCodebreakerAttempt() { return codebreakerAttempt; }
/** * @param codebreakerAttempt the codebreakerAttempt to set */ public void setCodebreakerAttempt(ArrayList codebreakerAttempt) { this.codebreakerAttempt = codebreakerAttempt; } public void checkCode(ArrayList attempt) { }
}
========================================================================
Codemaker.java
===================== 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 secretCode; private ArrayList 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()
/** * @return the secretCode */ public Set getSecretCode() { return secretCode; }
/** * @param secretCode the secretCode to set */ public void setSecretCode(Set secretCode) { this.secretCode = secretCode; }
/** * @return the codemakerResponse */ public ArrayList getCodemakerResponse() { return codemakerResponse; }
/** * @param codemakerResponse the codemakerResponse to set */ public void setCodemakerResponse(ArrayList codemakerResponse) { this.codemakerResponse = codemakerResponse; } }
===============================================================
Game.java
=============
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; } }
=========================================================================
ICodebreaker.java
=================
package core;
import java.awt.Color; import java.util.ArrayList;
public interface ICodebreaker { public void checkCode(ArrayList attempt); }
======================================================================
ICodemaker.java
======================
package core;
public interface ICodemaker { public void generateSecretCode(); public void checkAttemptedCode(); }
=========================================================================
IGame.java
=====================
package core;
public interface IGame { public void play(); }
=====================================================================
MasterMind.java
======================
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); } }
==================================================================
MasterMindUi.java
=====================
package userinterface;
import core.Game; import constants.Constants;
public class MasterMindUi { private Game game; public MasterMindUi(Game game) { this.game = game; initComponents(); } private void initComponents() { } }
===============================================================
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started