CS21 Lab 7: Mastermind

Top-down design due 11:59 pm, Thursday March 17th, 2011
Full program due 11:59 pm, Tuesday March 22nd, 2011

In this lab, you will design and implement a program that plays the game Mastermind. This lab is due in two parts. You should email your top-down design to Charlie no later than Thursday, 17 March -- the Thursday after spring break. The full implementation of the programming assignment is due as usual on a Tuesday night, 22 March. After getting feedback from Charlie on your proposed design, you will use bottom-up implementation and unit testing to incrementally construct the full program.

Mastermind is a code-breaking game for two players. Another description of the game and rules can be found on the Pressman Toy Corp. website. For our version, the computer will be the code maker and the program user will be the code guesser.

At the start of the game the computer will create a 4-letter code, where each letter is one of 6 possible letters (abcdef). To simplify the game, our version will not allow a letter to be used in the code more than once. For example, abcd, eafb, and cbaf are all valid codes, but abca is not valid because the letter a is used more than once.

The user then has 10 chances to guess the code. After each guess the computer tells the user how many exact and partial matches they achieved. An exact match means the letter and the position are correct. A partial match means the letter is correct, but the position is wrong.



Sample games

Here are some sample games:

$ python mastermind.py

  Welcome to MasterMind v0.9!!

  I will create a 4-letter code using letters from the
  set "abcdef". Only one of each letter is allowed in the
  code (i.e., no repeat letters are allowed).  Your job is to guess 
  the code. After each guess I will provide feedback, telling you:

   number of exact matches (letter and position correct)
   number of partial matches (letter correct, but in wrong position)

  Note: in neither case do I tell you *which* letters are exact
  and which are partial matches. I only tell you how many exact
  and partial matches you got.

  You have 10 chances to guess my code...

guess 1: abcd
                1 exact matches (letter *and* position correct)
                1 partial matches (letter correct, position incorrect)
guess 2: cdef
                1 exact matches (letter *and* position correct)
                2 partial matches (letter correct, position incorrect)
guess 3: efab
                1 exact matches (letter *and* position correct)
                2 partial matches (letter correct, position incorrect)
guess 4: fade
                0 exact matches (letter *and* position correct)
                3 partial matches (letter correct, position incorrect)
guess 5: afec

You win!!
The code was: afec
  

$ python mastermind.py

  Welcome to MasterMind v0.9!!

(intro stuff deleted to save space...)

guess 1: abcd
                0 exact matches (letter *and* position correct)
                3 partial matches (letter correct, position incorrect)
guess 2: abcdefg
  Please enter a 4-letter code....
guess 2: 1234
  Please enter a 4-letter code....
guess 2: fedc
                1 exact matches (letter *and* position correct)
                1 partial matches (letter correct, position incorrect)
guess 3: deaf
                2 exact matches (letter *and* position correct)
                1 partial matches (letter correct, position incorrect)

(many intermediate guesses deleted to save space)

guess 10: aced
                0 exact matches (letter *and* position correct)
                3 partial matches (letter correct, position incorrect)

You lose!  Next time we will battle to the death.
The code was: deba

Requirements
For this lab you must turn in your top-down design by the Thursday before the programming deadline. You should create your design and email it to Charlie. We will then look over your design, make comments on it, and send it back. If we think your design is inadequate we will ask you to fix and resubmit it. Once we approve your top-down design, you may implement the full program.

Here are the top-down design requirements:

Here is an example of a stubbed out program. Your top-down design should be similarly structured, except solving the Mastermind game instead of our dice simulation.

Once you email us your design, we will try to provide feedback within 36-48 hours. However, if everyone emails us their design at the same time, it might take longer. We encourage you to start your design early, so you have enough time to get our comments and still implement the full program before the final deadline.

Once you are done with the top-down design and receive feedback from Charlie, you may start implementing your program. Start by copying your design file to the filename mastermind.py, then edit and implement your program as mastermind.py:

  $ cd
  $ cd cs21/labs/07
  $ cp design.py mastermind.py
  $ gvim mastermind.py
Here are the full-program requirements:
Optional extension

One of the hardest parts of the real mastermind game program is figuring out how many exact and partial matches there are for each guess, if repeat letters are allowed. For example, if the code is "acab", and the user guesses "aafa", that would be 1 exact match (the first "a"), and one partial match (the second "a"). Notice how the third-guessed "a" is not a match, as the two in the code have already been matched.

As an optional extension, allow for repeat letters in the secret code. If you do this, please turn in both your original solution (which disallows repeated letters) and your extension as separate python programs.



Submit

Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.