CS110 Introduction to Computing
Fall 2006
Lab Assignment#8
Problem: Write a program that implements the game of Mastermind!.
The program selects a random set of digits (i.e. a secret code), whose length is specified by a variable say digits, and the user has to guess them in a finite number of rounds, also specified by a variable say rounds. The program reports the number of digits selected by the user that match the hidden digits. The number of digits that are correctly placed as well as incorrectly placed are reported. For example, if the secret code is 1 2 2 5, and the user guesses 3 1 2 2, then the program reports that there is 1 digit in place (the 2 that is the third digit), and 2 digits out of place (1 and the other 2).
The game stops when the user guesses the digits selected by the program or when the maximum number of rounds allowed has been reached. If the user did not guess the secret code, print out the code as well.
Getting Started
Remember to build up your solution incrementally, add and test one function at a time.
Your program should meet the following requirements:
- Allow the user to enter all digits space separated. You should take user input as a string and split the digits yourself.
- Do not worry about input error checking, that is, you may assume that the user will enter the input correctly according to your specifications, i.e. correct number of digits, correctly separated by spaces and all digits between 0 and 9. However, if you do implement input error checking, you will earn extra credit.
- Use a list to store the original secret code.
- Use a list to store the user guess as well.
- The number of digits in the code as well as the number of rounds should be controlled by variables so that they can be modified easily. You should inform the user at the beginning of the game how many digits he/she will be guessing and how many rounds. Set the game to 4 digits and 10 rounds is a good choice of difficulty.
- Have two counters that keep track of how many digits are in-place and how many are out-of-place, respectively.
- Do the in-place matching/counting first.
- You should think of a way to mark off digits that have already been matched, so as to avoid counting them repeatedly. For example, in the example given above with the secret code being 1 2 2 5 and 3 1 2 2, the correct count is 1 digit in place (the third 2 in the secret code) and 2 digits out of place. The digits out of place are 1 and the second 2 in the secret code. You do not want to count the third 2 again as an out of place digit because it's already matched as in place.
What you will need to do:
- Make sure you have read chapter 11 and understand the examples and library functions presented there.
- Do the design first: plan out a complete sketch prior to writing your program.
- Split your program into functions and make an incremental implementation plan.
- Make sure you do the above steps prior to entering the lab (or using your computer).
- Test and debug your program until it produces correct results.
- When done, print out the final version your your program and also a printout showing your program's sample outputs for at least two runs of a game. Demonstrate correctness on the calculation of inplace and out-of-place digits.
- Make a blog posting saying that you have completed the assignment and also write up any logistical or other issues/questions raised while working on this assignment. Also write about your level of confidence on the concepts learned so far.
- ALL work in this course should be done individually without any help from any person other than the class TA's and instructor(s).
Notes:
Add the following line to all your Python programs:
# File: <place name of your program file here>
# Date: <date created>
# Created by: <your name>
# Assignment: <place assignment number here>