Mastermind Project

Write the game Mastermind using Python. The project is due Sunday, Sept. 13, at Midnight. Thursday Sept. 17, 11:59 (suggested)

Try the game out at: http://www.irt.org/games/js/mind/

You'll be creating a command-line version of this game.

Here are the rules for Mastermind.

Besides the rules specified in the link above, here are some additional specifications:

1. The game should be one player, user versus the computer. The computer program you write will generate the secret code and provide feedback for each guess. The secret code should be randomly generated for each new game. You can import the module 'random' and then call:

        random.randint(0,5)

to get a number between 0 and 5 inclusive. Then you'll need to convert that into a color (e.g., 'red') using an if-else clause.

2. The player should enter the full name of each color of a guess, e.g., 'red' or 'blue'. Store the colors in a list.

3. Print out the number of exact and partial matches as the feedback for each guess, e.g.,

    2 exact, 1 partial.

Note that the feedback only says how many exact and partial matches, and nothing about which slots are matching.

4. The system should perform error-handling and let the user know when she has entered an invalid color. The user should be allowed to try again until she enters a valid guess. Instructor Note: Do this last

5. The system should have clear instructions so that someone (like your teacher) can sit down and play without asking the programmer (you) any questions.

6. Don't limit the player's guesses-- let them guess forever if they want.

7. Write the program in a file called mastermind.py

Iterative Process

You should always develop software iteratively, that is, get a little part done, test it to make sure it works, then add a little bit, test, add a bit, test, until the software is complete.

Here is an iterative plan for this project.  Follow it, not only as a good way to complete a project, but because you'll be given partial credit for the project (15 points) for each iteration you complete.

1. Write the getGuess code that allows the player to enter four colors into a list.  After reading in the guess, print it to make sure the colors are in the list correctly. 

2. Write code to generate the secret code. Use Python's 'random' module. Here's some code to read a single random number between 0 and 5 inclusive:

    import random  # put this at top of file

    number = random.randint(0,5)

You'll need to put the second line within a loop, to get four colors for the secret code, and you'll need to generate a color, e.g., 'red', from each number.

Test your code to make sure that it generates a different code each time you run it.

3. Write code that computes the exact matches. This code should compare the colors of a 'guess' list with the colors of a 'secretCode' list and increment a variable 'exacts' each time there is an exact match. You can test this code separately from (1) and (2)-- just create guess and secret code lists with fixed values and compare them.

4. Write code to compute partial matches. This code also compares the colors of the 'guess' with those of the 'secretCode', but for partial matches. This is the most complex code in this program.

5. Write a loop around parts of the previous code so that the player can repeatedly enter guesses and received feedback from the system.

6. Add code so that, when the player enters a correct guess (all exact matches), the system prints out a congratulations and tells the user how many guesses were required.

Extra Credit

Allow the user, when starting a new game, to choose the number of colors, up to ten. You can use a pre-defined set of colors (5 extra points)
Allow the user, when starting a new game, to choose the number of slots in a secret code or guess. (5 extra points)

Mastermind Grading Format