Assignment Scope For this assignment students will experience changing requireme
ID: 3589566 • Letter: A
Question
Assignment Scope
For this assignment students will experience changing requirements resulting in modifying the original design. This is real world experience and is intentionally incorporated into projects every semester to expose students to the need to be flexible in software engineering. After initial project design of a project, when software engineers delve into writing the source code, it frequently exposes gaps in previous assumptions.
Additionally, software engineers will write some test code for the backend development that eventually will not be used in the final project or also known as thrown away. We do this to make sure our backend logic works before we add a user interface. We will also incorporate this methodology for this assignment of the project.
Finally, software engineers always perform regression testing after modifying source code. Regression testing is a type of software testing which verifies that software which was previously developed and tested still performs the same way after it was changed or interfaced with other software. Changes may include software enhancements, patches, configuration changes, etc.
The goal is to simulate the foundation of the components of the game MasterMind by creating a Codemaker, Codebreaker, and the Game; this includes:
Allow the code breaker to attempt guessing the secret code.
Have the code maker respond to the code breaker’s attempt.
If the correct color is in the correct position, the code maker’s response should include a red peg
If the correct color is in the wrong position, the code maker’s response should include a white peg
If the color is not correct no pegs are used
Red pegs are presented first, then the white pegs
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.
To accomplish this, write the follow source code:
Reference the tasks below for the specifics of the source code requirements.
Compress a project and submit to Webcourses
Decompress compressed project and verify it is a Netbeans project
References
Netbeans.docx
Setting up a project in Netbeans.docx
Netbeans right click menu help.docx
Deliverables
To complete this assignment you must submit your compressed Netbeans project to Webcourses.
Please keep in mind that the tasks are guidance to accomplish the goals of the assignment. At times students will be required to do additional research (e.g. Google That S**T (GTS)!) to find implementation options. In the industry, software engineers are expected to be very self-sufficient to find the best solution for the task at hand.
Tasks and Rubric
Activity
MasterMind project
MasterMind class
Constants package
Constants class
core package
Codebreaker class
Remove previous data stored in member variable codebreakerAttempt (Hint: class ArrayList has a handy method . removeAll())
Instantiate an instance of class Scanner to take input from the console
System.out.println(" Enter your colors in left to right order " +
"Use BLUE, BLACK, ORANGE, WHITE, YELLOW, RED, GREEN, PINK:");
Output to the console the selected color
Add the color to the member variable codebreakerAttempt
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
Using an enhanced for loop output to the console the codebreaker’s attempt
Update method getCodebreakerAttempt() to call method consoleAttempt()
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
Game class
Call method play()
Instantiate an instance of class ArrayList specified as using class Color, set equal to calling method getCodebreakerAttempt() in class Codebreaker
Call method checkAttemptedCode() in class Codemaker, passing the local variable from step a. as an argument
Instantiate an instance of class ArrayList specified as using class Color, set equal to calling method getCodemakerResponse () in class Codemaker
Use an enhanced for loop to output to the console the codemaker’s response for each codebreaker’s attempt
ICodebreaker interface
ICodemaker interface
Add parameter ArrayList<Color> 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
Perform the following test cases
Test Cases
Action
Expected outcome
Test Case 1
Run application – start the game
JOptionPane.showMessageDialog() displays similar to figure 1
Test Case 2
Run application – codemaker generates secret code
The console window should be similar to figure 2
Java does NOT automatically translate its Color objects to a String value, rather it prints the RGB code of the color.
Test Case 3
Run application – codebreaker prompted for their guess
The console window should be similar to figure 3
Test Case 4
Run application – codebreaker entries acknowledged
The console window should be similar to figure 4
Test Case 5
Run application – codemaker response
The console window should be similar to figure 5
Figure 1 JOptionPane.showMessageDialog()
Figure 2 Generated secret code
Figure 3 Prompt the user for four colors
Figure 4 Prompting user for color selections
Figure 5 Codemaker's Response
Activity
MasterMind project
MasterMind class
Constants package
Constants class
core package
Codebreaker class
Add private method consoleAttempt() that will allow for backend testing of the logic, it shall do the following:Remove previous data stored in member variable codebreakerAttempt (Hint: class ArrayList has a handy method . removeAll())
Instantiate an instance of class Scanner to take input from the console
Prompt the user to enter their guess, my implementation was as follows:System.out.println(" Enter your colors in left to right order " +
"Use BLUE, BLACK, ORANGE, WHITE, YELLOW, RED, GREEN, PINK:");
Loop until the user enters four valid colors with no duplicatesInstantiate an instance of class String set equal to Scanner’s method next()Evaluate the user’s input (Hint: recommend using either String method .toUpperCase() or .toLowerCase() to alleviate issues with case sensitivity)If the entered color matches one of the eight valid colors of the game, do the following:Output to the console the selected color
Add the color to the member variable codebreakerAttempt
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
Using an enhanced for loop output to the console the codebreaker’s attempt
Update method getCodebreakerAttempt() to call method consoleAttempt()
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
Game class
Modify custom constructorCall method play()
Method play()Loop for the max number of attempts as defined in our Constants.java class AND the codebreaker hasn’t guessed the secrete codeInstantiate an instance of class ArrayList specified as using class Color, set equal to calling method getCodebreakerAttempt() in class Codebreaker
Call method checkAttemptedCode() in class Codemaker, passing the local variable from step a. as an argument
Instantiate an instance of class ArrayList specified as using class Color, set equal to calling method getCodemakerResponse () in class Codemaker
Use an enhanced for loop to output to the console the codemaker’s response for each codebreaker’s attempt
ICodebreaker interface
ICodemaker interface
Modify method signature public void checkAttemptedCode()Add parameter ArrayList<Color> 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
Explanation / Answer
//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<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;
}
//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<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());
}
}
/**
* @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;
}
public void checkAttemptedCode(ArrayList<Color> attempt) {
codemakerResponse.clear(); //resetting the response
System.out.println("Codemaker checking attempted code");
ArrayList<Integer> evaluatedPos=new ArrayList<Integer>(); //List for storing indices of right guesses
ArrayList<Integer> whitePegs=new ArrayList<Integer>(); //A temporary list for storing indices of matching colors, with wrong positions
int red_pegs=0,white_pegs=0;
ArrayList<Color> secret=new ArrayList<Color>();
secret.addAll(getSecretCode());
if(secret.equals(attempt)){
red_pegs=4;
white_pegs=0;
for(int i=0;i<red_pegs;i++){
codemakerResponse.add(Color.RED);
}
}else{
for(int i=0;i<attempt.size();i++){
if(secret.get(i).equals(attempt.get(i))){ //colors and positions match
red_pegs++;
evaluatedPos.add(i);
}else {
if(secret.contains(attempt.get(i))){ // if any colors match, but positions don't
white_pegs++;
whitePegs.add(i);
}
}
}
for(int i=0;i<attempt.size();i++){
if(evaluatedPos.contains(i)){ /* if index i is evaluated as a right guess,
adding red color to that index of the response,
the logic might be slightly different, but this
will do the same job*/
codemakerResponse.add(i, Color.RED);
}else {
if(whitePegs.contains(i)){
codemakerResponse.add(i, Color.WHITE); //to indicate partially wrong(wrong positions)guesses
}else{
codemakerResponse.add(i,null); //no pegs added
}
}
}
}
}
}
//Codebreaker.java
package core;
import java.awt.Color;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Scanner;
import constants.Constants;
public class Codebreaker implements ICodebreaker
{
// member variables
private ArrayList<Color> codebreakerAttempt;
public Codebreaker()
{
// instanatiate the member variables
codebreakerAttempt = new ArrayList();
}
/**
* @return the codebreakerAttempt
*/
public ArrayList<Color> getCodebreakerAttempt()
{
consoleAttempt();
return codebreakerAttempt;
}
/**
* @param codebreakerAttempt the codebreakerAttempt to set
*/
public void setCodebreakerAttempt(ArrayList<Color> codebreakerAttempt)
{
this.codebreakerAttempt = codebreakerAttempt;
}
public boolean checkCode(ArrayList<Color> attempt)
{
boolean errorFlag=false;
for (Color color : attempt) {
if(color!=Color.RED){ //only if ALL the colors in the list are red, attempt is correct else wrong / partially correct
errorFlag=true;
}
}
if(errorFlag){
System.out.println("Try again...");
System.out.println("Right guesses in (positions)");
for (int i=0;i<attempt.size();i++){
if(attempt.get(i)==Color.RED){
System.out.print(i+" "); //right guess in 'i'th position
}
}
System.out.println();
System.out.println("Right colors,wrong positions in (positions)");
for (int i=0;i<attempt.size();i++){
if(attempt.get(i)==Color.WHITE){
System.out.print(i+" "); //right color ,wrong position in 'i'th position
}
}
return false;
}
else {
return true;
}
}
private void consoleAttempt(){
boolean errorFlag=false; //error checking flag
codebreakerAttempt.clear(); //faster than using removeAll
Scanner scanner=new Scanner(System.in);
System.out.println(" Enter your colors in left to right order " +
"Use BLUE, BLACK, ORANGE, WHITE, YELLOW, RED, GREEN, PINK:");
for(int i=0;i<Constants.MAX_PEGS;i++){
String colorString=scanner.next().toUpperCase();
Color color=stringToColor(colorString);
if(color != null && !codebreakerAttempt.contains(color)){
codebreakerAttempt.add(color);
}else {
System.out.println("wrong / duplicate colors");
errorFlag=true;
break;
}
}
if(!errorFlag){
System.out.println("Your input: ");
for (Color col : codebreakerAttempt) {
System.out.print(col+" ");
}
System.out.println();
} else{
consoleAttempt();
}
}
private Color stringToColor(String colorString){
Color color;
try{
Field field=Class.forName("java.awt.Color").getField(colorString);
color=(Color)field.get(null);
}catch (Exception e) {
color=null;
}
return color;
}
}
//Game.java
package core;
import java.awt.Color;
import java.util.ArrayList;
import constants.Constants;
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;
play();
}
public void play()
{
while(true){ // will loop again and again until codebreaker wins or runs out of attempts
if(attempt<Constants.MAX_ATTEMPTS){
ArrayList<Color> codeBreakerAttempt=codebreaker.getCodebreakerAttempt();
codemaker.checkAttemptedCode(codeBreakerAttempt);
boolean win=codebreaker.checkCode(codemaker.getCodemakerResponse()); /*checkCode function of ICodebreaker has been modified,will return true if codebreaker wins*/
if(win){
System.out.println("Codebreaker wins");
break; //breaking the loop
}
}else{
System.out.println("Codemaker wins"); //after max attempts and codebreaker couldn't guess correctly
break;
}
attempt++;
}
}
/**
* @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;
}
public void checkIfWon(){
}
}
//ICodebreaker.java
package core;
import java.awt.Color;
import java.util.ArrayList;
public interface ICodebreaker
{
public boolean checkCode(ArrayList<Color> attempt);
}
//ICodemaker.java
package core;
import java.awt.Color;
import java.util.ArrayList;
public interface ICodemaker
{
public void generateSecretCode();
public void checkAttemptedCode(ArrayList<Color> attempt);
}
//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
{
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()
{
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.