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

Package Name: proj03 Source File Name: (Submit zip of these) Mastermind.java (co

ID: 3709279 • Letter: P

Question

Package Name:

proj03

Source File Name:

(Submit zip of these)

Mastermind.java (contains main())

MastermindPuzzle.java

EasyMastermindPuzzle.java

HardMastermindPuzzle.java

Winnable.java

If doing part 2, also include:

MastermindGUI.java (contains main())

FXMastermind.fxml

FXMastermindController.java                                                                                                                            

Test Files:

C2800_Proj3_Mastermind _Test.zip

Mastermind is a game where the player tries to figure out a pattern based on a series of clues. 4 different colored pegs (R for red, B for blue, G for green, W for white, P for purple, and Y for yellow) are chosen and hidden from the player. Then the user picks 4 pegs and guesses at the pattern. The computer will tell the user how many pegs are the correct color (hint, hint – you may want to reuse them in the next guess). The computer will also tell the user how many of the correctly colored pegs are also in the correct position. The user has up to 10 attempts to figure out the pattern.

This project should be done in 2 parts. The first part is a text based version of the game. Completing this part is worth up to 90%. If you complete the first part, then you may add a GUI to it for the last 10%. If you do the GUI portion, you still need to submit the text-based version. I will first test the text-based version. I will only look at the GUI version if your text-based version works.

Now even if you don’t write the GUI portion, I should be able to use your Mastermind classes and pair it with someone’s GUI classes and have it work the same. So, here is something you can use for main() in the text-based version:

On a side note, I realize that this is not the only way to accomplish the game. But when working in a team, public methods are often negotiated ahead of time and you must conform. Consider us in a team and I wrote the text-based main().

public static void main(String[] args)

{

     Scanner input = new Scanner(System.in);

     MastermindPuzzle mp;

     int easyHard;

     int puzzleNum;

    

     System.out.print("1=easy, 2=hard? ");

     easyHard = input.nextInt();

     System.out.println();

    

     System.out.print("Puzzle num? ");

     puzzleNum = input.nextInt();

     System.out.println();

    

     if (easyHard == 1)

     {

           mp = new EasyMastermindPuzzle();

     }

     else

     {

           mp = new HardMastermindPuzzle();

     }

     mp.startGame(puzzleNum);

    

     char[] guess = new char[mp.getNumPieces()];

     while (mp.stillPlaying())

     {

           System.out.print("Guess? ");

           for (int i = 0; i < guess.length; i++)

           {

                guess[i] = (input.next()).charAt(0);

           }

           System.out.println();

          

           mp.makeGuess(guess);

          

           System.out.printf("%20d %d --> ", mp.getLastCorrectColorCount(), mp.getLastCorrectPositionCount());

           for (int i = 0; i < guess.length; i++)

           {

                System.out.print(guess[i]);

                System.out.print(' ');

           }

           System.out.println();

     }

     if (mp.won())

     {

           System.out.println("Congratulations!");

     }

     else

     {

           System.out.println("Sorry, you lost");

     }

}

As a result of main() above – here are a few notes:

Mastermind class

Only needs to include main() above

MastermindPuzzle class

All instance variables should be private (not public or protected)

Should be abstract

Needs to implement Winnable interface

Need at least the default constructor

getNumPieces needs to return an integer with the number of pegs in a puzzle (currently 4 – use constants!)

chooseSolution should accept an integer that is the puzzle to choose and it must be abstract

startGame should accept an integer that is the puzzle to choose (1 through 3 or -1 for a random puzzle)

You must call chooseSolution in this method

I recommend that this method also reset your other variables that remember the state of the current game

makeGuess accepts a character array with the letters to check

I recommend that this method increase the number of guesses made, and set 2 instance variables that indicate the number of correctly colored pegs and the number in the correct position

This is the longest, most complicated method in the class

getLastCorrectColorCount needs to return the number of pegs that are the correct color in the last guess

getLastCorrectPositionCount needs to return the number of pegs that are in the correct position in the last guess

You may add extra methods to help yourself

EasyMastermindPuzzle class

This should be a subclass of MastermindPuzzle

This class ONLY needs constructor(s) and chooseSolutionchooseSolution should do the following:

If the parameter is 1, set the puzzle to R, G, B, W

If the parameter is 2, set the puzzle to P, R, W, Y

If the parameter is 3, set the puzzle to W, B, Y, P

If the parameter is -1, set the puzzle to random colors (4 out of R, G, B, W, Y, P)

HardMastermindPuzzle class

This should be a subclass of MastermindPuzzle

This class ONLY needs constructor(s) and chooseSolutionchooseSolution should do the following:

If the parameter is 1, set the puzzle to R, G, B, R

If the parameter is 2, set the puzzle to P, R, P, Y

If the parameter is 3, set the puzzle to W, W, Y, P

If the parameter is -1, set the puzzle to random colors (4 out of R, G, B, W, Y, P) with at least 1 duplicate color within in the puzzle

Winnable interface

This should have 3 methods – each returns a boolean: won, lost, stillPlaying

Sample Run #1: (the highlighted text is what the user types)

1=easy, 2=hard? 1

Puzzle num? 1

Guess? R G B Y

                   3 3 --> R G B Y

Guess? R G B W

                   4 4 --> R G B W

Congratulations!

Sample Run #2: (the highlighted text is what the user types)

1=easy, 2=hard? 2

Puzzle num? 1

Guess? R Y B B

                   2 2 --> R Y B B

Guess? R R G R

                   3 2 --> R R G R

Guess? R G B R

                   4 4 --> R G B R

Congratulations!

Sample Run #3: (the highlighted text is what the user types)

1=easy, 2=hard? 1

Puzzle num? 1

Guess? P Y G B

                   2 0 --> P Y G B

Guess? R G B Y

                   3 3 --> R G B Y

Guess? G R P Y

                   2 0 --> G R P Y

Guess? B G R W

                   4 2 --> B G R W

Guess? B G R W

                   4 2 --> B G R W

Guess? B G R W

                   4 2 --> B G R W

Guess? B G R W

                   4 2 --> B G R W

Guess? B G R W

                   4 2 --> B G R W

Guess? B G R W

                   4 2 --> B G R W

Guess? B G R W

                   4 2 --> B G R W

Sorry, you lost

Part 2: (The GUI part)

If you choose to do the GUI version, then you need to write 2 additional classes and a .fxml file:

MasterminGUI

This class contains main() – it only needs to create MastermindFrame and display it

FXMastermindController (variables and event handlers for FXMastermind.fxml)

This class contains all of variables for the controls, a MastermindPuzzle variable, and the event handlers

You should need 3 event handlers (2 for buttons and 1 anonymous one for the game number slider)

The controls should be:Labels for Easy, Hard, Game… (game number), the current game number chosen, and the results.

I chose a label for results because it will easily size to handle newline characters

Use a slider for easy/hard and game number.

For the easy/hard slider, I made the slider go from 0 to 1 (0 is easy, 1 is hard)

For the game numbers, I made it go from 1 to 4, where I let 4 represent random

Have an event handler for this slider that updates the game number to the right of the slider with the currently chosen game number. Make sure it says random if the last value is chosen in the slider.

Use a button for start and guess – You will need an event handler for each of these

These 2 event handlers should generally be pieces of main. You’ll have to replace the prompting code with getting values from the controls instead of reading it from a Scanner object.

Use 4 text fields for each peg of the user’s current guess

Here’s what it should look like when you start the program (The title bar should show Mastermind):

After I chose easy and puzzle 1, click on start. Then make a few guesses and here’s what it looks like:

When the user wins or loses, display the win/lose message at the bottom on the results label. For example,

Package Name:

proj03

Source File Name:

(Submit zip of these)

Mastermind.java (contains main())

MastermindPuzzle.java

EasyMastermindPuzzle.java

HardMastermindPuzzle.java

Winnable.java

If doing part 2, also include:

MastermindGUI.java (contains main())

FXMastermind.fxml

FXMastermindController.java                                                                                                                            

Test Files:

C2800_Proj3_Mastermind _Test.zip

Explanation / Answer

package strategy;

import java.util.LinkedList;

import java.util.List;

public class Code {

       private final T[] code;

       public Code(T... t) {

              this.code = t.clone();

       }

       public boolean equals(Object other) {

              if (other instanceof Code) {

                     Code that = (Code) other;

                     if (this.code.length != that.code.length) {

                           return false;

                     }

                     for (int i = 0; i < code.length; i++) {

                           if (this.code[i] != that.code[i]) {

                                  return false;

                           }

                     }

                     return true;

              }

              return false;

       }

       public String toString() {

              String result = "";

              for (T t : code) {

                     result += "T[" + t.i + "]";

              }

              return result;

       }

       public boolean contains(T t) {

              for (T x : this.code) {

                     if (x == t) {

                           return true;

                     }

              }

              return false;

       }

       public Answer compare(Code other) {

              int blacks = 0;

              int whites = 0;

              for (int i = 0; i < this.code.length; i++) {

                     if (code[i] == other.code[i]) {

                           blacks++;

                     } else if (this.contains(other.code[i])) {

                           whites++;

                     }

              }

              return new Answer(blacks, whites);

       }

       public T get(int i) {

              return this.code[i];

       }

       public int getLength() {

              return this.code.length;

       }

       public static List<Code> createAllCodes(int length) {

              final List<Code> result = new LinkedList<Code>();

              _createAllCodes(result, length, new T[length]);

              return result;

       }

       private static void _createAllCodes(List<Code> codes, int length, T[] ts) {

              if (length == 0) {

                     codes.add(new Code(ts));

              } else {

                     for (T t : T.values()) {

                           ts[length-1] = t;

                           _createAllCodes(codes, length - 1, ts);

                     }

              }

       }

}