Question: Write a Java program that will allow the user to play the Mastermind game. The game should randomly choose a pattern for the secret color

Write a Java program that will allow the user to play the Mastermind game. The game should randomly choose a pattern for the secret color code which is made of 4 color "pegs". The pegs available for making the code are blue, red, green, yellow, purple, and orange. A color can be used more than once in the secret code. The chosen secret code should be stored in a single-dimensional array and kept secret from the player until the end of the game.

The player will be allowed to guess the code up to 10 times. For each guess, the player will pick 4 colored pegs in the order they think they appear in the secret code.

The main board should be stored in a single 10 x 8 (rows x cols) two-dimensional array so that the board can be shown to the player after each guess. The rows in the 2D array represent a single guess from the user and the player feedback given by the game to the user about that guess. In other words, the first 4 columns of a row should contain a representation of the user's guessed pegs, and the last 4 columns of a row should contain a representation of the feedback the game gave to the player about their guess.

The player feedback is given after each guess by using white and black "pegs" with the following meanings:

- a white peg means a correct color was chosen but it was in the wrong position

- a black peg means a correct color was chosen and it was put in the correct position.

The white and black pegs are returned to the player in no particular order so as not to give away too much about the secret code.

For example, if the secret code is red-red-blue-blue and the player guesses red-blue-red-red, the game will award one black peg for the one correct red, one white peg for the correct red in the wrong positon, nothing for the third red as there is not a third red in the code, and a white peg for the blue since it is a correct color in the wrong position. No indication is given of the fact that the code also includes a second blue peg. The results to the player should also be shuffled into a random order.

The game ends when the player wins or has exhausted their 10 guesses. At the end of the game, the secret code should be revealed to the player along with whether they won or lost. The user should also be presented with an option to play again. If they choose not to play again, the program should exit.

Some sample output to start the game is shown below:

Welcome to Mastermind! The computer has selected a 4 color secret code made up of a combination of blue, red, green, yellow, purple, and orange pegs. Colored pegs may appear more than once in the secret code. Your job is to guess the secret code in 10 tries. You will be given feedback about each guess in no particular order. A white peg means a correct color was chosen but it was in the wrong position. A black peg means a correct color was chosen, and it was in the correct position.

Guess the code:

The game board presented to the player after their first guess would look like this:

R B R R | w w b

0 0 0 0 |

0 0 0 0 |

0 0 0 0 |

0 0 0 0 |

0 0 0 0 |

0 0 0 0 |

0 0 0 0 |

0 0 0 0 |

0 0 0 0 |

Some sample output at the end of the game:

Sorry! You lost! The secret code was: red red blue blue

Would you like to play again?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!