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
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:
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.
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started