COMP 1600 Project 4: Master Mind

30 points
Deadline: end of Lab April 22

You may complete this assignment either individually or with one partner

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 I have described in class. I have provided the design of the class specification, 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 are each of the accessor  ("getter") methods.

Getting Started

Several files are needed to make the project work:
  1. MMTest.java, a test driver to automatically test the methods you are implementing. You will run this program to test your solution. Provided.
  2. NumPartialTest.class, a test driver used by MMTest to test the numPartial() method. Provided.
  3. MMGame.java, the class that implements the MasterMind game evaluation component. MMGame API and partial solution provided.

In the I:\COMP\1600\20142-COMP-1600-01\Common folder, you will find a folder called Project4. Copy this into your workspace. If you cannot access the I: drive, then download the project zip file (right-click) and unzip.

The project will compile and run as provided but will show test failures. Make sure you can compile and run it before proceeding. Note that main() is in MMTest.java

Solution Strategy

Your solution development will proceed much more smoothly if you follow the strategy outlined here.
  1. Start by adding your name(s) to the comments at the top of MMGame.java! If you are working with a partner, please implement and submit only one solution.
  2. Next implement the two-parameter constructor. As provided, it will assign a randomly-generated solution -- see the next step for details. Your task here is to initialize the remaining instance variables.
  3. Implement randomString() next. It is a private method that will generate a random MasterMind solution. I recommend that randomString() be implemented using a java.util.Random object and a loop. Each iteration of the loop 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's value will be concatenated as if it were a String).
  4. 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.
  5. Implement numComplete() next. This compares the guess to the target character by character, counts the number of "complete" matches (correct value and position), and returns that count. This requires a loop but is pretty straightforward.
  6. 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! Helpful hint: First look for complete matches, replacing each matched character with an 'x' or similar. This will prevent it from being considered for a partial match. An guessed character can contribute to a complete or partial match but not both.

Some Examples of numComplete and numPartial results

Suppose the solution is 5300 and your guess is 1000. 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 guess 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

The Partner Option

You have the option of working with a partner to develop one solution. If you do this, you will follow the "pairs programming" technique. The first decision will be whose login account to use since you can only use one. Once that is decided, then pairs programming is pretty straightforward: both of you work at one PC (you can use a second one for Googling and API lookups). The "driver" is at the keyboard typing in code and testing. The "navigator" is at the side, directing the driver, giving advice, and watching for keystroke errors. After 45 minutes you must switch roles. We don't have easy access to folders shared by both members of the partnership, so you should work out a way to make sure both of you have up-to-date code at the end of each working session.

To Turn In

When finished, copy MMGame.java into your I:\COMP\1600\20142-COMP-1600-01\DropBox folder. If you are working with a partner, submit to only one partner's folder. Be sure the comments in the course code file has both names.

Point Allocations

PointsItem
4 2-parameter constructor
6 randomString
6 guess
6 numComplete
8 numPartial

[ COMP 1600 | Peter Sanderson | Math Sciences home page | Otterbein ]

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