Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 doesnt 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

Add to method main()

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

Add member variables of type

Game game;

CodebreakerUi codebreakerUi;

CodemakerUi codemakerUi;

JFrame frame;

JMenuBar menuBar;

JMenu gameMenu;

JMenu helpMenu;

JMenuItem newGameMenuItem;

JMenuItem exitMenuItem;

JMenuItem aboutMenuItem;

JMenuItem rulesMenuItem;

A custom constructor should be defined that receives a parameter of type Game class

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

A method initComponents() should initialize all the components for the UI and be called from the constructor

Set the default size of the JFrame

Set the default close operation of the JFrame

Use default layout manager BorderLayout

Set up the JMenuBar

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

Display a JOptionPane message informing the user:

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

Display a JOptionPane message informing the user:

Rules of the game as shown in Figure 6 below

CodebreakerUi.java

Add member variables of type:

JPanel codebreakerAttempt;

JPanel codebreakerColors;

Codebreaker codebreaker;

Create getters for the two JPanel member variables

A custom constructor should be defined that receives a parameter of type Codebreaker class

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

call method initComponents()

A method initComponents() should initialize all the components for the UI and be called from the constructorFor each JPanelSet the size of the JPanel using two methods to ensure the UI isnt too small

setMinimumSize()

setPreferredSize()

Add a titled border for each using method

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

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

CodemakerUi.java

Add member variables of type:

JPanel codemakerResponse;

JPanel secretCode;

Codebreaker codemaker;

Create getters for the two JPanel member variables

A custom constructor should be defined that receives a parameter of type Codebreaker class

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

call method initComponents()

A method initComponents() should initialize all the components for the UI and be called from the constructorFor each JPanelSet the size of the JPanel using two methods to ensure the UI isnt too small

setMinimumSize()

setPreferredSize()

Add a titled border for each using method

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

This can be temporary, it just shows the layout while there arent 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 8

image text in transcribed

Figure 1 Test Case 1

image text in transcribed

Figure 2 Mastermind Initial UI

image text in transcribed

Figure 3 Game Menu

image text in transcribed

Figure 4 Help Menu

image text in transcribed

Figure 5 About Menu Item

image text in transcribed

Figure 6 Rules Menu Item

image text in transcribed

Figure 7 Exit Menu Item

image text in transcribed

Figure 8 Project View

Code so far:

Constants.java

* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

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;

}

Codemaker.java:

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

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 final 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()

{

// 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! ");

secretCode.forEach((color) -> { System.out.println(color.toString()); });

}

/**

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

}

public void checkAttemptedCode(ArrayList attempt) {

codemakerResponse.clear(); //resetting the response

System.out.println("Codemaker is checking codebreaker attempt");

ArrayList evaluatedPos=new ArrayList(); //List for storing indices of right guesses

ArrayList whitePegs=new ArrayList(); //A temporary list for storing indices of matching colors, with wrong positions

int red_pegs=0,white_pegs=0;

ArrayList secret=new ArrayList();

secret.addAll(getSecretCode());

if(secret.equals(attempt)){

red_pegs=4;

white_pegs=0;

for(int i=0;i

codemakerResponse.add(Color.RED);

}

}else{

for(int i=0;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

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); /o pegs added

}

}

}

}

}

}

Codebreaker.java:

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

package core;

import java.awt.Color;

import java.lang.reflect.Field;

import java.util.ArrayList;

import java.util.Scanner;

import constants.constants;

public abstract class Codebreaker implements ICodebreaker

{

// member variables

private ArrayList codebreakerAttempt;

public Codebreaker()

{

// instanatiate the member variables

codebreakerAttempt = new ArrayList();

}

/**

* @return the codebreakerAttempt

*/

public ArrayList getCodebreakerAttempt()

{

consoleAttempt();

return codebreakerAttempt;

}

/**

* @param codebreakerAttempt the codebreakerAttempt to set

*/

public void setCodebreakerAttempt(ArrayList codebreakerAttempt)

{

this.codebreakerAttempt = codebreakerAttempt;

}

/** * * @param attempt * @return */ public boolean CheckCode(ArrayList 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();

System.out.println("Found correct color in right position!");

for (int i=0;i

if(attempt.get(i)==Color.RED){

System.out.print(i+" Red pegs "); //right guess in 'i'th position

}

}

System.out.println();

System.out.println("Found wrong color in wrong position");

for (int i=0;i

if(attempt.get(i)==Color.WHITE){

System.out.print(i+" White Pegs "); //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

String colorString=scanner.next().toUpperCase();

Color color=stringToColor(colorString);

if(color != null && !codebreakerAttempt.contains(color)){

codebreakerAttempt.add(color);

}else {

System.out.println("Invalid color choice, try again");

errorFlag=true;

break;

}

}

if(!errorFlag){

System.out.println("Codebreaker's attempt");

codebreakerAttempt.forEach((col) -> { 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:

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

package core;

import java.awt.Color;

import java.util.ArrayList;

import constants.constants;

public final 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() { @Override public void checkCode(ArrayList attempt) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } };

attempt = 0;

play();

}

public void play()

{

while(true){ // will loop again and again until codebreaker wins or runs out of attempts

if(attempt

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

}

}

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package core;

import java.awt.Color; import java.util.ArrayList; /** * */ public interface ICodebreaker { public void checkCode(ArrayList attempt); }

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package core;

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

public interface ICodemaker {

public void generateSecretCode(); public void checkAttemptedCode(ArrayList codebreakerAttempt); }

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package core;

/** * * @author Bryan tavarez */ public interface IGame { public void play(); }

Mastermind.java

package mastermind;

import core.Game; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import java.awt.event.*;

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();

JFrame myFrame = new JFrame ("Mastermind");

myFrame.setSize(600, 400); myFrame.setLocationRelativeTo(null); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.setVisible(true);

JMenuBar menubar = new JMenuBar(); myFrame.setJMenuBar(menubar);

JMenu game = new JMenu("Game"); menubar.add(game); JMenuItem NewGame = new JMenuItem("New Game"); game.add(NewGame); JMenuItem exit = new JMenuItem("Exit"); game.add(exit);

JMenu help = new JMenu("Help"); menubar.add(help); JMenuItem about = new JMenuItem("About"); help.add(about);

class exitaction implements ActionListener{ public void actionPerformed (ActionEvent e) { System.exit(0); } }

} }//MasterMindUi ui = new MasterMindUi(game);

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

//bryan tavarez

package userinterface;

import core.Game;

import constants.constants;

import javax.swing.*;

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions