JAVA MASTERMIND The computer will randomly select a four-character mastercode. Each character represents the first letter of...

80.2K

Verified Solution

Question

Programming

JAVA MASTERMIND

The computer will randomly select a four-character mastercode.Each character represents the first letter of a color from thevalid color set. Our valid color choices will be: (R)ed, (G)reen,(B)lue and (Y)ellow. Any four-character combination from the validcolor set could become the mastercode. For example, a validmastercode might be: RGBB or YYYR.

The game begins with the computer randomly selecting amastercode. The user is then given up to 6 tries to guess themastercode. The user must guess the correct color sequence in thecorrect order. After each user guess, the computer respondsindicating how many colors the user guessed correctly and how manyof those were in the correct position. This information helps theuser make a better (and hopefully more accurate) guess. If the usercorrectly guesses the code in 6 tries or less, the user wins theround. Otherwise, the user loses the round. After each round, theuser is given the option to play another round, at which point thegame either continues or ends. When the game is completelyfinished, some overall playing statistics should be displayed. Thisincludes how many rounds the user won as well as lost, in additionto the user's overall winning percentage.

Sample Run

WELCOME TO MASTERMIND

<-- blank line

How to Play:

1. I will pick a 4 character color code out of the followingcolors: Yellow, Blue, Red, Green.

2. You try to guess the code using only the first letter of anycolor. Example if you type YGBR that means you guess Yellow, Green,Blue, Red.

3. I will tell you if you guessed any colors correct and whetheror not you guess them in the right order.

<-- blank line

LET'S PLAY!

<-- blank line

Ok, I've selected my secret code. Try and guess it.

<--- Blank line

Enter guess #1 (e.g., YBRG ): ZZZZZ

Please enter a valid guess of correct length and colors

<--- Blank line

Enter guess #1 (e.g., YBRG ): YYYY

You have 2 colors correct

2 are in the correct position

<--- Blank line

Enter guess #2 (e.g., YBRG ): YBYY

You have 3 colors correct

3 are in the correct position

Enter guess #3 (e.g., YBRG ): YBYR

You have 3 colors correct

3 are in the correct position

Enter guess #4 (e.g., YBRG ): YBYG

You have 3 colors correct

3 are in the correct position

Enter guess #5 (e.g., YBRG ): YBYR

You have 3 colors correct

3 are in the correct position

Enter guess #6 (e.g., YBRG ): YBYB

That's correct! You win this round. Bet you can't do itagain!

Play again (Y/N)? k

Please enter a valid response (Y/N):

Y

<--- Blank line

Ok, I've selected my secret code. Try and guess it.

<--- Blank line

Enter guess #1 (e.g., YBRG ): GBYG

That's correct! You win this round. Bet you can't do itagain!

Play again (Y/N)? N

<--- Blank line

YOUR FINAL STATS:

Rounds Played: 2

Won: 2 Lost: 0

Winning Pct: 100.00%

Sample Run (user does not win)

<-- user makes several bad guesses before the outputbelow

Enter guess #6 (e.g., YBRG ): yyyy

You have 1 colors correct

1 are in the correct position

No more guesses. Sorry you lose. My sequence was YRRR

Play again (Y/N)?

Required Decomposition

  1. displayInstructions() - Displays the welcomemessage and game instructions.
  2. getRandomColor() - Accepts a random object asa parameter and returns a single character representing the firstletter of the valid color set (R, G, B or Y). You can accomplishthis by generating a random number 1-4 and then having each numbercorrelate to a character representing a color.
  3. buildMasterCode() - Accepts a random object asa parameter and returns a String representing the random4-character mastercode selected by the computer. There is oneimportant caveat. You are not allowed to return a masterCode ofYYYY from this method. Should such a mastercode be generated, youmust re-generate a different mastercode until you have one that isnot equal to YYYY. This methods calls getRandomColor() multipletimes to assist in building the string.
  4. displayStats() - Accepts two int parametersrepresenting how many rounds were played as well as how many timesthe user won the round. Displays number of times the user the wonand lost as well as the user's winning percentage. For the winningpercentage, it should be displayed using a printf with two decimalplaces and accommodate printing up to a possible perfect winningpercentage of 100%.
  5. isValidColor() - Accepts a char parameter andreturns true if the character represents the first letter of avalid color set (R, G, B, Y) and false otherwise.
  6. isValidGuess() - Accepts a String parameterrepresenting the user's guess at the mastercode. Returns true ifthe user's guess is a valid guess of correct length with validvalues from the color set and false otherwise. Note that being avalid guess has nothing to do with the guess being correct. We arestrictly talking about validity with respect to the length andcolor choices. A valid guess is any string of four characters longcontaining valid color codes after all spaces have been removed (inother words, \"Y B R G\" is a valid guess once the spaces have beenremoved). This method will call isValidColor() to assist invalidating the user's guess.
  7. getValidGuess() -- Accepts two parameters, aScanner object for reading user input and an int value representingwhich number guess this is for the user (the user only gets 6guesses). This method reads the user guess and validates it(uppercase or lowercase is acceptable). If the guess is not a validguess, an error message is displayed and the user prompted againfor a valid guess. This method calls isValidGuess() to assist invalidating the user's input and removing whitespace. This methodreturns a validated String representing the user's guess to thecalling method.
  8. countCorrectColors() - Accepts two Stringparameters representing the mastercode and the guess. Returns anint value representing the number of colors guessed correctly bythe user as compared to the mastercode irrespective of whether ornot those colors are in the correct position.
  9. countCorrectPositions() - Accepts two Stringparameters representing the mastercode and the guess. Returns anint value representing the number of colors guessed correctly intheir correct position by the user as compared to themastercode.
  10. checkGuess() - Accepts two String parametersrepresenting the mastercode and the guess. Returns a boolean valueindicating whether or not the user won the round based upon theirguess. If the user did not win the round, the user should beinformed how many colors they guessed correctly and whether any ofthose were in the correct position. This method callscountCorrectColors() and countCorrectPositions() to assist indetermining what information to display to the user.
  11. playOneRound() - Accepts a Scanner object anda String representing the masterCode. Returns a boolean indicatingwhether or not the user won this round. Allows the user up to 6guesses before the user automatically loses the round. This methodcalls getValidGuess() and checkGuess() to assist in processing theuser's guess.
  12. getUserChoice() - Accepts a Scanner object andreturns a character representing a valid user's choice as towhether or not they would like to play another round. Valid choicesare Y or N, but naturally you should let the user enter this inlower or uppercase.
  13. main() - The main is the controlling method ormanager of the game, continuing to play a new round of masterminduntil the user decides to quit the game. Once the game has ended,the stats should be displayed.

Answer & Explanation Solved by verified expert
4.0 Ratings (435 Votes)
Here is the completed code for this problem Comments are included go through it learn how things work and let me know if you have any doubts or if you need anything to change If you are satisfied with the solution please rate the answer Thanks MasterMindjava import javautilRandom import javautilScanner import orgomgCORBAOMGVMCID public class MasterMind method to display the instructions static void displayInstructions SystemoutprintlnWELCOME TO MASTERMIND Systemoutprintln SystemoutprintlnHow to Play Systemout println1 I will pick a 4 character color code out of the following colors Yellow Blue Red Green Systemout println2 You try to guess the code using only the first letter of any color Example if you type YGBR that means you guess Yellow Green Blue Red Systemout println3 I will tell    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students