Background: In the game of Mastermind, one player creates a code 4 colors long o
ID: 3681000 • Letter: B
Question
Background:
In the game of Mastermind, one player creates a code 4 colors long out of 6 possible colors (red, orange, yellow, green, blue, or white). The other player tries to guess the code in as few attempts as possible. For each incorrect guess, the player who created the code assigns red pegs and white pegs to the guess. A red peg means that the guess has a correct color in the correct spot, while a white peg means that the guess has a correct color in the incorrect spot. The total number of pegs for each guess cannot be more than 4 (the length of a code). Below are a few examples:
Code: RGWO
Guess: RYBG
1 red peg, 1 white peg
Code: RYWB
Guess: BWYR
0 red pegs, 4 white pegs
Code: OOBY
Guess: OGBY
3 red pegs, 0 white pegs
Code: RRRR
Guess: OYBG
0 red pegs, 0 white pegs
Problem
Write a program that allows you to play Mastermind against the computer. The computer should generate a random code (a String made up of the characters R, O, Y, G, B, and W, representing the possible colors) and you (the user) will guess it. For each guess that isn’t correct, the program should return the number of red pegs and the number of white pegs for the guess. You should be able to keep guessing until you win.
The required code for this has already been started for you. To complete this assignment, you need to download the Mastermind.java file (attached to Assignment 3 on Coursweb) and add the required methods. You should not have to modify the main method. Remember to document each of your methods appropriately.
Notes / Hints
Read the main method carefully to see how the methods work together, and then work through writing each method individually.
You are welcome to add additional methods that aren’t called by the main method (for example, you might want one of your methods to call another method you add).
This is a program where the user input matters a lot your code must do input validation to ensure a valid guess.
Make sure you test your program well! There are many different cases make sure you are handling them all. While testing, it might be helpful to output the computer’s code so that you can check your peg calculations.
What to Submit
The Mastermind.java file, with your additions. Please make sure there is a comment at the top with your name
// Name:
import java.util.Random;
import java.util.Scanner;
public class Mastermind {
/*
* Do not modify the main method.
*/
public static void main (String [] args) {
System.out.println("Welcome to Mastermind!");
System.out.println("Guess a four-letter string made up of R, B, Y, O, W, and G.");
// Get computer's code
String compCode = getRandomCode();
int guessCount = 0;
boolean won = false;
while(!won) {
// Get user's guess
String userGuess = getUserGuess();
guessCount++;
// Check to see if they won.
won = guessIsCorrect(userGuess, compCode);
if (won) {
break;
}
// Calculate & output number of red and white pins
int redPins = calculateRedPins(userGuess, compCode);
int whitePins = calculateWhitePins(userGuess, compCode, redPins);
System.out.println("Red pins: " + redPins + ", White Pins: " + whitePins);
}
System.out.println("You won! Number of guesses: " + guessCount);
}
/*
* TODO: Add the necessary methods below.
*/
}
Code: RGWO
Guess: RYBG
1 red peg, 1 white peg
Code: RYWB
Guess: BWYR
0 red pegs, 4 white pegs
Code: OOBY
Guess: OGBY
3 red pegs, 0 white pegs
Code: RRRR
Guess: OYBG
0 red pegs, 0 white pegs
Explanation / Answer
// MasterMind.java import java.awt.*; import java.util.*; import ch.aplu.jgamegrid.*; public class MasterMind extends GameGrid implements GGMouseListener { int[] secretCode = new int[4]; int currentRow; boolean roundFinished; final String version = "0.2"; ActiveRowMarker marker; private int placedPegs = 0; public MasterMind() { super(7, 10, 60, null, "sprites/mastermindbg.png", false); this.addMouseListener(this, GGMouse.lPress | GGMouse.rPress); this.setTitle("MasterMind - V " + version); getBg().setPaintColor(Color.red); reset(); show(); } public boolean mouseEvent(GGMouse mouse) { if (roundFinished) { reset(); return true; } Location loc = toLocation(mouse.getX(), mouse.getY()); if (placedPegs == 4 && loc.x == 1 && loc.y == currentRow) { // click on evalButton -> evaluate int[] guess = new int[4]; for (int i = 0; i < 4; i++) guess[i] = getOneActorAt(new Location(2 + i, currentRow)).getIdVisible(); evaluateGuess(guess); } if (loc.y == currentRow && loc.x > 1 && loc.x < 6) { if (getOneActorAt(loc) == null) { this.addActor(new Peg(), loc); placedPegs++; if (placedPegs == 4) { // show evaluate button addActor(new EvaluateButton(), new Location(1, currentRow)); } } else if (mouse.getEvent() == GGMouse.lPress) getOneActorAt(loc).showNextSprite(); //if leftclick -> next color else getOneActorAt(loc).showPreviousSprite(); //if rightClick -> previous color } refresh(); return true; } public void reset() { removeAllActors(); getBg().clear(); currentRow = this.nbVertCells - 2; roundFinished = false; for (int i = 0; i 0) { EvalPeg ep = new EvalPeg(0); addActor(ep, new Location(1, currentRow)); ep.turn(90 * i); blackPegs--; } else if (whitePegs > 0) { EvalPeg ep = new EvalPeg(1); addActor(ep, new Location(1, currentRow)); ep.turn(90 * i); whitePegs--; } } } private void showSolution() { int x = 2; for (int spriteNr : secretCode) { Peg peg = new Peg(); peg.show(spriteNr); addActor(peg, new Location(x, 1)); x++; } } private String printArray(int[] a) { String result = ""; for (int b : a) result += b + ", "; return result; } public static void main(String[] args) { new MasterMind(); } } // -----------class ActiveRowMarker ----------- class ActiveRowMarker extends Actor { public ActiveRowMarker () { super("sprites/activeRowMarker.png"); } } // ----------class EvalPeg ----------------- class EvalPeg extends Actor { public EvalPeg(int sprite) { // sprite 0 = black, sprite 1 = white super(true, "sprites/EvalPeg.png", 2); show(sprite); } } // -----------class EvaluateButton------------ class EvaluateButton extends Actor { public EvaluateButton() { super("sprites/EvaluateButton.png"); } } //-------- class Peg------------------------- class Peg extends Actor { public static final int NbColors = 6; public Peg() { super("sprites/peg.png", NbColors); } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.