# Mastermind
Your first project is to implement the game of Mastermind. In this game, the computer chooses 4 pegs each with one of 6 colors. The player’s job is then to guess the colors that the computer has chosen in the proper order. After each guess by the player, if the player’s guess is not correct, the computer will give two numbers as feedback. The first number is how many pegs are the proper color and in the proper position. The second number is how many pegs are the proper color, but not in the correct position.
The game ends when the color string is correct – and the player wins – or they give 10 incorrect guesses – and they lose.
What you need to do:
- Generate a random computer guess of four colors out of:
- Red, Orange, Yellow, Green, Blue, Purple
- Read a guess from the user as a string of colors
- Score the guess, and report the two values back to the user
- Allow the user to continue to guess until they get it correct, or reach 10 turns and they lose.
- Allow the user to play the game multiple times
Example:
1 | Welcome to Mastermind! |
Model/View/Controller
A common pattern for writing programs is known as MVC (Model/View/Controller). This pattern applies whenever we have some problem we can model (like a game board) and a user interface that displays and interacts with that model (the view). The controller is the code that manipulates the model in response to actions from the view.
The idea of MVC is that each part of the program is sufficiently abstracted from each other that they can change without needing to modify the other parts. For instance, our view is currently a textual interface, but later in the course, we could alter this to be a graphical user interface. If we did that, ideally we would not need to change the model or the controller, only the view code.
For us, the implementation of the model is very simple: either an array or String of colors (ints, chars, etc.) that represents the randomly-chosen colors we are trying to guess.
The view is a simple text-based program as we’ve written many times before. It will prompt the user for their guesses and display if the guess is correct, or show the two statistics that we must calculate.
The controller links these two things together. We will then make three classes:
- A main class (named Mastermind) that serves as our view, creates the Model and Controller, and deals with user input and output.
- A model class (named MastermindModel) that stores the representation of the computer’s guess and uses a constructor and accessors to create and query the solution the player is trying to guess, defined as follows:
1 | class MastermindModel { |
- A controller class named (MastermindController) that is defined as follows:
1 | class MastermindController { |
You are to provide the implementation of all three classes, but you must define your controller and model using at least the methods above. Any additional methods or fields you want to add must be private to your classes.
Hints and Notes
- For right color, wrong place, you will need to not count colors from the guess that are the right color in the right place.
- You also need to avoid double counting a color as being in the wrong position
- To help you solve these issues, you may find it necessary to make some auxiliary arrays that keep track of what you have used already
You may want to play the game on paper where you consider various guesses and solutions and score them to see the issue.
The perfect player can always win this game in 7 guesses or less.
Submission
Make sure to periodically commit and push your changes to github.
We will grade the last commit that was pushed prior to the deadline.