(Updated Deceber 5, 2022)
At one point, you may have played Mastermind®. Your opponent picks 4 pegs of various colors (red, orange, yellow, white, blue, green) and places them behind a secret. You then have a predetermined number of guesses to figure it out. You may have 8 or 10 or some other number of guesses.
Following each guess, you are told how many are correct but in the wrong place (white peg) and how many are exact matches (red or black peg). However, you are not told which ones fall into their respective category!
As we move through the chapters of Java Programming by Design, we will have interludes, like this one, where we will pause and build more of this game as we discover the new facility we have developed from the reading and exercises. The game will be called CodeBreaker.
While the graphical representation below looks exciting, we are not prepared to build this yet and will not be for some time.
However, we can begin with a text-based solution with a limit of 10 guesses and feedback to the player as to how successful they are. (Later, we will build both Swing and JavaFX graphical solutions.)
The process will involve:
- Identifying data types.
- Performing string analysis.
- Converting between different data types.
- Employing loops for selecting the secret and guess matching.
- Designing the logic flow of game play.
- Employing user-defined methods for breaking down the problem into smaller, digestible pieces.
- Developing a fully functional text-based game.
- Designing a Swing and JavaFX GUI layout.
- Designing the logic for gameplay within Swing and JavaFX.
- Developing a fully functional Swing and JavaFX game.
The following link will allow you to play the current prototype for the text-based version.
[NOTE: Donald Knuth (q.v.) wrote a paper in 1976 whereby he proposed a programmatic solution, or algorithm, to the “codemaker” game.]A sample run of the game might look like the following (guesses are in bold):
The game of CodeBreaker: The computer will choose 4 colors from the list RED, ORANGE, YELLOW, BLUE, GREEN and WHITE. You will have 10 chances to guess the colors from left to right. You will use the first letter of each color. Your guess could be "RWYY" or "R W Y Y". Spaces are not an issue. If you make a mistake with color selection, you will be prompted to reenter your guess. Each guess is graded. You will be told which colors are correct and how many are in the correct place. Enter your guess: rrrr Guess 1: RRRR exact:0 correct:0 Enter your guess: bbbb Guess 2: BBBB exact:2 correct:0 Enter your guess: rrbb Guess 3: RRBB exact:1 correct:1 Enter your guess: rbbr Guess 4: RBBR exact:2 correct:0 Enter your guess: obbo Guess 5: OBBO exact:2 correct:0 Enter your guess: gbbg Guess 6: GBBG exact:3 correct:0 Enter your guess: wbbg You did it!
In this example, Knuth’s solution is applied to eliminate colors rapidly. You can see that the final guess was afforded a bit of luck.