- The assignment is in java Code-breaker Game Specification Code-breaker is a nu
ID: 3918237 • Letter: #
Question
- The assignment is in java
Code-breaker Game Specification
Code-breaker is a number guessing game. The computer chooses a 4-digit secret code at random. The player must guess what that number is. After each guess, a score is displayed that tells the player how many digits he guessed correctly and that are in the proper position in the secret code, and how many digits he guessed correctly but the guess is in the wrong position.
For example:
Secret Code
Guess
Score
1234
1243
2.2
1234
2345
0.3
1234
5234
3.0
How the program should work
Display greeting text and instructions to the user.
Generate a random secret code.
In a loop:
Ask for their guess.
Compare their guess against the generated secret code and compute a score.
Display the score.
If their score is 4.0, print “You win!”
Repeat until score is 4.0 or they run out of guesses.
If they run out of guesses, print “Game over.”
Ask them if they want to play again.
Repeat the game until they answer “n”.
Some other requirements
Initially, the game should use four-digit codes and allow six guesses. But your game should be capable of using codes having up to 9 digits and changing the number of guesses, using two constant variables. That means you will be using for loops for everything, not one code block for each individual digit or guess.
Secret Codes generated by the computer must not have duplicate digits.
How to program the game
I suggest you create a class to store the computer-generated secret code and handle the code manipulation operations.
You will need something like the following methods in this class:
Now all you need to do is write the necessary code in Main to interact with the user and the class you write that contains these three methods.
Your secret codes should be stored as int arrays, where each element in the array is one digit of the code. To keep things simple, you can ask the user for each digit in the guess individually, but if you do that, you must ask the user in such a way that they know which digit they are inputting. For example:
You will need two loops in Main: one to allow the user to repeat the game until they want to quit, and another to allow them to guess multiple times.
Secret Code
Guess
Score
1234
1243
2.2
1234
2345
0.3
1234
5234
3.0
Explanation / Answer
For this kind of game, first we can make a class like SecretCode which can have two constants for holding secret code default length and default no. of guesses. Now we can have two another variable which can change these default value at runtime. For storing secret code as you mentioned we need array. we also need to maintain uniqueness of digits in code when generating for this we can use java collection set just for checking uniqueness. So overall class will look like :
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
public class SecretCode {
private final int DEFAULTCODELENGTH = 4;
private final int DEFAULTGUESSES = 6;
private int codeLength = DEFAULTCODELENGTH;
private int guesses = DEFAULTGUESSES;
private int[] secretCodeNum;
private static SecretCode secretCode;
private SecretCode(int codeLength, int guesses) {
if (codeLength!=0)
this.codeLength = codeLength;
if (guesses!=0)
this.guesses = guesses;
}
public static SecretCode getInstance(int codeLength, int guesses) {
if (secretCode == null) {
secretCode = new SecretCode(codeLength, guesses);
}
return secretCode;
}
public void generateSecretCode() {
secretCodeNum = new int[codeLength];
Random rnd = new Random();
Set<Integer> set = new HashSet();
int i=0;
while (i<codeLength) {
int num = rnd.nextInt(10);
if (!set.contains(num)) {
secretCodeNum[i++] = num;
set.add(num);
}
}
}
public int findExactMatch(int[] guess) {
int score = 0;
for (int i=0; i<codeLength; i++) {
if (secretCodeNum[i] == guess[i]) {
score++;
}
}
return score;
}
public double findInExactMatch(int[] guess) {
double score = 0.0;
List<int[]> arrayList = Arrays.asList(secretCodeNum);
for (int i=0; i<codeLength; i++) {
if (arrayList.contains(guess[i])) {
score = score+0.1;
}
}
return score;
}
public int getCodeLength() {
return codeLength;
}
public int getGuesses() {
return guesses;
}
public String getSecretCode() {
StringBuffer str = new StringBuffer("");
for (int i : secretCodeNum) {
str.append(i);
}
return str.toString();
}
}
After that we can write another class CodeBreakerGame which has instance of this secretCode game and has main method which will provide user interaction and handling score and user guesses. It will look like :
import java.util.Scanner;
public class CodeBreakerGame {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int codeLength = 0;
int guesses = 0;
SecretCode secretCode;
int[] guess;
double score = 0.0;
String playagain = "y";
System.out.println("Welcome to the Code Breaker Game!!! If you want to change default codelength and no. of guesses, Please Enter Yes");
String res = in.nextLine();
if ((res).equalsIgnoreCase("Yes")) {
System.out.println("Enter code Length :");
codeLength = in.nextInt();
System.out.println("Enter Guesses : ");
guesses = in.nextInt();
}
secretCode = SecretCode.getInstance(codeLength, guesses);
while (!playagain.equalsIgnoreCase("n")) {
secretCode.generateSecretCode();
System.out.println("Secret Code is generated successfully");
for (int i=0; i<secretCode.getGuesses(); i++) {
guess = new int[secretCode.getCodeLength()];
for (int j=0; j<secretCode.getCodeLength(); j++) {
System.out.println("Enter guess digit "+j+" : ");
guess[j] = in.nextInt();
}
score = score + secretCode.findExactMatch(guess) + secretCode.findInExactMatch(guess);
if (score == 4.0) {
System.out.println("You Win !");
break;
} else {
System.out.println("Your Score is "+score);
}
}
System.out.println("You are out of guesses, secret code was "+secretCode.getSecretCode()+" If you don't want to play again enter n:");
playagain = in.next();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.