CS21 Lab 8: Mastermind

Due before 11:59pm Tuesday night Nov. 1

Run update21 to create your cs21/labs/08 directory then copy our mastermindTDD.py file from your cs21/labs/07 directory into a file named mastermind.py in your 08 directory. You will use this as the starting point for the full implementation of Mastermind.

$ update21
$ cd cs21/labs/08
$ cp ../07/mastermindTDD.py mastermind.py
$ ls
  mastermind.py
$ vim mastermind.py   

Introduction
You will complete the implementation of the Mastermind program using your lab 7 Top-Down-Design solution as a starting point for implementing the complete program.

Mastermind is a code-breaking game for two players. For our version, the computer will be the code maker and the program user will be the code guesser or breaker.

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. Possible codes are abcd, eafb, cbaf, and so on. 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.

To make things more concrete let's consider some examples. Below is an example of just part of the game -- a code (usually not shown!) and multiple guesses, to show exact and partial matches.

code = 'eadb'

guess: abcd
             0 exact matches (letter *and* position correct)
             3 partial matches (letter correct, position incorrect)
guess: efab
             2 exact matches (letter *and* position correct)
             1 partial match (letter correct, position incorrect)
guess: cdef
             0 exact matches (letter *and* position correct)
             2 partial matches (letter correct, position incorrect)
guess: cabe
             1 exact match (letter *and* position correct)
             2 partial matches (letter correct, position incorrect)
You should do incremental, bottom-up, implementation and unit testing of functions. One way to do unit testing is within the python interpreter (another is to add calls to the function you are testing from main). To do unit testing in the interpreter, import your program in the python interpreter and then calls your functions from the interpreter prompt, passing in different values to test them out. To import your program in the python interpreter without having python run it, you need to have the following at the bottom of your file:
if __name__ == '__main__': main()
Then do:
$ python
>>> from mastermind import *
Now you can call functions in mastermind.py. For example, if I have a function named generateSecretCode, I can call it from the python interpreter prompts like this:
>>> x = generateSecretCode()
>>> print x
abef
>>> print generateSecretCode()
afec
Requirements
Sample Games

Here are some sample games, to help you understand the program requirements:

$ python mastermind.py

Welcome to mastermind.  I'm going to create a secret code of
four letters taken from the set [a,b,c,d,e,f].  My code will
contain only one of each letter from the set.
See if you can guess my code in 10 tries or less.
After each guess I'll give you a hint as to the number of
letters you have correct and how many correct letters are in
their exact correct position.  Use this information to make a better guess

Good Luck!

Enter guess number 1 : ABCD
        0 exact matches (letter *and* position correct)
        2 partial matches (letter correct, position incorrect)
Enter guess number 2 : xyze
Your guess must contain only the characters a-f with no duplicates...try again
Enter guess number 2 : abcdefg
Sorry, your guess abcdefg is not 4 letters long...try again
Enter guess number 2 : cdef
        0 exact matches (letter *and* position correct)
        3 partial matches (letter correct, position incorrect)
Enter guess number 3 : acef
        0 exact matches (letter *and* position correct)
        2 partial matches (letter correct, position incorrect)
Enter guess number 4 : bdef
        0 exact matches (letter *and* position correct)
        4 partial matches (letter correct, position incorrect)
Enter guess number 5 : defb
Good for you, you won in  5 guesses


$ python mastermind.py

Welcome to mastermind.  I'm going to create a secret code of
four letters taken from the set [a,b,c,d,e,f].  My code will
contain only one of each letter from the set.
See if you can guess my code in 10 tries or less.
After each guess I'll give you a hint as to the number of
letters you have correct and how many correct letters are in
their exact correct position.  Use this information to make a better guess

Good Luck!

Enter guess number 1 : abcd
        1 exact match (letter *and* position correct)
        2 partial matches (letter correct, position incorrect)
Enter guess number 2 : bcde
        2 exact matches (letter *and* position correct)
        1 partial match (letter correct, position incorrect)
Enter guess number 3 : cdef
        0 exact matches (letter *and* position correct)
        3 partial matches (letter correct, position incorrect)
Enter guess number 4 : adef
        0 exact matches (letter *and* position correct)
        2 partial matches (letter correct, position incorrect)
Enter guess number 5 : acde
        1 exact match (letter *and* position correct)
        1 partial match (letter correct, position incorrect)
Enter guess number 6 : cdef
        0 exact matches (letter *and* position correct)
        3 partial matches (letter correct, position incorrect)
Enter guess number 7 : adef
        0 exact matches (letter *and* position correct)
        2 partial matches (letter correct, position incorrect)
Enter guess number 8 : febc
        0 exact matches (letter *and* position correct)
        3 partial matches (letter correct, position incorrect)
Enter guess number 9 : feba
        0 exact matches (letter *and* position correct)
        2 partial matches (letter correct, position incorrect)
Enter guess number 10 : febc
        0 exact matches (letter *and* position correct)
        3 partial matches (letter correct, position incorrect)
Sorry, you lose, the secret code was: bcfd

Useful Resources
Extra Challenge Extensions
Below are two suggestions for extensions to lab 8 if you are looking for an extra challenge. You are welcome to come up with your own extensions and try them out too. These are NOT required parts of the lab 8 assignment, and as usual, do not try any of these until you have a fully working solution to the regular lab 8 assignment.

If you attempt any extra challenge parts, do not add them to your mastermind.py file, but instead first make a copy of your mastermind.py file to extra_mastermind.py, and implement your extensions in the extra_mastermind.py file (i.e. your mastermind.py file should contain your solution to the regular lab 8 assignment only):

$ cp mastermind.py extra_mastermind.py
$ vim extra_mastermind.py

Suggested Extensions:

  1. Change your program to allow for repeat letters in the secret code. The hardest part 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.

  2. Use the graphics library and implement a graphical interface (GUI) to your mastermind program. The user should click on the window to make guesses and you should graphically display the exact and partial match information. You could certainly use colors and/or shapes instead of letters if you implement a gui version of mastermind.

Submit

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