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:
- Mastermind.java, a JFrame subclass that contains the main()method and
implements the GUI. Provided.
- 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:
-
Specify the value of "n", which is the the length
of the string, and "k", which is the number of different digits (0 through
k-1) which may appear in the string. In the example shown above,
default values are given: n is 4 and k is 6. An example string would
thus be "3205".
- When you click New Game, a random string of digits is created. You
cannot see it but are invited to guess what it is.
-
To make a guess, enter your guess in the box provided and click Make Guess.
Your guess must conform to the restrictions imposed by "n" and "k".
-
After you make a guess, you get feedback in the two boxes "complete" and
"partial". Each box will contain a number in the range 0 to "n".
The number in "complete" is a count of how many digits in your guess were
correct and in the correct position. The number in "partial" is a
count of how many digits in your guess were correct but in the wrong position.
These are your clues to use in forming the next guess. When the value
in "complete" is the same as "n and "partial" is 0, you have correctly
guessed the answer!
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.
- Start by adding your name to the comments at the top of MMGame.java!
- 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).
- 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.
- 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.
- 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
Points | Item |
1 | constructor public MMGame(int n, int k) |
4 | private String randomString() |
4 | public boolean guess() |
4 | public int numComplete() |
3 | public 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 |
2 | public 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 |
2 | public 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)