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
- 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 usingforloops for everything, not one code block for each individual digit or guess.
Secret Codes generated by the computermust 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:
generateSecretCode()
int calculateExactMatches()
int calculateInexactMatches()
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 asintarrays, 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:
Enter digit 1:
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.
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