MP2: The Game of Mastermind -- Univ. of Illinois, CS 223 -- Fall 1996
[Return to Math 10 Homepage]
The Game of Mastermind
This programming assignment is offered as a sample of what is assigned
at other universities. CS 223, is a first course in C++ for computer
science majors. It differs from Math 10, in that it is assumed that
students will have had a preliminary course in the language SCHEME,
which is a cousin to the language LISP, a functional language. None
of the students, however, had had any previous formal experience
programming in C++.
This program was due October 13, 1996, about 5 weeks after the
beginning of class (one third of the way through the semester). It was
the second program due.
1. Problem Description
In this program you and the computer will play the game of Mastermind.
The game goes like this. The computer will generate a random sequence
of four colors from a given set of six colors. You have to guess in the
shortest number of attempts what the sequence is, i.e. the colors and their
respective positions.
At each guess you will enter a four letter code for a specific set of colors
at their respective p;ositions.
The colors and their representation are as follows:
COLOR SYMBOL
White w
Red r
Green g
Blue b
Yellow y
Cyan c
Program Details
Here are the high-level steps that your program should follow:
- When the program starts the computer should generate a random
combination of these colors corresponding to each of the four places.
The colors can be repeated any number of times. E.g., the computer
generates a random sequence bygb. This would mean the sequence:
POSITION COLOR
1 Blue
2 Yellow
3 Green
4 Blue
- The computer should ask the user for the guess. The user is
expected to enter a four leter code. Of course, the program
should give error message if the entered code is incorrect or
outside limits (e.g., wsdg).
- The computer should match the user guess with the internally
generated sequence, i.e., in this case bygb, and give
the following two outputs:
- How many correct colored pegs at correct locations, and
- How many correct colored pegs at wrong locations.
- Reading this, the user would guess the next possible
sequence, and steps 2 and 3 should be repeated until the user
finds the exact sequence that the computer had generated, or the
user has entered 10 guesses and has not yet found the correct
sequence. Points should be given in the form of how many guesses
the user had made to reach the final sequence.
3. Input
The followng will be the input to your code by the test cases.
- First, a seed for the random generator.
- Then guess sequences of 4 characters.
We (i.e., the TAs and graders) will be running test files through
your program. They can be found in the class directory. An
example of a test file is as shown below:
12
bbbb
bycr
ggrr
1234
ghyi
90io
drg6
bgBc
BBg2
GGYB
12dc
GGRb
Make sure that your code runs well, and distinguishes between
proper and improper input, before you handin your code.
4. Expected output
Here is the expectged output that your program should produce:
- A prompt for each input, the seed and the guesses
- The number of correct colored pegs at correct positions
- The number of correct colored pegs at incorrect positions
- After 10 guesses, the program should terminate and give the
correct sequence.
5. An example of a running program
Your output need not look exactly like this; but format it so
that it is easily readable.
% mp2
Welcome to Mastermind.
Enter a seed for the random generator: 12
Computer has determined its sequence.
Enter your guess (from wrgbyc): gbyb
2 correct colored pegs at right locations
0 correct colored pegs at wrong locations
Enter your guess (from wrgbyc): cbwb
0 correct colored pegs at right locations
1 correct colored pegs at wrong locations
Enter your guess (from wrgbyc): gcyr
2 correct colored pegs at right locations
2 correct colored pegs at wrong locations
Enter your guess (from wrgbyc): gryc
CORRECT !! You have done it in 4 guesses.
%_
6. Other Details
- Your program should have appropriate error checking for a
wrong guess and let the user re-enter the guess again.
- You should provide for input both in Upper case and in Lower
case letters.
- Do not use the string library functions.
- You should use the srand() and rand()
functions to seed the random generator and generate random
numbers to determine the sequence. See your book forhow to use
these functions. You will need to add the following line to the
top of your program:
#include <stdlib.h>
7. Grading Criteria
Here is a rough grading scheme for this MP:
- Functionality -- 50 points
- Compilation -- 10 points
- Test Cases -- 40 points
- Solution Quality -- 50 points
- Coding Style and Comments -- 25 points
- General Functionality and Breakdown -- 25 points
No late assignments will be accepted. A more detailed breakdown
of deductions will be provided with your graded code.
8. Suggestions
- First and foremost, start early. This should not be a
difficult assignment. But the thought you put into it early will
alleviate days' worth of anxiety and stress as the deadline
approaches.
- Make sure that your code is modular. Write separate
functions for each task. If you do not do this, you will lose points!
- Comment your code. This includes commenting function
headers and only parts within your code that are non-intuitive.
Don't get caught commenting every line, like assembly code. The
rule of thumb to use is: comment your code so that you could
understand everything if you were to read it again next year.
- Test your code. This should be done by compiling in your
own directory, not using handin. You have access to all
the test files. Make use of this resource. For this assignment,
we will not be comparing your output to any standard output.
- Handin something, and handin early. Don't
wait until 10 minutes before the due time. Everyone will be
handing in at that time and your handin may be delayed,
causing loss of points. Excuses related to the speed (or lack
thereof) of handin will not be accepted.
- Read the newsgroup. Official announcement concerning this
MP will be made thorugh the newsgroupo, as well as clarifying
remarks. You are responsible for the official information
provided in the newsgroup.
- We have provided a sample executable for you. Check out
mp2 to see how your code should work.
- Try to enjoy yourself. :)
This page is maintained by Dennis C. Smolarski, S.J.
dsmolarski@math.scu.edu
Last changed: 2 January 1998. Slightly modified 26 September 2001.