Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are asked to write a Java program about the classic game called Mastermind. The digits 1-6 are used to represent the colours of the

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

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.

Task:

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

public class CodeMaker {

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

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

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

Page 1 of 4

// default constructor public CodeMaker() { }

// 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() { }

}

import java.util.Scanner; 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() { }

// 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.

MastermindGame.java :

public class MastermindGame { // The number of possible by guess public static final int MaxNumGuess = 12; // The main program public static void main(String[] args) { // create the CodeMaker and CodeBreaker objects CodeMaker cm = new CodeMaker(); CodeBreaker cb = new CodeBreaker(); // generate the secret code cm.genSecretCode(); System.out.println("Welcome to Mastermind."); System.out.print("I generated a secrete code. Please guess: "); for (int i = 0; i < MaxNumGuess; i++) { // get player's input cb.getGuessCodeInput(); // match the input and the secret code if (cm.getSecretCode().compare(cb.getGuessCode())) { System.out.println("Congratulations! You win!"); return; } // find the hints by coder maker int numBlack = cm.getSecretCode().getNumBlackKeyPegs(cb.getGuessCode()); int numWhite = cm.getSecretCode().getNumWhiteKeyPegs(cb.getGuessCode()); System.out.println("Sorry! It is not correct."); System.out.println("Hints: " + numBlack + " Black and " + numWhite + " White"); if (i != MastermindGame.MaxNumGuess - 1) { System.out.print("Please guess again (" + (MastermindGame.MaxNumGuess - i - 1) + " guesses left):"); } } System.out.println("Sorry! You loss."); System.out.print("The secrete code is: "); cm.getSecretCode().printCode(); } }

Code.java:

public class Code { public static final int NumDistinctCode = 6; // The four pegs private int peg1; private int peg2; private int peg3; private int peg4; // Default constructor public Code() { setPeg1(0); setPeg2(0); setPeg3(0); setPeg4(0); } // A constructor that a Code object using four pegs public Code(int p1, int p2, int p3, int p4) { setPeg1(p1); setPeg2(p2); setPeg3(p3); setPeg4(p3); } // Generate a random code. The code may contain duplicated pegs. public void genRandomCode() { peg1 = (int) (Math.random() * Code.NumDistinctCode) + 1; peg2 = (int) (Math.random() * Code.NumDistinctCode) + 1; peg3 = (int) (Math.random() * Code.NumDistinctCode) + 1; peg4 = (int) (Math.random() * Code.NumDistinctCode) + 1; } // set the four pegs public void setPegs(int p1, int p2, int p3, int p4) { peg1 = p1; peg2 = p2; peg3 = p3; peg4 = p4; } // check if there is duplicated pegs in the code public boolean hasDuplicatedPegs() { return getPeg1() == getPeg2() || getPeg1() == getPeg3() || getPeg1() == getPeg4() || getPeg2() == getPeg3() || getPeg2() == getPeg4() || getPeg3() == getPeg4(); } // return true if the code of this object is the same as the code of c. // return false otherwise. public boolean compare(Code c) { return getPeg1() == c.getPeg1() && getPeg2() == c.getPeg2() && getPeg3() == c.getPeg3() && getPeg4() == c.getPeg4(); } // return the number of black key pegs by comparing this object and c. public int getNumBlackKeyPegs(Code c) { return (getPeg1() == c.getPeg1() ? 1 : 0) + (getPeg2() == c.getPeg2() ? 1 : 0) + (getPeg3() == c.getPeg3() ? 1 : 0) + (getPeg4() == c.getPeg4() ? 1 : 0); } // return the number of white key pegs by comparing this object and c. public int getNumWhiteKeyPegs(Code c) { return (getPeg1() == c.getPeg2() ? 1 : 0) + (getPeg1() == c.getPeg3() ? 1 : 0) + (getPeg1() == c.getPeg4() ? 1 : 0) + (getPeg2() == c.getPeg1() ? 1 : 0) + (getPeg2() == c.getPeg3() ? 1 : 0) + (getPeg2() == c.getPeg4() ? 1 : 0) + (getPeg3() == c.getPeg1() ? 1 : 0) + (getPeg3() == c.getPeg2() ? 1 : 0) + (getPeg3() == c.getPeg4() ? 1 : 0) + (getPeg4() == c.getPeg1() ? 1 : 0) + (getPeg4() == c.getPeg2() ? 1 : 0) + (getPeg4() == c.getPeg3() ? 1 : 0); } public void printCode() { System.out.println(peg1 + " " + peg2 + " " + peg3 + " " + peg4); } public int getPeg1() { return peg1; } public void setPeg1(int peg1) { this.peg1 = peg1; } public int getPeg2() { return peg2; } public void setPeg2(int peg2) { this.peg2 = peg2; } public int getPeg3() { return peg3; } public void setPeg3(int peg3) { this.peg3 = peg3; } public int getPeg4() { return peg4; } public void setPeg4(int peg4) { this.peg4 = peg4; } }

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 BPlease 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 players 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions