Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Sample Run of Program The followings show two sample runs of the program. The blue texts indicate the user inputs. use this coding ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- public

image text in transcribed

image text in transcribed Sample Run of Program The followings show two sample runs of the program. The blue texts indicate the user inputs.

image text in transcribed

image text in transcribed use this coding -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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; }

} --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

}

}

// default constructor public CodeMaker) t // 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 1 // A member variable of Code to store the guess code Code guessCode: // default constructor public CodeBreaker) i // getter of guessCode public Code getGuessCode) // setter of guessCode public void setGuessCode (Code guessCode) i // This is a method to get the guess code from the player, and store // the input to guessCode. public void getGuessCodeInput) t 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. d.com/bbcswebdav/pid-1299944-dt-content-rid-501/233_1/courses/cstS- Please guess again (2 guesses left): 4 1 2 3 Congratulations! You win! Welcome to Mastermind I generated a secrete code. Please guess: 6 543 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 45 Sorry! It is not correct Hints: 0 Black and 3 White Please guess again (9 guesses left) 3 4 5 6 Sorry! It is not correct Hints 1 Black and 2 White Please guess again (8 guesses left): 4 5 6 1 Sorry! It is not correct. Hints 2 Black and 0 White Please guess again (7 guesses left): 5 6 1 2 Sorry! It is not correct. Hints 1 Black and 2 White Please guess again (6 guesses left): 6 1 2 3 Sorry! It is not correct Hints: 0 Black and 3 White Please guess again (5 guesses left) 6 5 4 3 Sorry! It is not correct. Hints 1 Black and 2 White Page 3 of 4 Please guess again (4 guesses left): 54 3 2 Sorry! It is not correct. Hints 1 Black and 2 White Please guess again (3 guesses left): 4 3 2 1 Sorry! It is not correct Hints: 0 Black and 2 White Please guess again (2 guesses left): 3 2 1 6 Sorry! It is not correct. Hints 1 Black and 2 White Please guess again (1 guesses left): 2 165 Sorry! It is not correct. Hints: 1 Black and 2 White Sorry! You loss The secrete code is: 3 5 6 2

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