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

Object Oriented Problem: MasterMind Game Assignment Scope For this assignment st

ID: 3593970 • Letter: O

Question

Object Oriented Problem: MasterMind Game

Assignment Scope

For this assignment students will begin developing the front end, or User Interface, portion of the MasterMind game. Based on my professional experience working in the industry I have always had to develop a UI for every application, therefore I translate that experience to students so they can have the same opportunity and be prepared professionally.

Typically, there is a one-to-one correlation of back end functionality to front end UI component. Depending upon the design of the application it doesn’t always correlate perfectly, however with MasterMind, it works well.

Back-end functionality

Front-end UI component

Codemaker.java

CodemakerUi.java

Codebreaker.java

CodebreakerUi.java

Game.java

MasterMindUi.java

The goal is to develop the front-end components of the game MasterMind by creating classes:

CodebreakerUi.java

CodemakerUi.java

MasterMindUi.java

Students will also begin to learn writing simple ActionListeners or Event Handlers.

The UI will be developed in multiple assignments, it is not expected that for Assignment 4 the fully functioning UI is complete.

The image that follows is a prototype of what the UI will look like. It does not have to be an exact match. The rubric will provide guidance and recommendations on how to accomplish this, however feel free to be creative in developing the look and feel of the UI.

To accomplish this:

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.

I have provided multiple code examples on Webcourses that shows how to implement numerous of the tasks below, please reference those code examples prior to asking me for help!   

Tasks and Rubric

Activity

mastermind package

Mastermind.java

Instantiate an instance of class MastermindUi, passing the reference object of class Game as an argument

core package

Game.java

In the customer constructor, comment out the call the method play()

userInterface package

MastermindUi.java

Game game;

CodebreakerUi codebreakerUi;

CodemakerUi codemakerUi;

JFrame frame;

JMenuBar menuBar;

JMenu gameMenu;

JMenu helpMenu;

JMenuItem newGameMenuItem;

JMenuItem exitMenuItem;

JMenuItem aboutMenuItem;

JMenuItem rulesMenuItem;

Set member variable of type class Game to the parameter passed in

Instantiate the member variable of class CodebreakerUi passing as an argument to the constructor the instance of class Codebreaker in class Game (hint: use the getter!)

Instantiate the member variable of class CodemakerUi passing as an argument to the constructor the instance of class Codemaker in class Game (hint: use the getter!)

call method initComponents()

Set the default size of the JFrame

Set the default close operation of the JFrame

Use default layout manager BorderLayout

JMenu Game should be added to the JMenuBar

JMenuItems New Game and Exit should be added to the JMenu Game

JMenu Help should be added to the JMenuBar

JMenuItems About and Game Rules should be added to the JMenu Help

JMenuBar should be set on the JFrame

Add the CodemakerUi JPanels to the JFrame using the getters defined in the class

Add the CodebreakerUi JPanels to the JFrame using the getters defined in the class

Set the visibility of the JFrame (hint: this should ALWAYS be the last step on a UI)

Write an inner class to create an ActionListener that is registered to the JMenuItem with the text Exit; it should

Display a JOptionPane message confirming the user wants to exit using method showConfirmDialog()

If yes, exit the application by calling method System.exit() passing the value of 0 as an argument

If no, do not exit the application

Write an inner class to create an ActionListener that is registered to the JMenuItem with the text About using method showMessageDialog(); it should

Application name and version

Author

Date of development

Write an inner class to create an ActionListener that is registered to the JMenuItem with the text Game Rules using method showMessageDialog(); it should

Rules of the game as shown in Figure 6 below

CodebreakerUi.java

JPanel codebreakerAttempt;

JPanel codebreakerColors;

Codebreaker codebreaker;

Create getters for the two JPanel member variables

Set member variable of type class Codebreaker to the parameter passed in

call method initComponents()

setMinimumSize()

setPreferredSize()

setBorder(BorderFactory.createTitledBorder("Codebreaker Attempt"));

This can be temporary, it just shows the layout while there aren’t any buttons or other components in the JPanels

CodemakerUi.java

JPanel codemakerResponse;

JPanel secretCode;

Codebreaker codemaker;

Create getters for the two JPanel member variables

Set member variable of type class Codemaker to the parameter passed in

call method initComponents()

setMinimumSize()

setPreferredSize()

setBorder(BorderFactory.createTitledBorder("Codemaker Response"));

This can be temporary, it just shows the layout while there aren’t any buttons or other components in the JPanels

Mastermind application

Test Case 1

Test Case 1 passes

Test Case 2

Test Case 2 passes

Test Case 3

Test Case 3 passes

Source compiles with no errors

Source runs with no errors

Source includes comments

Total

Perform the following test cases

Test Cases

Action

Expected outcome

Test Case 1

Rregression Testing: Initial JOptionPane displays

JOptionPane is similar to figure 1

Test Case 2

Mastermind Initial UI displays

Mastermind UI looks similar figure 2

Test Case 3

Mastermind Game Menu

Game menu looks similar to figure 3

Test Case 4

Mastermind Help Menu

Help menu looks similar to figure 4

Test Case 5

About Menu Item Action Listener

JOptionPane displays similar to figure 5

Test Case 6

Rules Menu Item Action Listener

JOptionPane displays similar to figure 6

Test Case 7

Exit Menu Item Action Listener

JOptionPane displays similar to figure 7;

if user selects yes, the application should exit;

if user selects no, the application continues to run

Test Case 8

Project view

Project view matches figure

Figure 1 Test Case 1

Figure 2 Mastermind Initial UI

Figure 3 Game Menu

Figure 4 Help Menu

Figure 5 About Menu Item

Figure 6 Rules Menu Item

Figure 7 Exit Menu Item

Figure 8 Project View

Current Code

============================================================================

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()
{
  
}
}

============================================================================

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;
import java.util.Scanner;


public class Codebreaker implements ICodebreaker
{

// member variables
private ArrayList codebreakerAttempt;
//private Game game;
  
public Codebreaker()
{
// instanatiate the member variables
codebreakerAttempt = new ArrayList<>();
}
  
private void consoleAttempt()
{
// reset the codebreakerAttempt
codebreakerAttempt.removeAll(codebreakerAttempt);
  
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:");
  
while(codebreakerAttempt.size() < Constants.MAX_PEGS)
{
String guess = scanner.next();

switch(guess.toUpperCase())
{
case "BLUE":
System.out.println("You enteted blue");
codebreakerAttempt.add(Color.BLUE);
break;
case "BLACK":
System.out.println("You enteted black");
codebreakerAttempt.add(Color.BLACK);
break;
case "GREEN":
System.out.println("You enteted green");
codebreakerAttempt.add(Color.GREEN);
break;
case "ORANGE":
System.out.println("You enteted orange");
codebreakerAttempt.add(Color.ORANGE);
break;
case "PINK":
System.out.println("You enteted pink");
codebreakerAttempt.add(Color.PINK);
break;
case "RED":
System.out.println("You enteted red");
codebreakerAttempt.add(Color.RED);
break;
case "YELLOW":
System.out.println("You enteted yellow");
codebreakerAttempt.add(Color.YELLOW);
break;
case "WHITE":
System.out.println("You enteted white");
codebreakerAttempt.add(Color.WHITE);
break;
default:
System.out.println("Invalid color choice, try again");
break;
}
  
if(codebreakerAttempt.size() < Constants.MAX_PEGS)
System.out.println("Next color");
}
  
System.out.println("Codebreaker's attempt");
  
for(Color color : codebreakerAttempt)
{
System.out.println(color);
}
}
/**
* @return the codebreakerAttempt
*/
public ArrayList getCodebreakerAttempt()
{
// temporary code to test backend
consoleAttempt();
return codebreakerAttempt;
}

/**
* @param codebreakerAttempt the codebreakerAttempt to set
*/
public void setCodebreakerAttempt(ArrayList codebreakerAttempt)
{
this.codebreakerAttempt = codebreakerAttempt;
}
  
@Override
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.List;
import java.util.Random;
import java.util.Set;


public class Codemaker implements ICodemaker
{
// member variables
private Set secretCode;
private ArrayList codemakerResponse;
private boolean codeGuessed;
  
public Codemaker()
{
// instantiate the member variable
codeGuessed = false;
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 void checkAttemptedCode(ArrayList attempt)
{
// correct color in correct position is a red peg
int redPegs = 0;
// correct color in the wrong position is a white peg
int whitePegs = 0;
// incorrect color has no peg
// keep track of which pegs we already counted
Set countedPegs = new HashSet();
  
System.out.println("Codemaker is checking codebreaker attempt");
  
// convert the Set to an ArrayList
List secretList = new ArrayList(secretCode);
  
// is it an exact match?   
if(secretList.equals(attempt))
{
redPegs += 4;
whitePegs = 0;
System.out.println("You guessed it!");
}
// not an exact match, is it even in the secret code
else
{
// check for correct color and correct position
for(int peg = 0; peg < Constants.MAX_PEGS; peg++)
{
if(secretList.get(peg) == attempt.get(peg))
{
System.out.println("Found correct color in correct position!");
// correct peg in correct location, add a red peg
redPegs++;
countedPegs.add(attempt.get(peg));
}
}
  
// check for any match
for(Color color : attempt)
{
// check if any of the colors are correct
if(secretList.contains(color))
{
for(int peg = 0; peg < Constants.MAX_PEGS; peg++)
{

if((secretList.get(peg) != attempt.get(peg)) &&
(secretList.contains(attempt.get(peg))) &&
!countedPegs.contains(attempt.get(peg)))
{
System.out.println("Found correct color in wrong position");
// add a white peg
whitePegs++;
countedPegs.add(attempt.get(peg));
}
}
}
}
}
  
evaluatePegs(redPegs, whitePegs);
}

private void evaluatePegs(int red, int white)
{
// clear the codemakerResponse
codemakerResponse.removeAll(codemakerResponse);
  
System.out.println("Red pegs " + red + " white pegs " + white);
  
if(red == Constants.MAX_PEGS)
{
codeGuessed = true;
}
  
for(int r = 0; r < red; r++)
{
codemakerResponse.add(Color.RED);
}
  
for(int w = 0; w < white; w++)
{
codemakerResponse.add(Color.WHITE);
}
}
  
/**
* @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;
}

/**
* @return the codeGuessed
*/
public boolean isCodeGuessed() {
return codeGuessed;
}

/**
* @param codeGuessed the codeGuessed to set
*/
public void setCodeGuessed(boolean codeGuessed) {
this.codeGuessed = codeGuessed;
}
  
  
}

============================================================================

Game.java

package core;

import java.awt.Color;
import java.lang.System;
import java.util.ArrayList;
import java.util.Scanner;
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()
{
do{
attempt++;
System.out.println("**** Attempt " + attempt + " ****");
ArrayList guess = codebreaker.getCodebreakerAttempt();
codemaker.checkAttemptedCode(guess);
ArrayList result = codemaker.getCodemakerResponse();
  
System.out.println("Codemaker's response ");
  
for(Color color : result)
{
System.out.println(color);
}
}while(attempt <= Constants.MAX_ATTEMPTS && !codemaker.isCodeGuessed());
}

/**
* @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;

import java.awt.Color;
import java.util.ArrayList;


public interface ICodemaker
{
  
public void generateSecretCode();
public void checkAttemptedCode(ArrayList attempt);
}

============================================================================

IGame.java

package core;


public interface IGame
{
public void play();
}

Back-end functionality

Front-end UI component

Codemaker.java

CodemakerUi.java

Codebreaker.java

CodebreakerUi.java

Game.java

MasterMindUi.java

Check

Explanation / Answer

#include #include #include #include #include #include using namespace std; int main(){ int choose; int choose2; int c1, c2, c3, c4; int g1, g2, g3, g4; //int D; //bool menu = true; MenuOne: cout