MastermindUijava 1Add member variables of type aGame game bC
MastermindUi.java
1.Add member variables of type
a.Game game;
b.CodebreakerUi codebreakerUi;
c.CodemakerUi codemakerUi;
d.JFrame frame;
e.JMenuBar menuBar;
f.JMenu gameMenu;
g.JMenu helpMenu;
h.JMenuItem newGameMenuItem;
i.JMenuItem exitMenuItem;
j.JMenuItem aboutMenuItem;
k. 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
Set up the JMenuBar
i.JMenu Game should be added to the JMenuBar
ii.JMenuItems New Game and Exit should be added to the JMenu Game
iii.JMenu Help should be added to the JMenuBar
iv.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()
For each JPanel
i.Set the size of the JPanel using two methods to ensure the UI isn’t too small
setMinimumSize()
setPreferredSize()
ii.Add a titled border for each using method
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()
For each JPanel
i.Set the size of the JPanel using two methods to ensure the UI isn’t too small
setMinimumSize()
setPreferredSize()
ii.Add a titled border for each using method
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
|    MastermindUi.java  |      1.Add member variables of type a.Game game; b.CodebreakerUi codebreakerUi; c.CodemakerUi codemakerUi; d.JFrame frame; e.JMenuBar menuBar; f.JMenu gameMenu; g.JMenu helpMenu; h.JMenuItem newGameMenuItem; i.JMenuItem exitMenuItem; j.JMenuItem aboutMenuItem; k. 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 i.JMenu Game should be added to the JMenuBar ii.JMenuItems New Game and Exit should be added to the JMenu Game iii.JMenu Help should be added to the JMenuBar iv.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 constructor   For each JPanel i.Set the size of the JPanel using two methods to ensure the UI isn’t too small setMinimumSize() setPreferredSize() ii.Add a titled border for each using method 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  |   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 constructor   For each JPanel i.Set the size of the JPanel using two methods to ensure the UI isn’t too small setMinimumSize() setPreferredSize() ii.Add a titled border for each using method 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  |   
Solution
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<Color> 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 < 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() {
}
}
//CodebreakerUi.java
package userinterface;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Dimension;
import core.Codebreaker;
public class CodebreakerUi {
// Instance variables
private JPanel codebreakerAttempt;
private JPanel codebreakerColors;
private Codebreaker codebreaker;
/**
* Constructor
*
* @param codebreaker
* - Codebreaker object
*/
public CodebreakerUi(Codebreaker codebreaker) {
initComponents(codebreaker);
}
/**
* Initialize all the components for the UI.
*
* @param codebreaker
* - Codebreaker object
*/
private void initComponents(Codebreaker codebreaker) {
this.codebreaker = codebreaker;
this.codebreakerAttempt = new JPanel();
this.codebreakerColors = new JPanel();
// Set jpanel sizes
codebreakerAttempt.setMinimumSize(new Dimension(430, 400));
codebreakerAttempt.setPreferredSize(new Dimension(430, 400));
codebreakerColors.setMinimumSize(new Dimension(300, 50));
codebreakerColors.setPreferredSize(new Dimension(300, 50));
// Set border
codebreakerAttempt.setBorder(BorderFactory.createTitledBorder(\"Codebreaker Attempt\"));
codebreakerColors.setBorder(BorderFactory.createTitledBorder(\"Codebreaker Colors\"));
}
/**
* Returns the codebreakerAttempt
*/
public JPanel getCodebreakerAttempt() {
return codebreakerAttempt;
}
/**
* Returns the codebreakerColors
*/
public JPanel getCodebreakerColors() {
return codebreakerColors;
}
}
//CodemakerUi.java
package userinterface;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import core.Codemaker;
public class CodemakerUi {
// Instance variables
private JPanel codemakerResponse;
private JPanel secretCode;
private Codemaker codemaker;
/**
* Constructor
*
* @param codemaker
* - Codemaker object
*/
public CodemakerUi(Codemaker codemaker) {
initComponents(codemaker);
}
/**
* Initialize all the components for the UI.
*
* @param codemaker
* - Codemaker object
*/
private void initComponents(Codemaker codemaker) {
this.codemaker = codemaker;
this.codemakerResponse = new JPanel();
this.secretCode = new JPanel();
// Set jpanel sizes
codemakerResponse.setMinimumSize(new Dimension(150, 400));
codemakerResponse.setPreferredSize(new Dimension(150, 400));
secretCode.setMinimumSize(new Dimension(300, 50));
secretCode.setPreferredSize(new Dimension(300, 50));
// Set border
codemakerResponse.setBorder(BorderFactory.createTitledBorder(\"Codemaker Response\"));
secretCode.setBorder(BorderFactory.createTitledBorder(\"Secret Code\"));
}
/**
* Returns the codemakerResponse
*/
public JPanel getCodemakerResponse() {
return codemakerResponse;
}
/**
* Returns the secretCode
*/
public JPanel getSecretCode() {
return secretCode;
}
}
// MasterMindUi.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.
*/
//bryan tavarez
package userinterface;
import core.Game;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MasterMindUi {
// Instance variables
private Game game;
private CodebreakerUi codebreakerUi;
private CodemakerUi codemakerUi;
private JFrame frame;
private JMenuBar menuBar;
private JMenu gameMenu;
private JMenu helpMenu;
private JMenuItem newGameMenuItem;
private JMenuItem exitMenuItem;
private JMenuItem aboutMenuItem;
private JMenuItem rulesMenuItem;
/**
* Constructor
*
* @param game
* - Game object
*/
public MasterMindUi(Game game) {
this.game = game;
// Instantiate CodemakerUI
this.codemakerUi = new CodemakerUi(game.getCodemaker());
// Instantiate CodebreakerUI
this.codebreakerUi = new CodebreakerUi(game.getCodebreaker());
initComponents();
}
/**
* Initializes the UI components
*/
private void initComponents() {
frame = new JFrame(\"Mastermind\");
// Set the default size of the JFrame
frame.setSize(600, 500);
// Set the default close operation of the JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Use default layout manager BorderLayout
frame.setLayout(new BorderLayout(10, 0));
// Center frame to the screen
frame.setLocationRelativeTo(null);
// Set up the JMenuBar
menuBar = new JMenuBar();
// Add JMenu Game to the JMenuBar
gameMenu = new JMenu(\"Game\");
menuBar.add(gameMenu);
// Add JMenuItems New Game and Exit the JMenu Game
newGameMenuItem = new JMenuItem(\"New Game\");
gameMenu.add(newGameMenuItem);
exitMenuItem = new JMenuItem(\"Exit\");
gameMenu.add(exitMenuItem);
// Add JMenu Help to the JMenuBar
helpMenu = new JMenu(\"Help\");
menuBar.add(helpMenu);
// Add JMenuItems About and Game Rules to the JMenu Help
aboutMenuItem = new JMenuItem(\"About\");
helpMenu.add(aboutMenuItem);
rulesMenuItem = new JMenuItem(\"Game Rules\");
helpMenu.add(rulesMenuItem);
// Add JMenuBar to the JFrame
frame.setJMenuBar(menuBar);
// Add the CodemakerUi JPanels to the JFrame using the getters defined
// in the class
frame.add(this.codemakerUi.getSecretCode(), BorderLayout.NORTH);
frame.add(this.codemakerUi.getCodemakerResponse(), BorderLayout.EAST);
// Add the CodebreakerUi JPanels to the JFrame using the getters defined
// in the class
frame.add(this.codebreakerUi.getCodebreakerAttempt(), BorderLayout.WEST);
frame.add(this.codebreakerUi.getCodebreakerColors(), BorderLayout.SOUTH);
// Add action listener to exit menu item
exitMenuItem.addActionListener(new ExitActionListener());
// Add action listener to about menu item
aboutMenuItem.addActionListener(new AboutActionListener());
// Add action listener to game rules menu item
rulesMenuItem.addActionListener(new RulesActionListener());
// Set the visibility of the JFrame
frame.setVisible(true);
}
// Inner class to create an ActionListener for Exit menu item
class ExitActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
// Display a JOptionPane message confirming the user wants to exit
// using method showConfirmDialog()
int ans = JOptionPane.showConfirmDialog(null, \"Confirm to exit Mastermind?\", \"Exit?\",
JOptionPane.YES_NO_OPTION);
// 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
if (ans == 0)
System.exit(0);
}
}
// Inner class to create an ActionListener for About menu item
class AboutActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
// Display a JOptionPane message informing the user:
// Application name and version
// Author
// Date of development
JOptionPane.showMessageDialog(null, \"Mastermind version 1.0\ Karin Whiting\ Fall 2018\");
}
}
// Inner class to create an ActionListener for Game rules menu item
class RulesActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
String rules = \"Step 1: The codemaker selects a four color secret code, in any order, no duplicate colors.\ \ \"
+ \"Step 2: The codemaker places a guess in the bottom row, no duplicate colors.\ \ \"
+ \"Step 3: The codemaker gives feedback next to each guess row with four pegs\ \"
+ \"~Each red peg means that one of the guessed colors is correct, and is in the right location.\ \"
+ \"~Each white peg means that one of the guessed colors is correct, but is in the wrong location.\ \ \"
+ \"Step 4: Repeat with the next row, unless the secret code was guessed on the first turn\ \ \"
+ \"Step 5: Continue until the secret code is guessed or there are no more guesses left, there are 10 attempts\";
// Display a JOptionPane message informing the user about the rules
// of the game
JOptionPane.showMessageDialog(null, rules);
}
}
}
//Mastermind.java
package mastermind;
import core.Game;
import userinterface.MasterMindUi;
import javax.swing.JOptionPane;
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);
}
}