| peg 1 | peg 2 | peg 3 | peg 4 | Hint 1 | Hint 2 | |
| Code 1 | red | blue | red | white | ||
| Guess 1 | orange | red | green | yellow | 0 | 1 |
| Guess 2 | red | red | yellow | red | 1 | 2 |
| Guess 3 | blue | red | red | orange | 1 | 3 |
Our goal is to design and implement a MasterMind class that plays the role of the code maker and allows the user to play the role of the code guesser. A skeleton source code file is located in the folder G:\Lintont\COSC135b\Pickup\MasterMind\ (copy the entire folder to your H:\ drive), or the source code is located here.
If you define constants, you should use them wherever appropriate, thus, if you need to check if a variable is holding the code for the red color, rather than asking peg1 == 0; you should ask peg1 == RED; Constants (like ours) are allowed as the cases in a switch statement. Once you've entered each of the constants, make the code fragment below the body of your buttonClicked method.results.setText(" " + RED + WHITE + BLUE + GREEN + YELLOW + ORANGE);
Compile and run the project. Click the button and record what is printed in the TextArea below:
For this, we could use a method that translates our integers 0 to 5 (or the constants RED, ORANGE, etc.), to Strings for our colors, "red", "orange", "yellow", etc. Our method will input an integer and output a String. If the input is not an integer from 0 to 5, the String "error" should be returned, otherwise, the String corresponding to the color associated with the input should be returned. Thus, if 0 is the input, "red" is the output, and if 3 is the input, "green" is the output. Add a method named colorWord, that performs this conversion (int to String). Use a switch block in your method. You can handle the problem of input being out of range (not from 0 to 5) in the default case. You need NOT display an error message here, just return the String "error". Once you have written your method, replace the body of the buttonClicked method with the code below.results.setText("Testing colorWord method:\n" +Run the project and record the output displayed below:
Format.justify('c', "input", 8) +
Format.justify('c', "output", 8) + "\n");
for (int k = 6; k >= -1; k--) {
results.append(Format.justify('c', k, 8) +
Format.justify('c', colorWord(k),8) + "\n");
}
| Your switch block could look like this:
switch(n) { case 1: case 2: case 6: statement1; statement2; break; case 4: case 5: case 3: other statement; break; } |
Or even better, like this:
case 1: case 2: case 6: statement1; statement2; break; case 4: case 5: case 3: other statement; break; } |
Write a buttonClicked method that tests your makeSecretCode()
method 6 times. On the first and fourth test, simply append the number
codes to the TextArea named results (as in "0, 4, 0, 2."
if your code is red, blue, red yellow). On the second and sixth tests,
print a nicely formatted message using words for the colors, like "the
code is red, blue, red, yellow." (periods, commas and all). On the third
and fifth tests, append just the words corresponding to the colors in the
secret code (as in "red, blue, red, yellow."). Use a for loop and
a switch block for this. You can take advantage of both features
of the switch block mentioned above (doing more and more as well
as lumping cases together). Copy your buttonClicked method body
below:
Calculating the value of hint 1 is relatively easy. That is, given the 4 pegs of a guess and the four pegs of a secret code, it is straightforward to calculate the number of pegs in the guess that are in the right location and the correct color (or completely correct). Call the guessed pegs guess1, guess2, guess3 and guess4, and assume these are inputs to a method named hint1. The body of hint1 can check if guess1 == code1, and if so increment some variable. Then do the same for the second, third and fourth pegs. Finally, the method should return the value of this variable (namely the number of pegs in the guess that are completely correct). Write the hint1 method. Copy your hint1 method below (all of it, not just the body).