(Updated December 5, 2022)
Getting Started
Ok. Now we have some basic abilities at our disposal. We can now begin roughing out the game – at least from the viewpoint of the initial setup.
First, we will begin with a basic template for nearly all basic Java programs:
import java.util.Scanner;
public class CodeBreakerI2 {
private static Scanner kb = new Scanner(System.in);
public static void main(String[] args) {
// code goes here!
}
}
Iteration 1: Basic Java template for the new CodeBreaker game.
As you can see, we have named our class and setup for using the Scanner
class. Pretty basic stuff but essential.
What Needs To Be Managed?
At this point, we need some variables. We need a secret, the user’s input that represents their guess, and the colors from which we will choose four (4) random colors. These are shown below.
String secret="", input, colors="ROYBGW";
The choice to have the variable colors
represented as a String
was based on design. We know that each individual character is uniquely addressable using the charAt()
method. But, more importantly, we know that since each position is represented as a number we can eventually use Math.random()
to pick a random position in the String
! All we need to know is the length.
int r;
r = (int)(Math.random() * 6);
If we had a variable r
that held the random number, then we could use that value as an argument to charAt()
. Recall that the basic use of Math.random()
producing an int
, as shown above, will yield a number in the range 0-5. This coincides with the indexes of colors
.
0 1 2 3 4 5 ------------------------- colors --> | R | O | Y | B | G | W | -------------------------
All that is left to do now is to get the character and add it to secret
.
int r;
char c;
String secret="", input, colors="ROYBGW";
r = (int)(Math.random() * 6);
c = colors.charAt(r);
secret = secret + colors.charAt(r);
The code first picks the random number and store it in r
. Then, using r
, it selects a character c
from colors
. This is more steps than are actually necessary, but this code helps to illustrate the work necessary to go from random numbers to a randomly selected color.
Now, that being done, we have only selected one color. We need three more, but we do not know how to write loops yet. So, we have to do it the tedious way of copy-paste!
r = (int)(Math.random() * 6);
c = colors.charAt(r);
secret = secret + c;
r = (int)(Math.random() * 6);
c = colors.charAt(r);
secret = secret + c;
r = (int)(Math.random() * 6);
c = colors.charAt(r);
secret = secret + c;
r = (int)(Math.random() * 6);
c = colors.charAt(r);
secret = secret + c;
Finally, we consider simplifying the code and eliminate the use of r
and c
since they seem to clutter the code. Reducing the work down, we can perform simple substitution for r
and c
such that
r = (int)(Math.random() * 6);
c = colors.charAt(r);
secret = secret + c;
becomes
c = colors.charAt((int)(Math.random() * 6));
secret = secret + c;
and finally
secret = secret + colors.charAt((int)(Math.random() * 6));
Repeating the statements until we have four randomly selected colors would then look like the following:
// select 4 random colors for the secret.
secret = secret + colors.charAt((int)(Math.random() * 6));
secret = secret + colors.charAt((int)(Math.random() * 6));
secret = secret + colors.charAt((int)(Math.random() * 6));
secret = secret + colors.charAt((int)(Math.random() * 6));
Instructions Anyone?
At this point we can consider the importance of displaying some instructions to the user. If they have never played before, this is a good time to inform them of game play. We also need to instruct them on how to make guesses in such a way that we can interpret them later when we have more tools at our disposal.
// display instructions
System.out.println("\n"+
"The game of CodeBreaker:\n\n"+
"The computer will choose 4 colors from the list RED, ORANGE, YELLOW,\n"+
"BLUE, GREEN and WHITE. You will have 10 chances to guess the colors\n"+
"from left to right. You will use the first letter of each color. Your\n"+
"guess could be \"RWYY\" or \"R W Y Y\". Spaces are not an issue. If\n"+
"you make a mistake with color selection, you will be prompted to\n"+
"reenter your guess.\n\n"+
"Each guess is graded. You will be told which colors are correct and\n"+
"how many are in the correct place.\n");
Make a Guess!
Then, all that is left to do is to set the stage for reading a guess from the user. Since we are still lacking in some tools, this will be a simple one-guess model without any checking to see if they guessed correctly. This is achieved by prompting and reading the String
. We have also chosen to upcase the input String
and remove all spaces with the replace()
method.
System.out.println("Enter your guess:");
input = kb.nextLine().toUpperCase().replace(" ", "");
System.out.println("The secret was " + secret);
Recall that since nextLine()
returns a String
object, we are able to call the toUpperCase()
method on the returned object which also returns a new String
object. We are further able to call the replace()
method on the String
object returned by toUpperCase()
. After all those calls are completed, input
contains a reasonably normalized and slightly sanitized String
for us to check against secret
.
Putting It All Together
The final version of CodeBreaker for Interlude 2 is shown below.
import java.util.Scanner;
public class CodeBreakerI2 {
private static Scanner kb = new Scanner(System.in);
public static void main(String[] args) {
String secret="", input, colors="ROYBGW";
// display instructions
System.out.println("\n"+
"The game of CodeBreaker:\n\n"+
"The computer will choose 4 colors from the list RED, ORANGE, YELLOW,\n"+
"BLUE, GREEN and WHITE. You will have 10 chances to guess the colors\n"+
"from left to right. You will use the first letter of each color. Your\n"+
"guess could be \"RWYY\" or \"R W Y Y\". Spaces are not an issue. If\n"+
"you make a mistake with color selection, you will be prompted to\n"+
"reenter your guess.\n\n"+
"Each guess is graded. You will be told which colors are correct and\n"+
"how many are in the correct place.\n");
// select 4 random colors for the secret.
secret = secret + colors.charAt((int)(Math.random() * colors.length()));
secret = secret + colors.charAt((int)(Math.random() * colors.length()));
secret = secret + colors.charAt((int)(Math.random() * colors.length()));
secret = secret + colors.charAt((int)(Math.random() * colors.length()));
System.out.println("Enter your guess:");
input = kb.nextLine().toUpperCase().replace(" ", "");
System.out.println("The secret was " + secret);
}
}
Iteration 2: Final version for this interlude.