CS 0401 – Project 3: Mastermind

Due: Wednesday, March 16, 2016

Your third 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:

Example:

Welcome to Mastermind!

Would you like to play? yes

 

Enter guess number 1: rrrr

Colors in the correct place: 1

Colors correct but in wrong position: 0

 

Enter guess number 2:

 

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 so far. 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:

  1. A main class (named Mastermind) that serves as our view, creates the Model and Controller, and deals with user input and output.
  2. 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:

class MastermindModel {

 

     //private variable(s) to store the answer

 

     public MastermindModel() { /* Make the answer */ }

 

     public char getColorAt(int index) {

           /* Return color at position index as a char

            (first converted if stored as a number) */

     }

}

 

  1. A controller class named (MastermindController) that is defined as follows:

class MastermindController {

 

     public MastermindController(MastermindModel model) { … }

 

     public boolean isCorrect(String guess) { … }

 

     public int getRightColorRightPlace(String guess) { … }

 

     public int getRightColorWrongPlace(String guess) { … }

 

}

 

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

Submission

Create a Mastermind.zip file that contains your three .java files. Upload it to the submission website as we have been doing with labs. Make sure to do this prior to the deadline. No late work is accepted.

If, prior to the deadline, you realize that you need to submit a different version of your code, name it Mastermind-2.zip. (You can add new numbers as necessary, but try not to submit many times.)

We will grade the last submission that was prior to the deadline.