COMP 141 Fall 2017

Program 7: Mastermind

Assigned: Friday, November 3
Due: Thursday, November 16, 2017 by 11:55pm

We are going to play the classic game Mastermind. If you don't know the rules, you can watch this video: https://www.youtube.com/watch?v=ZSXpmIH-_d4
Here's the summary of the rules: Mastermind is a game played by two players, the codemaker and the codebreaker. The game begins with the codemaker selecting a code, a sequence of four colors (digits, pegs or other symbols) chosen from a set of six colors (repetitions allowed). The codebreaker will then try to guess the code. After each guess, the codemaker responds with two numbers. The first, the number of exact matches, is the number of times the codebreaker guessed the correct color in the correct position. The second, the number of near matches, is the number of colors in the guess which are the right color, but in the wrong position.

For our purposes, we will use numbers instead of colors, so each guess will be a 4 digit number and each code (the number being decoded) will also be a 4 digit number. We will also ensure that the secret code has no repeated digits. Therefore, 1234 is a valid code, but 1223 is not.

In our game, Player 1 will provide the code to be guessed at the start of the program, and then your program allows Player 2 to try and guess the entered code. The program does not generate a random key so that you can more easily test your program. (See the Challenge questions for an opportunity to randomly generate a code.) Your program does not try to play mastermind, it simply enforces the rules and provides feedback to the player. Player 2 gets twelve guesses and if they do not correctly guess the code in 12 guesses, they lose.

Here are the details:

You must follow these steps:

Sample Interaction

(What the user types is in bold.)
(Program begins)
Enter the secret code; (4-digit number, no repeats): 6453
Enter a 4 digit guess: 1221
Invalid guess.
Guess again: 2342
Invalid guess.
Guess again: 12345
Invalid guess.
Guess again: abcd
Invalid guess.
Guess again: 1234
You have 0 perfect matches.
You have 2 near matches.
Enter a 4 digit guess: 1265
You have 0 perfect matches.
You have 2 near matches.
Enter a 4 digit guess: 3456
You have 2 perfect matches.
You have 2 near matches.
Enter a 4 digit guess: 3546
You have 0 perfect matches.
You have 4 near matches.
Enter a 4 digit guess: 6453
You win. It only took you 5 guesses!
(Program ends)
Another sample run:
(Program begins)
Enter the secret code; (4-digit number, no repeats): 2431
Enter a 4 digit guess: 6543
You have 0 perfect matches.
You have 2 near matches.
Enter a 4 digit guess: 5432
You have 2 perfect matches.
You have 1 near matches.
Enter a 4 digit guess: 5431
You have 3 perfect matches.
You have 0 near matches.
Enter a 4 digit guess: 6431
You have 3 perfect matches.
You have 0 near matches.
Enter a 4 digit guess: 1234
You have 1 perfect matches.
You have 3 near matches.
Enter a 4 digit guess: 2345
You have 1 perfect matches.
You have 2 near matches.
Enter a 4 digit guess: 3456
You have 1 perfect matches.
You have 1 near matches.
Enter a 4 digit guess: 3421
You have 2 perfect matches.
You have 2 near matches.
Enter a 4 digit guess: 2341
You have 2 perfect matches.
You have 2 near matches.
Enter a 4 digit guess: 2143
You have 1 perfect matches.
You have 3 near matches.
Enter a 4 digit guess: 2413
You have 2 perfect matches.
You have 2 near matches.
Enter a 4 digit guess: 2561
You lose. The correct code was 2431

(Program ends)

What to Do

  • Create a Python program named yourlastname_yourfirstname_prg7.py
  • Include the standard program header at the top of your Python file.
  • Follow the comment guide to correctly comment your code.
  • Submit your Python file on Moodle under Program 7.
  • Requirements

    When I examine your program, it should satisfy the following requirements.

  • Your program file is named correctly and it includes the standard program header.
  • You include a main function.
  • You prompt Player 1 for the secret code.
  • You correctly write the code for count_perfect_matches.
  • You correctly write the code for count_near_matches.
  • You correctly write the code for repeated.
  • You write a loop that allows the user to continue to play until the code is guessed or they've guessed 12 times.
  • At each iteraction of the loop you: prompt the user to guess the code.
  • At each iteraction of the loop you: check the user input to make sure it is a valid guess.
  • At each iteraction of the loop you: tell the user the number of perfect matches and the number of near matches.
  • At the end of the game, you tell the user whether they won or lost.
  • At the end of the game, if they lost, you tell them what the secret code was.
  • You comment your code appropriately (particularly your functions), use appropriate variable names, and your code must be neatly and clearly formatted and include proper use of 'white space'.

  • Challenge Question

    This should only be attempted after all of the above specifications are working correctly.

    Grading

    Your program will be graded on correctness, as well as on coding style, which refers to choices you make when writing your code, such as good use of variable names, appropriate indentation, and comments (this is not an exhaustive list). See the syllabus for further grading details.

    You will receive one bonus point for every complete day your program is turned in early, up to a maximum of five points. For instance, if your program is due on September 20 at 11:59pm, if you turn in your code on Moodle any time on September 19 from 12:00am through 11:59pm, you will receive one extra point on your project. Programs submitted on September 18 from 12:00am through 11:59pm will receive two points. This pattern continues for up to five points.