Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that will allow the user to play the Mastermind game. The g

ID: 3753674 • Letter: W

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 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?

Explanation / Answer

ScreenShot

-----------------------------------------

Program

//Packages for random generation
import java.util.Random;
import java.util.Scanner;
public class MasterMind {
   //Method to generate random word
public static String SecretCode(){
   //Random generator
   Random r = new Random();
//Random generation string
        String alphabet = "BRGYPO";
        String sc="";
        //Return secret code
        for (int i = 0; i < 4; i++) {
           sc+=(alphabet.charAt(r.nextInt(alphabet.length())));
        }
        return sc;
}
//Display of user guess
public static void displayArray(char arr[][]) {
   for(int i=0;i<10;i++) {
       for(int j=0;j<8;j++) {
           System.out.print(arr[i][j]+" ");
       }
       System.out.print(" ");
   }
}
//Main method
   public static void main(String[] args) {
       //Read input object
       Scanner sc=new Scanner(System.in);
       //Variable for user guess
       String guess="";
       //Generate secret code
       String secretcode=SecretCode();
       //System.out.println(secretcode);
       //User guess store array
       char userBoard[][]=new char[10][8];
       //Initialize 0
       for(int i=0;i<10;i++) {
           for(int j=0;j<8;j++) {
               userBoard[i][j]='0';
           }
       }
       //Prompt for user guess
       for(int i=0;i<10;i++) {
           System.out.print("Enter your Guess: ");
           guess=sc.next();
           //If guess an dsecret code same you won
           if(guess.equals(secretcode)) {
               System.out.println("CONGRATS YOU WON!!!!");
               System.exit(0);
           }
           //Otherwise
           else {
               int l=0;
               for(l=0;l<4;l++) {
                   userBoard[i][l]=guess.charAt(l);
               }
               for(int j=0;j<4;j++) {
                   for(int k=0;k<4;k++) {
                       if(guess.charAt(j)==secretcode.charAt(k)) {
                           if(j==k) {
                               userBoard[i][l]='B';
                               break;
                           }
                           else {
                               userBoard[i][l]='W';
                               break;
                           }
                          
                       }
                       else {
                           userBoard[i][l]=' ';
                       }
                   }
                   l++;
               }
              
          
          
               displayArray(userBoard);
           }
       }
       String S="";
       for(int i=0;i<4;i++) {
           if(secretcode.charAt(i)=='B') {
               S+="blue"+" ";
           }
           else if(secretcode.charAt(i)=='R') {
               S+="red"+" ";
           }
           else if(secretcode.charAt(i)=='O') {
               S+="orange"+" ";
           }
           else if(secretcode.charAt(i)=='P') {
               S+="purple"+" ";
           }
           else if(secretcode.charAt(i)=='G') {
               S+="green"+" ";
           }
           else if(secretcode.charAt(i)=='Y') {
               S+="yellow"+" ";
           }
       }
      
       System.out.print("Sorry! You lost! The secret code was:"+S);
      

   }

}

---------------------------------

Output

Enter your Guess: YPRG
Y P R G W B   W
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 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
Enter your Guess: BRBR
Y P R G W B   W
B R B R       
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 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
Enter your Guess: BPRB
Y P R G W B   W
B R B R       
B P R B   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 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Enter your Guess: BYPO
Y P R G W B   W
B R B R       
B P R B   B   
B Y P O   W W
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 0 0 0 0
0 0 0 0 0 0 0 0
Enter your Guess: OYOP
Y P R G W B   W
B R B R       
B P R B   B   
B Y P O   W W
O Y O P   W   W
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 0 0 0 0
Enter your Guess: PRBR
Y P R G W B   W
B R B R       
B P R B   B   
B Y P O   W W
O Y O P   W   W
P R B R W     
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
Enter your Guess: PPYY
Y P R G W B   W
B R B R       
B P R B   B   
B Y P O   W W
O Y O P   W   W
P R B R W     
P P Y Y W B 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
Enter your Guess: YYRB
Y P R G W B   W
B R B R       
B P R B   B   
B Y P O   W W
O Y O P   W   W
P R B R W     
P P Y Y W B W B
Y Y R B W W   
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Enter your Guess: BGRG
Y P R G W B   W
B R B R       
B P R B   B   
B Y P O   W W
O Y O P   W   W
P R B R W     
P P Y Y W B W B
Y Y R B W W   
B G R G   W   W
0 0 0 0 0 0 0 0
Enter your Guess: GRBG
Y P R G W B   W
B R B R       
B P R B   B   
B Y P O   W W
O Y O P   W   W
P R B R W     
P P Y Y W B W B
Y Y R B W W   
B G R G   W   W
G R B G B     W
Sorry! You lost! The secret code was:green purple green yellow

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote