# Programming Project #3

Assignment Overview This project focuses on programming with loops and conditionals, as well as our first project working extensively with strings. It is worth 60 points (6% of your overall grade). It is due Monday, June 2nd before midnight.  The Problem We are going to play the classic game Mastermind. If you don’t know the rules, you can look at: http://www.cut-the-knot.org/ctk/Mastermind.shtml
We are going to play the game with a slight variation, using numbers instead of colors. Thus each guess will be a 4-digit number, and the key (the number being decoded) will be a 4-digit number. We will also stipulate that no number will repeat in the key (to make your program a little easier). Thus, 1234 is a legal key but 1123 is not legal and should not be used in our game.
In our game, you provide the key to be guessed at the start of the program, and then your program allows a player to try and guess the entered key. (The program could easily generate a random key, but that would make testing your program more challenging.) Your program does not try to play mastermind, it simply enforces the rules and provides feedback to the player.
After each guess made by the player, your program generates two numbers: how many of the 4 numbers in the guess are exactly correct (the correct number in the correct position of the key) and how many of the guess numbers are in the key but not in the correct position.  Note that those two values cannot sum to more than 4, or put another way, each digit in the guess produces at most one value.
The player gets twelve guesses; if they do not guess the key in 12 guesses, they lose.
Program Specifications Your program will play the mastermind game as follows: 1. Prompt for the 4-digit key we will play with. No error checking here. 2. Prompt the user for a guess.  a. we check to see if the guess is exactly length 4 b. we check to see if the guess contains all digits c. we check to see if the guess has no repeats (this is the hardest check) any violation of these rules prints an error message and prompts for a guess again.  Bad input does not count as a guess toward losing the game. 3. End the game if: a. the guess is correct. Print out a win message including the count of how many guesses were made (see example below). b. it was the last guess (the twelfth) and the guess is incorrect. Print out a loser message and the value of the key. 4. If the game isn’t over, print the status of the game.  That is: a. print out how many of the guess digits are exactly right (correct digit in the correct position) and
b. how many of the guess digits are correct values but are not in the correct position.  For example, if the key is 1234 and the guess is 4256. The feedback would be 1 digit exactly correct (2) and 1 digit in the wrong position (4). That is, each digit in the guess produces at most one result (exact or wrong position).  5. Keep (and print) a history of guesses so that the user can see how they are progressing.  Use a string that you keep adding on to.  To put a carriage return in your history sting, include the “\n” character sequence. 6. Continue the game until an end condition occurs (as defined in step 3).  Deliverables proj03.py -- your source code solution (remember to include your section, the date, project number and comments).
1. Please be sure to use the specified file name, i.e. “proj03.py” 2. Save a copy of your file in your CSE account disk space (H drive on CSE computers).   3. You will electronically submit a copy of the file using the "handin" program: http://www.cse.msu.edu/handin/webclient
Assignment Notes: We are working with strings here, so here is a reminder of some functions that can be useful. Look at the online Python manual about strings for more details. Assume we have a string variable my_string = ‘abcde’ 1. len(my_string) returns the length in characters of the string. In this case it is 5. 2. my_string.isdigit() returns a Boolean indicating whether the string contains only numbers. In this case it returns False. 3. my_string.count(c)returns the number of occurrences of  character c in my_string 4. ‘a’ in my_string returns a Boolean indicating whether a string (in this case ‘a’) is part of the string. In this case, it returns True. 5. for ch in my_string:    print(ch) This loop goes through each individual character, one at a time, and sets ch (a variable name, you can choose it to be something else) to that character. In this case, it prints out the characters in my_string, one line at a time. 6. str(7) converts an integer (7) to a string (‘7’).  Getting Started 1. Do the normal startup stuff (create proj03.py, back it up to your H: drive, etc) 2. Write a high-level outline of what you want to do. Think before you code. 3. Start to break the problem down. How you do that is up to you, but here are some suggestions. a. Prompt for a key, and then loop and collect guesses into a string and print the guess string each time through the loop. Don’t error check yet or try to play the game. b. Add one error checking routine at a time (there are three; checking for repeated digits is the hardest check so leave that for last) c. Add one evaluation check at a time (there are two) d. Check for winners and losers.  Checking for winning is relatively easy so do that first.  Checking for losing involves keeping track of the twelve guesses so it is more challenging.

Sample Interaction Here, exist are those digits that exist in the key, but not in the right position; position are those digits that are both correct values and in the right position.
================================ RESTART ==========  What is the key:1234
Guess:4321 4321: exist:4, position:0
Guess:5678 4321: exist:4, position:0 5678: exist:0, position:0
Guess:1243 4321: exist:4, position:0 5678: exist:0, position:0 1243: exist:2, position:2
Guess:123 ****guess must have length of 4, try again 4321: exist:4, position:0 5678: exist:0, position:0 1243: exist:2, position:2
Guess:12345 ****guess must have length of 4, try again 4321: exist:4, position:0 5678: exist:0, position:0 1243: exist:2, position:2
Guess:1b34 ****contains non-numbers, try again 4321: exist:4, position:0 5678: exist:0, position:0 1243: exist:2, position:2
Guess:1123 ****no repeated digits, try again 4321: exist:4, position:0 5678: exist:0, position:0 1243: exist:2, position:2
Guess:1234 4321: exist:4, position:0 5678: exist:0, position:0 1243: exist:2, position:2
You guessed the key: 1234 It took you 4 guesses