CS 307Assignment, Implementing a Program With Multiple Classes - Mastermind
Official Assignment Handout

Programming Assignment 4: This is a pair assignment. You may work with one other person on this assignment using the pair programming technique. Review this paper on pair programming. You are not required to work in a pair on the assignment. 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 work together, at the same computer, on the assignment. 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 should not partition the work, work on your own, and then put things together. You may not acquire, from any source (e.g., another student or student pair or an internet site), a partial or complete solution to a problem or project that has been assigned. You may not show another student or student pair your solution to an assignment. You may not have another person (current student, former student, tutor, friend, anyone) “walk you through” how to solve an assignment. Review the class policy on collaboration from the syllabus.

If you work with a partner you will turn in one version of your code. Pick which account to submit the code to.

If you are working with a partner and want to use slip days you must both have the required number of slip days and both students use slip days. If the assignment is turned in 1 day late each student in a pair must have at least 1 slip day and it costs each student in the pair 1 slip day.

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


Provided Files:

  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 TestSummary.txt. A document detailing the tests you ran on your classes and program to ensure they work correctly. point out the testing code in the classes. (Method names, purpose, results of tests.) Provided by you
Submission A4.jar. JAR of the program, including the source code for all classes you created plus TestSummary.txt, Game.java, and MastermindDriver.java. Provided by you.

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 tips for this assignment at the bottom of the handout!

Read the Wikipedia article on the game of Mastermind.

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. You can make changes to the style of the output if you like.

Part of the assignment grade will be determined by how easily your program could be altered to allow 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?

Part of the assignment grade will be determined by if you broke the problem up into different 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.

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 purpose. 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 Mastermind.java is called.

You must provide a description of the tests you ran. Even though your testing code will not be executed you must include your testing code in your submission (simply comment it out) and a file that describes your testing strategy. 


When finished turn in a jar file named A4.jar that contains all the files needed for your program to be compiled and run. Include all the source code for all the classes you created, Game.java, MastermindDriver.java, and TestSummary.txt. (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. See my page on creating jars in eclipse, Sun's page for using jar, my tips for using jar, and / or the guide to creating jars in Eclipse.

Checklist. Did you remember to:


Tips

  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 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.
     
  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.
     
  4. This assignment is also different than others because I provide no test cases for you. You are responsible for designing and implementing all tests. (And do this as you go! Not at the end!)
     
  5. Use the Scanner class to get input from the keyboard. to use the Scanner class you must include the line

    import java.util.Scanner;

    in the class that uses the Scanner class. To create a Scanner object that is tied to the keyboard you could use the following code:

    Scanner s;
    s = new Scanner(System.in);

    In this assignment the only method you should need from the Scanner class is the getLine() method. Here are some examples of using the getLine() method.

    System.out.print("Enter your name: ");
    String name = s.nextLine();

    System.out.print("Press Enter to continue: ");
    s.nextLine();
     
  6. One word of caution. This assignment could easily turn into a huge time sink. So I recommend you work on this incrementally. Start with a design and try to get that to work. Have a working program at all time and add to it as you implement more features. This will avoid the assignment becoming an all or nothing affair. Even if you don't finish you will have a working version with some functionality ready to turn in.
     
  7. The hardest thing algorithmically about this assignment is how to generate a result given a secret code and a guess. It is important to realize black pegs should be generate first and that a given peg from the secret code and the guess cannot be reused.

    Here are some examples:

    Code:  RRRR
    Guess: BBBB


    This one is easy. There aren't any B's in the code, so the  guess of BBBB would generate a result with no pegs.

    Code:  RRRR
    Guess: RBBY


    The R in the first position of the guess matches up exactly with the R in the first position of the code. This generates one black peg. The key is once a peg has generated a peg in cannot be used to generate any more. So the R in the first position of the code and the first position of the guess cannot generate any more pegs. The guess of RBBY generates a result with 1 black peg and 0 white pegs.

    Here is another example.

    Code:  RBYR
    Guess: BBRG


     This code and guess would give a result of 1 black peg and 1 white peg. Black pegs take precedence so the B in the second position of the code and guess result in a black peg. One useful way of solving this problem is to determine the black pegs and scratch out or replace the pegs that were used. So given the Code of RBYR and the Guess of BBRG there could be a temporary result of

    Code:  R-YR
    Guess: B-RG


    To get the number of white pegs in the result the remaining characters can be compared for matches, independent of position. If a match is found then those pegs should be scratched out as well. The B in the first position of the guess doesn't match any characters in the code. The "-" in the second position of the guess should be skipped. The r in the third position of the guess matches the R in the first position of the code. This is a peg in the guess that is the right color, but in the wrong position so it generates 1 white peg. Once used the R in the first position of the code and the R in the third position of the guess can be reused so they should be scratched out.

    Code:  --YR
    Guess: B--G


    The G in the fourth position of the guess doesn't match anything so the result would be 1 black peg and 1 white peg. To summarize

    Code:  RBYR
    Guess: BBRG

    generates a result of 1 black peg and 1 white peg.
     
  8. 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.

  Back to the CS 307 homepage.