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

You are asked to write a Java program about the classic game called Mastermind .

ID: 3870379 • Letter: Y

Question

You are asked to write a Java program about the classic game called Mastermind. The description of Mastermind can be found in https://en.wikipedia.org/wiki/Mastermind_(board_game).

In this assignment, the digits 1-6 are used to represent the colours of the code pegs. At the beginning of the program, the code maker, i.e., the program itself, will randomly generate a secret code consists of 4 code pegs. In each round, the code breaker, i.e., the player, will guess the secret code by inputting 4 digits separated by space. The code maker will then check if the code pegs in the guess are the same as the code pegs in the secret code. If the guess and the secret code are the same, the player win. Otherwise, the code maker will give hints using 0 or 4 key pegs to the code breaker as follows: The number of BLACK key pegs refers to the number of code pegs in the guess code that are the same to the code pegs in the secret code in terms of digit and position; The number of WHITE key pegs refers to the number of code pegs in the guess code that have the same colour as the code pegs in the secret code. For example, if the secret code is “1 3 5 4” and the guess code is “1 4 3 6”, the key pegs are 1 BLACK and 2 WHITE because the first peg is “1” which is the same as the first peg in the secret code; the second and third pegs are found in the secrete code, but in a different position; the fourth peg is incorrect.

Your Task

You have to implement two classes, namely, CodeMaker and CodeBreaker according to the members and method headers as listed below:

// A member variable of Code to store the secret code private Code secretCode;

// setter of secretCode
public void setSecretCode(Code secretCode) { }

Page 1 of 4

// this method generate the secret code and store the secret code
// in the member variable secretCode. The secret code generated will // not have duplicated pegs. For example, it is not allowed to have // the secret code "1 2 2 4" because "2" is a duplicate.
public void genSecretCode() {
}

}

// setter of guessCode
public void setGuessCode(Code guessCode) { }

// This is a method to get the guess code from the player, and store // the input to guessCode.
public void getGuessCodeInput() {
}

}

In order to run the program, two other classes, namely, MastermindGame and Code. These two classes are given to you and can be downloaded from blackboard (MastermindGame.java and Code.java). You are NOT allowed to make any modification on these two classes.

Sample Run of Program

The followings show two sample runs of the program. The blue texts indicate the user inputs.

Welcome to Mastermind.
I generated a secrete code. Please guess:
1 2 3 4 Sorry! It is not correct.
Hints: 0 Black and 4 White
Please guess again (11 guesses left):
1 2 4 3 Sorry! It is not correct.

Hints: 1 Black and 3 White
Please guess again (10 guesses left):
1 3 2 4 Sorry! It is not correct.
Hints: 1 Black and 3 White
Please guess again (9 guesses left):
3 2 1 4 Sorry! It is not correct.
Hints: 0 Black and 4 White
Please guess again (8 guesses left):
2 4 1 3 Sorry! It is not correct.
Hints: 1 Black and 3 White
Please guess again (7 guesses left):
1 4 3 2 Sorry! It is not correct.
Hints: 0 Black and 4 White
Please guess again (6 guesses left):
2 3 4 1 Sorry! It is not correct.
Hints: 0 Black and 4 White
Please guess again (5 guesses left):
4 2 1 3 Sorry! It is not correct.
Hints: 2 Black and 2 White
Please guess again (4 guesses left):
4 2 3 1 Sorry! It is not correct.
Hints: 1 Black and 3 White
Please guess again (3 guesses left):
4 3 2 1 Sorry! It is not correct.
Hints: 2 Black and 2 White
Please guess again (2 guesses left):
4 1 2 3 Congratulations! You win!

Welcome to Mastermind.
I generated a secrete code. Please guess:
6 Sorry! It is not correct.
Hints: 1 Black and 2 White
Please guess again (11 guesses left):
1 2 3 4 Sorry! It is not correct.
Hints: 0 Black and 2 White
Please guess again (10 guesses left):
2 3 4 5 Sorry! It is not correct.
Hints: 0 Black and 3 White
Please guess again (9 guesses left):
3 Sorry! It is not correct.
Hints: 1 Black and 2 White
Please guess again (8 guesses left):
4 Sorry! It is not correct.
Hints: 2 Black and 0 White
Please guess again (7 guesses left):
5 Sorry! It is not correct.
Hints: 1 Black and 2 White
Please guess again (6 guesses left):
6 Sorry! It is not correct.
Hints: 0 Black and 3 White
Please guess again (5 guesses left):
6 Sorry! It is not correct.
Hints: 1 B
Please guess again (4 guesses left):5 Sorry! It is not correct.

Hints: 1 Black and 2 White
Please guess again (3 guesses left):
4 Sorry! It is not correct.

Hints: 0 Black and 2 White
Please guess again (2 guesses left):
3 Sorry! It is not correct.
Hints: 1 Black and 2 White
Please guess again (1 guesses left):
2 Sorry! It is not correct.
Hints: 1 Black and 2 White
Sorry! You loss.
The secrete code is: 3 5 6 2

Tips and Assumptions

1) A sample program can be downloaded from blackboard. After downloading and unzipping the file Asgn1.zip, double click the file run.bat. You have to have jdk installed to run this sample program.

2) You may assume that the player’s input must be valid. It means that he/she will only input 4 digits from 1-6 with a space in-between.

3) You should read the comment of the two programs Code.java and MastermindGame.java to understand the purpose of the methods. In particular, the classes CodeMaker and CodeBreaker need to make use of the methods of Code.

4) The method “nextInt()” can be called on a Scanner object to get the next integer input by the player.

Explanation / Answer

Solution=============================

CodeBreaker.java===========

public class CodeBreaker {

   // A member variable of Code to store the guess code
   Code guessCode;

   // default constructor
   public CodeBreaker() {

   }

   // getter of guessCode
   public Code getGuessCode() {
       return guessCode;
   }

   // setter of guessCode
   public void setGuessCode(Code guessCode) {
       this.guessCode = guessCode;
   }

   // This is a method to get the guess code from the player, and store // the
   // input to guessCode..
   public void getGuessCodeInput() {
       //Input via scanner
       Scanner in = new Scanner(System.in);
       //Using nextInt(), to take input for each code index
       int code[] = new int[4];
      
       for(int i=0;i<4;i++){
           code[i]=in.nextInt();
       }
      
       guessCode = new Code(code);
       //Closing in stream
       in.close();
   }

}


CodeMaker.java=========================

public class CodeMaker {

   // A member variable of Code to store the secret code private Code
   // secretCode;
   private ArrayList<Integer> possiblePegs;
   private Code secretCode;

   // getter of secretCode
   public Code getSecretCode() {
       return secretCode;
   }

   // setter of secretCode
   public void setSecretCode(Code secretCode) {
       this.secretCode = secretCode;
   }

   // Page 1 of 4

   // default constructor
   public CodeMaker() {
      
       //This array list will help in choosing unique random numbers between 1-6
       possiblePegs = new ArrayList<>();
       for (int i = 1; i < 7; i++) {
           possiblePegs.add(i);
       }

   }

   // this method generate the secret code and store the secret code
   // in the member variable secretCode. The secret code generated will // not
   // have duplicated pegs. For example, it is not allowed to have // the
   // secret code "1 2 2 4" because "2" is a duplicate.
   public void genSecretCode() {
       //Shuffling the arraylist
       Collections.shuffle(possiblePegs);
       //Putting the first 4 elements of list to generate secret code
       int[] code = { possiblePegs.get(0), possiblePegs.get(1), possiblePegs.get(2), possiblePegs.get(3) };
       secretCode = new Code(code);
   }

}

My Code class=================

class Code {

   private int digits[];

   public Code(int[] code) {
       digits = code;
   }

   public int[] getCode() {
       return digits;
   }

}

Note: you may need to adapt these files to work with Your Code and Mastermind classes, since they were not provided..

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote