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
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
if __name__ == '__main__': main()
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
>>> word = "apple" >>> print word "apple" >>> charList = list(word) >>> print charList ['a', 'p', 'p', 'l', 'e'] >>> rejoined = ''.join(charList) >>> print rejoined "apple"
s = "hello there" l = [1,2,3,5,78,9] if ( 'e' in s): print "e is in", s if (17 not in l): print "17 is not in the list"
>>> l = [1,2,3,5,78,9] >>> l.remove(5) >>> print l [1,2,3,78,9]
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:
Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.