(Updated June 12, 2022)
[Note: Mastermind is a registered trademark ® of Invicta Toys and Games Ltd. and has been distributed by Pressman Toys, Hasbro, and Parker Brothers.]
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 blind. 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 C 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 a graphical solution.)
The process will involve:
- Identifying data types.
- Performing string analysis.
- Converting between different data types.
- Employing loops for selecting the blind 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 GUI layout.
- Designing the logic for gameplay within the GUI.
- Developing a fully functional GUI 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". Spaces are not allowed in the guess. If you make a mistake with color selection, you will be prompted to reenter your guess. Each guess is graded. You will be told how many colors are correct and how many are in the correct place. Enter your guess: bbbb Guess 1: BBBB exact:0 correct:0 Enter your guess: gggg Guess 1: BBBB exact:0 correct:0 Guess 2: GGGG exact:0 correct:0 Enter your guess: rrrr Guess 1: BBBB exact:0 correct:0 Guess 2: GGGG exact:0 correct:0 Guess 3: RRRR exact:0 correct:0 Enter your guess: yyyy Guess 1: BBBB exact:0 correct:0 Guess 2: GGGG exact:0 correct:0 Guess 3: RRRR exact:0 correct:0 Guess 4: YYYY exact:2 correct:0 Enter your guess: oooo Guess 1: BBBB exact:0 correct:0 Guess 2: GGGG exact:0 correct:0 Guess 3: RRRR exact:0 correct:0 Guess 4: YYYY exact:2 correct:0 Guess 5: OOOO exact:2 correct:0 Enter your guess: yyoo Guess 1: BBBB exact:0 correct:0 Guess 2: GGGG exact:0 correct:0 Guess 3: RRRR exact:0 correct:0 Guess 4: YYYY exact:2 correct:0 Guess 5: OOOO exact:2 correct:0 Guess 6: YYOO exact:0 correct:4 Enter your guess: yoyo Guess 1: BBBB exact:0 correct:0 Guess 2: GGGG exact:0 correct:0 Guess 3: RRRR exact:0 correct:0 Guess 4: YYYY exact:2 correct:0 Guess 5: OOOO exact:2 correct:0 Guess 6: YYOO exact:0 correct:4 Guess 7: YOYO exact:2 correct:2 Enter your guess: oyoy Guess 1: BBBB exact:0 correct:0 Guess 2: GGGG exact:0 correct:0 Guess 3: RRRR exact:0 correct:0 Guess 4: YYYY exact:2 correct:0 Guess 5: OOOO exact:2 correct:0 Guess 6: YYOO exact:0 correct:4 Guess 7: YOYO exact:2 correct:2 Guess 8: OYOY exact:2 correct:2 Enter your guess: yooy Guess 1: BBBB exact:0 correct:0 Guess 2: GGGG exact:0 correct:0 Guess 3: RRRR exact:0 correct:0 Guess 4: YYYY exact:2 correct:0 Guess 5: OOOO exact:2 correct:0 Guess 6: YYOO exact:0 correct:4 Guess 7: YOYO exact:2 correct:2 Guess 8: OYOY exact:2 correct:2 Guess 9: YOOY exact:2 correct:2 Enter your guess: ooyy 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.