CS 314 Specification 4 - Designing and Implementing a Program With Multiple Classes - MasterMind

Programming Assignment 4: Pair Assignment. You may work with one other person on this assignment using the pair programming technique. Your partner can be in any CS314 section taught by Mike. Review this paper on pair programming. You are not required to work in a pair on the assignment. (You may complete it by yourself if you wish.) If you begin working with one partner and do not wish to finish the assignment with that partner you must complete the assignment individually. If you work with a partner, the intent is that you work together, at the same computer. One person "drives" (does the typing and explains what they are doing) and the other person "navigates" (watches and asks questions when something is unclear). You shall not partition the work, work on your own, and then put things together.

You and your partner may not acquire from any source (e.g.  another student or an internet site) a partial or complete solution to a problem or project that has been assigned. You and your partner may not show other students your solution to an assignment. You may not have another person (current student other than your partner, former student, tutor, friend, anyone) “walk you through” how to solve an assignment. You may get help from the instructional staff. You may discuss general ideas and approaches with other students. Review the class policy on collaboration from the syllabus. If you took CS314 or CS307 in a prior semester and worked with a partner you must start from scratch on the assignment. If you took CS314 or CS307 in a prior semester and did this assignment on your own and want to work with a partner this term you must start from scratch.

The purpose of this assignment is to design and implement a program with multiple classes


  File Responsibility
Sample output SampleOutput.txt. A text file that shows what the output of the game could be. You do not have to follow this format exactly, but yours should be similar. Provided by me.
Implementation MastermindDriver.java Provided by me. DO NOT ALTER. This is how we will run your game.
Implementation Game.java Provided by you and me. (Mostly you)
Implementation Other classes you create for the game of Mastermind. Provided by you.
Testing MasterMindTest.java. Create a testing class for your program. Provided by you
Submission A4.jar. JAR of the program, including the source code for all classes you created plus Game.java, MasterMindTest.java and MastermindDriver.java. Provided by you.

Submission Requirement: Because you are designing the classes for this program, we don't know what the class names will be. Therefore your submission will be a jar file. jar is a program included in the standard edition of Java loaded on the computers in the Microlab and that you have on your computer if you downloaded Java. Some IDEs provide the ability to create jar files.

Here is a link to how to create jar files in Eclipse. If you are not using Eclipse you can use jar from the command line.

The archive tool we use in the class is called jar. (Java ARchive) I strongly recommend learning to use that tool well before the due date. In the past students have had trouble turning in the assignment on time because they did not allow enough time to learn to use jar. jar can be used to create executable Java programs and to archive multiple files into a single file. You will be using jar to create an archive, not to create an executable, so there is no need to include the .class files.

You can test your jar is correct by creating a new Java Project in Eclipse and adding your jar to it by selecting File -> Import -> General -> Archive file. Browse to the location of your jar and choose it. Look at the resulting project and see if the files you thought your jarred are actually added. Alternatively you could use a zip program to look at the contents of your archive file.

Do not include any directory structure in the jar. This means if you have an Eclipse project and an src folder the directory structure is NOT preserved. This is an option when creating the jar. (see the images on the handout for creating jars)

IMPORTANT: You must include the .java files in the jar, not the .class files. If your jar does not contain your source code (your .java files) you and your partner will receive a 0 on the assignment. It is vital that you do not to wait until the last minute to learn how to create jar files and that you test your jar to ensure the .java source code files are in it.

If you need help creating jar see the instructor, a TA, or a proctor during lab hours.

Problem Description:

Under the constraints of the General Assignment Requirements, design and implement a program to play a text based version of the board game Mastermind. You are free to use whatever classes and methods from the Java standard library you wish to use.

Read the Wikipedia article on the game of Mastermind. If you have never played Mastermind there are numerous online versions such as this one.

The version of the game you implement will have the following properties.

The output of the game should be a simple text based display. Look at this page for sample output. (User responses are in bold italics.) Your program does not have to match this output exactly but should be similar.

Part of the assignment grade will be determined by how easy it would be to alter your program to allow a different number of guesses before the user loses, a different number of pegs in the code, and how easily different colors could be added, assuming they start with a different letter than other existing colors. (Up to 26, one for each capital letter. Are there any colors that start with an X?) For example how easily would it be to change the code to have 5 pegs and allow pegs to be the color Maroon? A valid solution would be to ask the user for this information at the start, although this is not required.

Part of the assignment grade is based on breaking the problem up into a number of small, simple classes. One of the criteria of the assignment is to break the problem up into multiple classes even if you think the problem could be solved more easily with no discernible structure or one big class. For this assignment you should err on the side of having lots of simple classes instead of a few complex ones. Your solution shall have at least 5 different classes.

Your classes shall have well defined responsibilities. A poor solution would do output in most or all of the classes. A good solution confines output to one class. Think about how easy it would be to change your program if on Tuesday I require you to use a GUI instead of a text based interface.

