C SC 160 Project 3: Mastermind

20 points
Due: Tuesday November 4

This is an individual assignment

For this lab project, you are to complete the implementation of the MMGame class that maintains the internal state and accepts and evaluates guesses for the MasterMind game that we discussed in lab. I have provided the design of the class interface, specifying each of its public methods. I have also established all of the instance variables you will need. It is your task to implement each of the methods that is not fully implemented. The MMGame() constructor without parameters is implemented for you, as is each of the accessor  ("get") methods.

Getting Started

Two Java files are needed to make the project work:
  1. Mastermind.java, a JFrame subclass that contains the main()method and implements the GUI. Provided.
  2. MMGame.java, the class that implements the Mastermind game evaluation. Documentation and partial solution provided.
Update your public repository folder then open it to find the Project3 folder. Copy the Project3 folder into your own workspace.

Organize your work using a jGRASP project. Some of the instructions for doing this were provided in Project 2. Since Mastermind.java contains main(), it will be the one you designate as the the Project's Main File.

The project will compile and run as provided but will not produce meaningful results. Make sure you can compile and run it before proceeding.

The user's view of MasterMind (the test harness)

The GUI that I am providing for this lab is not an application that is intended for playing the game. Rather it is simply a test harness for you to use to develop and test your code. You should also make use of System.out.println for generating any further debugging or testing diagnostics you may need. The GUI is shown below:

Here's how the game is played:

Example

Click New Game, using the default n=4 and k=6.  The random string "5300" is generated but not shown . Implementation note: during development you will need to print it to System.out.

Type in your first guess, "1000" and click Make Guess.  The response should be complete=2, partial=0.  There are two 0's in correct positions (third and fourth) but the rest are not found.  Implementation note: notice that the first 0 is not counted as a partial even though it is a correct digit in the incorrect position!  This is because the third and fourth 0's in the guess will match exactly ("completely") those positions in the answer.  A given position in the answer cannot contribute to both complete and partial counts.

The following table represents a sequence of guesses and responses for this example, which was guessed after 6 tries.
 
#
answer
guess
complete
partial
1
5300
1000
2
0
2
5300
1035
0
3
3
5300
1500
2
1
4
5300
5200
3
0
5
5300
5400
3
0
6
5300
5300
4
0

Solution Strategy

Your solution development will proceed much more smoothly if you follow the strategy outlined here.
  1. Start by adding your name to the comments at the top of MMGame.java!
  2. Next implement the two-parameter constructor and randomString() together. The constructor needs to invoke randomString() to get the target string (print it out so you can see it). randomString() is a private method. I recommend that randomString() be implemented using a java.util.Random object and a loop. Each iteration will generate one randomly-generated digit of the string, using Random's int nextInt(int n) method to get an integer in the desired range (0 through k-1). You can append it to a string using the "+" operator (if the left operand is a String and the right operand is an int, the int will be concatenated as if it were a String).
  3. Implement guess() next. This does not evaluate the guess but determines whether or not the user's guess correctly follows the required structure: correct length and every character is a valid one. Don't forget to return true or false. Helpful hint: in Java, a char and int can work together. For instance, '3' == 51 evaluates to true, because 51 is the numeric Unicode value for that character. Similarly, '3' == '0'+3 evaluates to true, because the character '0' has Unicode value 48, and 48+3 is 51. So you can easily check to see if a character falls within a given range. The Unicode values for the characters '0' through '9' are 48 through 57.
  4. Implement numComplete() next. This compares the guess to the target character by character, counts the number of "complete" matches (matching value and position), and returns that count. This requires a loop but is pretty straightforward.
  5. Implement numPartial() last. It is by far the most logically complex of the methods and requires nested loops for its solution. Do not attempt this one until all the preceeding methods are tested and debugged!

Point Allocations

PointsItem
1constructor public MMGame(int n, int k)
4private String randomString()
4public boolean guess()
4public int numComplete()
3public int numPartial() works when each digit in solution is different, each digit in guess is different, and none in the guess is in correct position. E.g. solution="2301", guess="1253" yields result of 3
2public int numPartial() works when each digit in solution is different, and one or more in the guess are in correct position. E.g. solution="2301", guess="2253" yields result of 1
2public int numPartial() works for any other configuration of solution and guess (digits can be repeated in both solution and guess, and any number of guess digits are in the correct position)

To Turn In

When you are finished, email MMGame.java as an attachment to me, psanderson@otterbein.edu


[ CSC 160 schedule | C SC 160 | Peter Sanderson | Math Sciences server  | Math Sciences home page | Otterbein ]

Last updated:
Peter Sanderson (PSanderson@otterbein.edu)