Master Mind
Master Mind is a board game for two players. A description of the game can be found on this Wikipedia article.
In the original version of the game:
- Player 1 picks a secret pattern of four colored pegs. The pattern is a sequence, the order of the pegs matters. Multiple pegs can have the same color. There are six possible colors.
- Player 2 provides a guess for the secret pattern.
- Player 1 provides feedback, that gives information to Player 2 about the correct pattern. The feedback consists of black pegs and white pegs, as follows:
- A black peg is provided for every peg in the guess that appears at the exact same location in the secret pattern.
- A white peg for every peg in the guess that appears in the secret pattern, but not in the same location as in the guess.
- Each peg in the guess CANNOT generate more than one peg in the feedback. A single peg in the guess can only influence the feedback by generating a single black peg, or a single white peg (or it may also not generate any pegs at all). For example, suppose that the peg at position 2 of the guess is green, and suppose that the pegs in positions 2 and 3 of the secret pattern are green. Then, the peg at position 2 of the guess generates a single black peg in the feedback (black pegs are given priority over white pegs).
- Similarly, each peg in the secret pattern CANNOT generate more than one peg in the feedback. Again, black pegs are given priority over white pegs.
- If the guess was correct (indicated by four black pegs), then the game is over. The goal of Player 2 is to make a correct guess using as few guesses as possible.
- If the guess was incorrect, then Player 2 gets to continue guessing, and receiving feedback for each guess, until Player 2 finds the correct pattern (or, until a certain limit of guesses, like 10 guesses, has been reached).
More details on the game can be found on the Wikipedia article. You can play the game on the web on the Archimedes Lab page.
Your task is to implement a program that can play Master Mind with a human. You should start with a program that can do the following:
- Pick a pattern at random. Since you have only learned to write text-based programs, the pattern can use numbers instead of colors. The pattern should contain four numerical digits, each digits between 1 and 6. Repetitions are allowed.
- Ask the user to guess the pattern.
- Provide the CORRECT feedback to the user. Use letter 'o' instead of black pegs, and letter 'x' instead of white pegs.
Most people who have tried this project have had trouble in producing the correct feedback, and particularly in making sure that each digit (in the guess or in the correct pattern) generates one and only one character on the feedback string. Getting the number of x's right seems to be the most error-prone task.
The second stage of this project would be to extend your program, so that:
- The human picks the pattern (obviously, you don't need to write any code for that).
- The computer guesses the pattern and prints out the guess.
- The human provides (hopefully correct) feedback to the computer for each of the computer's guesses.
This is a more creative task. You need to come up with a strategy that helps the computer use the provided feedback, so as to find the correct pattern in ten or fewer guesses (or at least in a smarter way than by just making an uninformed random guess each time). If you search on the web, you will find algorithms that guarantee finding the correct pattern in at most five guesses. Still, I would suggest trying to come up with your own strategies and seeing how well they work.
A possible third stage of the project would be to convert it from text-based to a graphical user interface (GUI). See the discussion on graphical interfaces on this website.