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:
(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
Requirements
When I examine your program, it should satisfy the following requirements.
This should only be attempted after all of the above specifications are working correctly.
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.