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

Write a program (Mastermind.java) that plays a variation of the game of Mastermi

ID: 3656676 • Letter: W

Question

Write a program (Mastermind.java) that plays a variation of the game of Mastermind with a user. The program uses pseudorandom numbers to generate a four-digit number. The user should be allowed to make guesses until s/he gets the number correct. Clues should be given to the user indicating how many digits of the guess are correct and in the correct place, and how many digits are correct but in a wrong place.

Example:
(9935 is the generated random number)

Please enter a four-digit number: 9874
The number of correct digits but in the wrong place: 0 The number of correct digits in the right place: 1 Please enter a four-digit number: 9899
The number of correct digits but in the wrong place: 1 The number of correct digits in the right place: 1 Please enter a four-digit number: 9593
The number of correct digits but in the wrong place: 3 The number of correct digits in the right place: 1 Please enter a four-digit number: 9935
The number of correct digits but in the wrong place: 0 The number of correct digits in the right place: 4
You are correct!

Explanation / Answer

public class MasterMind { //input from the keyboard private Scanner keyboard; private String input; //user decides which mode to use, 4 holes and 6 colors, or 5 holes and 7 colors. private static int mode; private final int STANDARD_MODE = 6; private final int CHALLENGE_MODE = 7; //two-dimensional array stores all guesses and key pegs private char[][] guess; private char[][] key; //char arry stores the answer generated by the computer private char[] answer; //class object MMAnswer generates the answer for each game private MMAnswer answerGenerator; //boolean value indicates the play wins the game private boolean winner; //class object MMGuess compares the guesses to the answer //and provides scores of each game including hits and number of guesses private MMGuess guessEvaluator; private int totalHits; private int totalGuesses; private int guessCount; private int hintsUsed = 0; // The amount of times someuse used a hint /** * MasterMind constructor with mode setting * @param mode the integer indicates the number of colors of the game, * 6: 4 holes and 6 colors; 7: 5 holes and 7 colors */ public MasterMind(int mode) { MasterMind.mode = mode; keyboard = new Scanner(System.in); winner = false; totalHits = 0; answerGenerator = new MMAnswer(STANDARD_MODE); answer = answerGenerator.getAnswer(); totalGuesses = 0; guessCount = 0; guess = new char[30][mode - 2]; key = new char[30][mode - 2]; }//MasterMind constructor with mode setting /** * MasterMind constructor enables the keyboard input */ public MasterMind() { keyboard = new Scanner(System.in); }//MasterMind constructor public static void main(String[] args) throws IOException { //make sure the input is in correct format boolean successfulStart = false; MasterMind MMGame; int mode = 0; //a loop generates multiple games as long as the play would like to continue do { successfulStart = false; //a minimal initialized game enables the keyboard input MMGame = new MasterMind(); //ask user to play or quit the game successfulStart = MMGame.startNewGame(); //check the answer from the user and starts the game if (successfulStart) { //ask the user for the difficulty level of the game, 2 modes MMGame.setMode(); //generate a new game with the user-defined mode setting mode = MMGame.getMode(); MMGame = new MasterMind(mode); //generate answer based on the user-defined mode MMGame.answerGenerator = new MMAnswer(mode); MMGame.answer = MMGame.answerGenerator.getAnswer(); //a loop asks the user to input all the guesses, each loop generates one guess //displays the key pegs, scores and total number of guesses at the end of each guess do { //ask the user to input one guess contains desired number of holes, 4 or 5, based on the user-defined mode //store each guess into the two-dimensional array MMGame.guess[MMGame.totalGuesses] = MMGame.getGuess(); //compares the guess to the answer MMGame.guessEvaluator = new MMGuess(MMGame.guess[MMGame.totalGuesses], MMGame.answer); //check whether the user wins the game MMGame.winner = MMGame.guessEvaluator.evaluateGuess(); //store each key into the two-dimensional arry for each guess MMGame.key[MMGame.totalGuesses] = MMGame.guessEvaluator.getKey(); //calculate the total score from scores of individual guesses //Perfect match earns 2 points, close match earns 1 point MMGame.totalHits += MMGame.guessEvaluator.getSingleHits(); // Compute the total number of guesses MMGame.guessCount++; //display the key pegs for each guess System.out.print(" Your hits: "); for(int i = MMGame.key[MMGame.totalGuesses].length - 1; i >= 0; i--) System.out.print(MMGame.key[MMGame.totalGuesses][i] + " "); /*System.out.println(" X = right color and right position, 2 points " + "x = right color but wrong position, 1 point " + "0, color not exist"); System.out.println("--------------------------------------------------------------------"); //display the total points from scores of individual guesses System.out.print("Total points earned: " + MMGame.totalHits + " "); //calculate and display the number of guesses that the user made MMGame.totalGuesses ++; System.out.println("Total number of guesses: " + MMGame.totalGuesses); System.out.println("********************************************************************");*/ } //check whether the user wins the game //if not, ask the user whether continue the game or start over or need a hint before next guess while (!MMGame.winner && MMGame.continueCurrentGame()); //the current game ends if the user wins the game if (MMGame.winner) { // Show Contratulations and single game stats System.out.println("Congratulations! You win!"); System.out.println("********************************************************************"); System.out.println("It took you " + MMGame.guessCount + " guesses."); System.out.println("You Averaged " + MMGame.totalHits / ((double) MMGame.guessCount) + " Points Per Guess."); // Save the game data // class object MMStatistics generates the Statistics Controller for each game MMStatistics statsController = new MMStatistics(); statsController.setMode(mode); statsController.saveStats(MMGame.guessCount); } } //game over if the user is not ready for the game else { break; } } //a new game will start if the user is ready for a new game while (successfulStart); }//main /** * Asks the user whether the user wants to start a new game * System exits if the user doesn't want to play * @return successfulStart *
user's answer for want to play? */ public boolean startNewGame() throws IOException { //keeps track of valid input boolean successfulStart = false; //a loop prompts the user with the same question if the input is not valid do { System.out.println("Ready to start a new game? y = yes, n = no, s = show stats"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("y") || input.equalsIgnoreCase("yes")) { successfulStart = true; return true; } else if (input.equalsIgnoreCase("n") || input.equalsIgnoreCase("no")) { System.out.println("Thank you for playing! Bye!"); successfulStart = true; return false; } else if (input.equalsIgnoreCase("s") || input.equalsIgnoreCase("show stats")) { System.out.println("*****************************************************"); System.out.println("MasterMind Statistics"); System.out.println("***************************"); System.out.println("Standard Mode"); MMStatistics stats = new MMStatistics(); stats.setMode(STANDARD_MODE); stats.displayStats(); System.out.println("***************************"); System.out.println("Advanced Mode"); stats.setMode(CHALLENGE_MODE); stats.displayStats(); System.out.println("*****************************************************"); successfulStart = false; } else { System.out.println("Error in reading your input."); successfulStart = false; } } //keep looping if the input is not valid while(!successfulStart); return successfulStart; }//startNewGame() /** * Sets the mode of the game based on user's input * @return successfulSetMode *
valid input of the user's answer to which difficulty level want to play? */ public boolean setMode() { //keeps track of valid input boolean successfulSetMode = false; //a loop prompts the user with the same question if the input is not valid do { System.out.println("Please choose: s = standard mode ( use 6 colors and 4 holes)"); System.out.println(" c = challenge mode( use 7 colors and 5 holes)"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("s")) { mode = STANDARD_MODE; successfulSetMode = true; return true; } else if (input.equalsIgnoreCase("c")) { mode = CHALLENGE_MODE; successfulSetMode = true; return true; } else { System.out.println("Error in reading your input."); successfulSetMode = false; } } //keep looping if the input is not valid while (!successfulSetMode); return successfulSetMode; }//setMode() /** * Gets the user-defined game mode * @return mode *
the user-defined game mode */ public int getMode() { return mode; }//getMode() /** * loops to input 4 or 5 colors for each guess * @return guess[] *
the guessed colors defined by the user */ public char[] getGuess() { //user will enter one color at a time int colorPosition = 1; //keep track of the valid input for each color boolean successfulGetGuess = false; //a loop keeps asking the user to enter the correct number of colors for a guess do { //4 colors need for a guess in the standard mode if (mode == STANDARD_MODE) { guess[totalGuesses] = new char[4]; System.out.println("Please choose four colors from:"); System.out.println("r = red o = orange y = yellow g = green b = blue i = indigo"); successfulGetGuess = getColorInput(colorPosition); for(colorPosition = 2; colorPosition
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