Assignment Scope For this assignment the goal is to simulate the foundation of t
ID: 3871178 • Letter: A
Question
Assignment Scope
For this assignment the goal is to simulate the foundation of the components of the game MasterMind by creating a Codemaker, Codebreaker, and the Game; this includes:
Set up constants, which are used for clarity, safety and consistency throughout a project.
Establish the method signatures (i.e. methods that must be implemented in the classes) for the interfaces ICodemaker, ICodebreaker, and IGame.
Establish the member variables for the classes to replicate the real game, write custom constructors, generate getters/setters for the member variables, instantiate instances of classes, write a simple method to randomly generate the secret code in the Codemaker class.
To accomplish this, write the follow source code:
Create constants using “public static final”
Using Java API classes Arrays, ArrayList, Color, HashSet, Random and Set
Update the interfaces to include method signatures
Updating the classes to include
Member variables
Getters/setters for member variables
Use the Netbeans editor to create these for you by following these steps
Right click in the source code file
Select RefactoràEncapsulate Fields
Click on Select All button (on the right)
Click on Refactor button (on the bottom)
Custom constructor
Methods
Implementing interface methods
Instance variables
Import necessary classes
Use the Netbeans editor to assist you with this by following these steps
Right click in the source code file
Select Fix Imports
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
=
Tasks and Rubric
Activity
MasterMind project
MasterMind class
Method main() should:
Instantiate an instance of class Game
Constants
Constants class
Create constants by declaring them as “public static final”:
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));
ArrayList<Color> responseColors = new ArrayList<Color>(Arrays.asList(Color.RED, Color.WHITE));
int MAX_ATTEMPTS = 10;
int MAX_PEGS = 4;
int COLORS = 8;
core package
Codebreaker class
ArrayList<Color> codebreakerAttempt
Getter/setter for member variables
Empty parameter list
Instantiates the member variable
public void checkCode(ArrayList<Color> attempt)
Codemaker class
Set<Color> secretCode
ArrayList<Color> codemakerResponse
Getter/setter for member variables
Empty parameter list
Member variable secretCode should be instantiated as an instance of class HashSet()
Member variable codemakerResponse should be instantiated as an instance of class ArrayList()
Calls method generateSecretCode()
public void generateSecretCode()
public void checkAttemptedCode()
Instantiate and instance of Java API class Random
Use the method .nextInt() in class Random to select an index into the Constants.codeColors ArrayList, pass the ArrayList as an argument to the method call
Create an instance of Java API class Color and set it equal to the color stored at the index of the Constants.codeColors ArrayList by calling the get() method in class ArrayList passing the int from step i. above as an argument
Add the selected color to the Set member variable by calling method .add in class Set passing the Color object from step ii. above as and argument
Use an enhanced for loop to output the generated secret code
Game class
int attempt
Codebreaker codebreaker
Codemaker codemaker
Getter/setter for member variables
Empty parameter list
Instantiates the member variables
public void play()
ICodebreaker interface
public void checkCode(ArrayList<Color> attempt)
ICodemaker interface
public void generateSecretCode()
public void checkAttemptedCode()
IGame interface
public void play()
userInterface package
MasterMindUi class
MasterMind application
Test Case 1
Test Case 1 passes
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
The console window should be similar to figure 1
Java does NOT automatically translate its Color objects to a String value, rather it prints the RGB code of the color.
Figure 1 Generated secret code
I currently have this
mastermind package -> MasterMind.java
package mastermind;
import javax.swing.JOptionPane; //import JOptionPane
import core.Game;
import java.util.*;
/**
*
*
*/
public class MasterMind {
public static void main(String[] args) {
Game myGame = new Game();
System.out.println("Welcome to MasterMind!"); //Call static method System.out.println()
JOptionPane.showMessageDialog(null, "Let's Play MasterMind!"); //show dialog box saying Let's Play MasterMind! with a null argument
}
}
core package - > Codebreaker.java
package core;
import java.util.ArrayList;
import constants.Constants;
/**
*
*
*/
public class Codebreaker implements ICodebreaker { //implements codebreaker interface
ArrayList<Color> codebreakerAttempt;
public void setColor(Color){
this.Color = Color;
public Color getColor(){
return Color;
}
}
public void checkCode(ArrayList<Color> attempt)
}
constants package - > Constants.java
package constants;
import java.util.ArrayList;
import java.util.*;
/**
*
* @
*/
public class Constants {
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));
ArrayList<Color> responseColors = new ArrayList<Color>(Arrays.asList(Color.RED, Color.WHITE));
int MAX_ATTEMPTS = 10;
int MAX_PEGS = 4;
int COLORS = 8;
}
Codemaker.java
package core;
import constants.Constants;
/**
*
* @
*/
public class Codemaker implements ICodemaker { //implements codemaker interface
Set<Color> secretCode;
ArrayList<Color> codemakerResponse;
}
Game.java
package core;
/**
*
* @
*/
public class Game implements IGame {
}
Icodebreak.java
package core;
/**
*
* @
*/
public interface ICodebreaker { //code breaker interface
}
Icodemaker.java
package core;
/**
*
*
*/
public interface ICodemaker { //code maker interface
}
Igame.java
package core;
/**
*
* @
*/
public interface IGame { //game interface
}
MasteMindUI.java
Please help!
package userInterface;
/**
*
* @
*/
public class MasterMindUi {
}
Activity
MasterMind project
MasterMind class
Method main() should:
Instantiate an instance of class Game
Constants
Constants class
Create constants by declaring them as “public static final”:
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));
ArrayList<Color> responseColors = new ArrayList<Color>(Arrays.asList(Color.RED, Color.WHITE));
int MAX_ATTEMPTS = 10;
int MAX_PEGS = 4;
int COLORS = 8;
core package
Codebreaker class
Member variablesArrayList<Color> codebreakerAttempt
Getter/setter for member variables
Custom constructorEmpty parameter list
Instantiates the member variable
Implements ICodebreaker interface methodpublic void checkCode(ArrayList<Color> attempt)
Codemaker class
Member variablesSet<Color> secretCode
ArrayList<Color> codemakerResponse
Getter/setter for member variables
Custom constructorEmpty parameter list
Instantiates the member variablesMember variable secretCode should be instantiated as an instance of class HashSet()
Member variable codemakerResponse should be instantiated as an instance of class ArrayList()
Calls method generateSecretCode()
Implements ICodemaker interface methodspublic void generateSecretCode()
public void checkAttemptedCode()
Method generateSecretCode()Instantiate and instance of Java API class Random
Loop until four of the eight colors have been randomly selected to be the secret code with no duplicates (hint: member variable secretCode is of type Set which automatically does not allow for duplicates)Use the method .nextInt() in class Random to select an index into the Constants.codeColors ArrayList, pass the ArrayList as an argument to the method call
Create an instance of Java API class Color and set it equal to the color stored at the index of the Constants.codeColors ArrayList by calling the get() method in class ArrayList passing the int from step i. above as an argument
Add the selected color to the Set member variable by calling method .add in class Set passing the Color object from step ii. above as and argument
Use an enhanced for loop to output the generated secret code
Game class
Member variablesint attempt
Codebreaker codebreaker
Codemaker codemaker
Getter/setter for member variables
Custom constructorEmpty parameter list
Instantiates the member variables
Implements IGame interface methodspublic void play()
ICodebreaker interface
Add method signaturespublic void checkCode(ArrayList<Color> attempt)
ICodemaker interface
Add method signaturespublic void generateSecretCode()
public void checkAttemptedCode()
IGame interface
Add method signaturespublic void play()
userInterface package
MasterMindUi class
MasterMind application
Test Case 1
Test Case 1 passes
Source compiles with no errors
Source runs with no errors
Source includes comments
Explanation / Answer
package com.secryption.mastermind;
public class Game {
private int numberOfPegs = 0; //number of pegs the player wants to use
private String playerName = "";
int numberOfGuesses = 0;
boolean isAlive = true;
public static void main(String[] args) {
Game game1 = new Game();
game1.setupGame();
}
private void setupGame() { //here is where we get the name, pegs etc.
ScreenHelper sh1 = new ScreenHelper();
CodeBreaker cb1 = new CodeBreaker();
setPlayerName(sh1.getUserInput("Enter your name: "));
String pg = sh1.getUserInput("How many pegs would you like?");
setNumberOfPegs(Integer.parseInt(pg));
CodeMaker cm1 = new CodeMaker(numberOfPegs);
cm1.createBoard();
System.out.println(cm1.printBoard());
System.out.println("You may begin guessing now. Enter your guess as colors. For example with 4 pegs.");
System.out.println("rybl = Position 0=red, Position 1=yellow etc.");
System.out.println("r=red, y=yellow, b=black, w=white, l=blue, g=green, p=purple");
while(isAlive) {
String guess = cb1.makeGuess();
System.out.println("Your guess: " + guess);
String result = cm1.checkGuess(guess);
numberOfGuesses++;
if(result.equals("BBBB")) {
System.out.println("You win " + playerName + "! It took you " + numberOfGuesses + " to crack the code.");
isAlive = false;
} else {
System.out.println("Result: " + result);
}
}
}
public void setNumberOfPegs(int i) {
numberOfPegs = i;
}
public int getNumberOfPegs() {
return numberOfPegs;
}
public void setPlayerName(String n) {
playerName = n;
}
public String getPlayerName() {
return playerName;
}
}
package com.secryption.mastermind;
import java.util.ArrayList;
public class CodeMaker {
private int boardLength = 0;
private String[] colors = {"r", "g", "l", "y", "p", "b", "w"};
private ArrayList<String> board = new ArrayList<String>();
private ArrayList<String> tempBoard = new ArrayList<String>();
private String result = "";
public CodeMaker(int i) {
boardLength = i;
}
public int getBoardLength() {
return boardLength;
}
public void createBoard() {
for(int i = 0; i < boardLength; i++) { // get random colors for each of teh pegs
board.add(randomColor()); // add to arraylist. just add color
}
}
private String randomColor() {
int a = (int) (Math.random() * 7);
String r = colors[a];
return r;
}
public String printBoard() {
for ( String s : board) {
result = result + s;
}
return result;
}
private void makeTempBoard() {
tempBoard.clear();
for( String w : board) {
tempBoard.add(w);
}
}
public String checkGuess(String g) {
String result = "";
String piece = "";
makeTempBoard();
//System.out.println(tempBoard.size());
for(int i = 0; i < g.length(); i++) { //check each pair of guess. position/color.. should change this
String subA = g.substring(i, i + 1);
//System.out.println("The guess letter: " + subA);
for(int b = 0; b < tempBoard.size(); b++) { //take each part of the guess and check it against each board peg
//System.out.println("The tempBoad character: " + b);
if(subA.equals(tempBoard.get(b)) && (i == b)) {
piece = "B";
tempBoard.set(b, "*"); //set to * so we don't get double results.
break;
}
else if(subA.equals(tempBoard.get(b))) {
piece = "W";
//same here, remove item from arraylist.
tempBoard.set(b, "*");
break;
} else {
piece = "-";
} //end else
}//end if
result = piece + result;
}//end for
return result;
}//end method
}
package com.secryption.mastermind;
public class CodeBreaker {
public String makeGuess() {
ScreenHelper sh2 = new ScreenHelper();
String guess = sh2.getUserInput("Enter a guess: ");
return guess;
}
}
package com.secryption.mastermind;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ScreenHelper {
public String getUserInput(String prompt) {
String inputLine = null;
System.out.print(prompt + " ");
try {
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
inputLine = is.readLine();
if (inputLine.length() == 0 ) return null;
} catch (IOException e) {
System.out.println("IOException: " + e);
}
return inputLine;
}
}
public class JakobMastermind {
public static void main(String[] args) {
// INTRODUCTION
System.out.println("Hi and welcome to the game Mastermind!! :) Made by Jakob Petersson ");
// ANSWER/SECRET
// (4 arrays for a combination of 4 numbers that will guess a number between 1 and 6)
int[] secret=new int[4];
for(int i=0;i<4;i++){
secret[i]=(int)(Math.random()*6+1);
}
// (Type out the answer)
System.out.println("The secret: "+secret[0]+secret[1]+secret[2]+secret[3]);
// GUESSES
int[] guess=new int[4];
for(int i=0;i<4;i++){
do{
guess[i]=Kbd.readInt("Guess number "+(i+1)+": ");
}while(!(guess[i]>=1 && guess[i]<=6));
}
System.out.println("Your guess was: "+guess[0]+guess[1]+guess[2]+guess[3]+" ");
// CONTROL
// WHITE
int white=0;
if(guess[0]==secret[1] || guess[0]==secret[2] || guess[0]==secret[3]){
white=white+1;
}
if(guess[1]==secret[0] || guess[1]==secret[2] || guess[1]==secret[3]){
white=white+1;
}
if(guess[2]==secret[0] || guess[2]==secret[1] || guess[2]==secret[3]){
white=white+1;
}
if(guess[3]==secret[0] || guess[3]==secret[1] || guess[3]==secret[2]){
white=white+1;
}
// BLACK
int black=0;
if(guess[0]==secret[0]){
black=black+1;
}
if(guess[1]==secret[1]){
black=black+1;
}
if(guess[2]==secret[2]){
black=black+1;
}
if(guess[3]==secret[3]){
black=black+1;
}
// CONTROL - ANSWER
white=white-black;
if(white>1 && black>=1){
System.out.print("You have "+white+" white pins");
}else if(white==1 && black>=1){
System.out.print("You have "+white+" white pin");
}else if(white>1 && black==0){
System.out.println("You have "+white+" white pins.");
}else if(white==1 && black==0){
System.out.println("You have "+white+" white pin.");
}
if(black>1 && white>=1){
System.out.println(" and "+black+" black pins.");
}else if(black==1 && white>=1){
System.out.println(" and "+black+" black pin.");
}else if(black==1 && white<1){
System.out.println("You have "+black+" black pin.");
}else if(black>1 && white<1){
System.out.println("You have "+black+" black pins.");
}
// WIN
if(secret[0]==guess[0] && secret[1]==guess[1] && secret[2]==guess[2] && secret[3]==guess[3]){
System.out.println(" Congratulations, you've won!! :D");
}
}
}
import java.io.*;
import java.util.*;
public class Main {
private BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
private int[] guess = new int[4];
private int[] secret = new int[4];
private int turn = 1;
public static void main(String[] args) throws IOException {
new Main ();
}
Main() throws IOException {
// intro //
System.out.println ("Hi and welcome to the game Mastermind!! :) Made by Jakob Petersson ");
// secret = {(w,x,y,z): w,x,y,z € {1,2,3,4,5,6}} //
for (int i = 0; i < secret.length; i++)
secret[i] = new Random ().nextInt (6) + 1;
// print the answer //
System.out.println ("The secret: " + secret[0] + secret[1] + secret[2] + secret[3]);
do {
// ask for quess //
for (int i = 0; i < 4; i++) {
do {
System.out.println ("Guess number " + (i + 1) + ": ");
guess[i] = Integer.parseInt (reader.readLine ());
} while (!(guess[i] >= 1 && guess[i] <= 6));
}
System.out.println ("Your guess was: " + guess[0] + guess[1] + guess[2] + guess[3] + " ");
// white and black pins//
boolean[] white = new boolean[4], black = new boolean[4];
Arrays.fill (white, false);
Arrays.fill (black, false);
for (int i = 0; i < white.length; i++) {
black[i] = guess[i] == secret[i];
if (!black[i]) {
int j = 0;
for (int k = 0; k < 3; k++, j++) {
if (j == i)
j++;
white[i] = white[i] || (guess[i] == secret[j]);
}
}
}
int whitePins = 0, blackPins = 0;
for (int i = 0; i < white.length; i++) {
if (white[i])
whitePins++;
if (black[i])
blackPins++;
}
System.out.println ("You have " + whitePins + " white pin" + (whitePins == 1 ? "" : "s") + " and " + blackPins + " black pin"
+ (blackPins == 1 ? "" : "s"));
turn ++;
} while (!(secret[0] == guess[0] && secret[1] == guess[1] && secret[2] == guess[2] && secret[3] == guess[3]) && turn <= 8);
System.out.println (" Congratulations, you've won!! :D");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.