The top level class of your program must be called Game. It must have a constructor that takes a boolean. The boolean is used for testing purposes. If it is true, then the secret code is revealed throughout the game. The Game class must also have a method named runGames that carries out the actual games. Your program must run correctly when the main method of class MastermindDriver.java is called.

You must include a tester class, MasterMindTest.java Even though your testing code will not be executed you must include your testing code (in its own class) in your submission. On this assignment you can't share testing code because you are all doing your own design.

When finished turn in a jar file named A4.jar that contains all the .java files needed for your program to be compiled and run. Include all the source code for all the classes you created, plus Game.java, MastermindDriver.java, and MasterMindTester.java. (Don't turn in the .class files.) This is not an executable jar, simply an archive jar. Use the turnin program to turn the file in. jar is a program included in the standard edition of Java loaded on the computers in the Microlab and that you have on your computer if you downloaded Java. If you work with a partner turn in only a single version of the code. Do not turn in the jar to both accounts. Instead, pick one person's account and turn the file into that account only.

Checklist. Did you remember to:

  1. review and follow the general assignment requirements?
  2. work on the assignment with at most, one other person?
  3. fill in the header in your MastermindDriver.java?
  4. ensure your program does not suffer a compile error or runtime error?
  5. test your program?
  6. create a testing class, MasterMindTest.java and turn it in with your other files? (Do not call the testing code in the final version of your program.)
  7. Ensure you jar files contains your source code files and not just the .class files? Ensure your jar has no directory structure? If your jar file does not contain al the necessary .java files for your program to run your assignment grade will be 0.
  8. turn in your Java source code in a file named A4.jar to the proper account in the Microlab via the turnin program before 11 pm, Thursday, February 13? If working with a partner turn in only one version of the code. (Do not turn in the jar to both of your accounts.)


  1. This is a difficult assignment because you must design (and design is messy!) and implement the program. You have designed algorithms but this is the first time you must design classes from scratch. Spend some time away from the computer, with pencil and paper, brainstorming what classes you need, what instance variables they need, and what methods they should have.

    Recall when designing a program in an object oriented way you should consider implementing a class for each type of thing you see in the problem. For example in Mastermind there are pegs, colors, codes (the secret code and the player's guesses can both be considered the same data type, a code), a board, results, a computer player, a human player, and an over-all game. Some things are so simple you may choose not to implement a class for them. For example the computer player doesn't do anything more than pick the secret code. Maybe that is simple enough for the Game or Board class to do. Also you may use some pre existing type, primitive or a class, to represent things. For example, you may choose not to have a color class. You could represent colors as single chars, Strings, ints, or possibly use the built in Java Color class. For this assignment you should err on the side of having lots of simple classes instead of a few complex ones.

    Avoid putting too much in the Game class. The suggested solution consists of 6 classes in addition to MasterMindDriver and Game. My Game class is a total of 75 lines including blank lines, lines with a single brace, and comments.
  2. After deciding what classes you need, implement them one at a time, simplest ones first. Test a class thoroughly before going on to the next class. You may need to alter your design as you implement and test classes. Remember Design a little - code a little - test a little. Repeat.

    I can't stress this point enough! The whole point of object oriented decomposition is to simplify the problem, but you need to make sure one class works before going on to another. To do this you will have to write code to test the class even though that testing code will not be used in the final program. Don't try to code all the classes and then test by running the game.
  3. You are free to use inheritance if you wish, but are not required to. (On this assignment inheritance and polymorphism are marginally useful at best.)
  4. This assignment is different than others because I provide no test cases for you. You are responsible for designing and implementing all tests. (Do this as you go! Not at the end! Saying "I ran the program and it worked." is not a valid testing strategy.)
  5. If you have never played mastermind one of the hardest things is understanding the feedback. The feedback gives no information about which peg in the guess generated the feedback.

    Assume we use capital letters for colors: B = blue, R = red, G = green, P = purple, W = white, and K = Black. For clarity I show empty pegs in the feedback with a hyphen (-), although these blank feedback pegs are omitted in most versions of the game.

    Assume the secret code is BRGR. The guess PBRP generates the feedback WW-- . All I know is I have two pegs of the correct color, but I have no idea from that guess alone which of the two pegs are the correct color. The feedback is not -WW-. Given the same secret code the guess RBPP generates the exact same feedback as PBRP, WW-- .

    When showing the feedback, give the number of black pegs first, then the number of white pegs as shown in the sample output. With the same secret code of BRGR, the guess RBGP would generate the feedback KWW-  (K for black).

    Finally a given peg in the secret code can generate at most one feedback peg with black given priority over white. Given the secret code BRGR the guess GGGG would generate a feedback of K--- only.
  6. If you are looking for an extra challenge you should educate yourself on Javas Enum type. Here is some documentation on Enum from Sun. Here is the Enum section from the Sun Java tutorial. Color is a good candidate to make an Enum instead of a Class. Another extra thing you might try is using the DrawingPanel class from the CS312 to implement a graphical user interface instead of a text based interface.

  Back to the CS 314 homepage.