MP2: The Game of Mastermind -- Univ. of Illinois, CS 223 -- Fall 1996

Math 10 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics and Computer Science

[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:

  1. 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
    
  2. 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).
  3. The computer should match the user guess with the internally generated sequence, i.e., in this case bygb, and give the following two outputs:
    1. How many correct colored pegs at correct locations, and
    2. How many correct colored pegs at wrong locations.
  4. 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. 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:

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

7. Grading Criteria

Here is a rough grading scheme for this MP: No late assignments will be accepted. A more detailed breakdown of deductions will be provided with your graded code.

8. Suggestions


This page is maintained by Dennis C. Smolarski, S.J. dsmolarski@math.scu.edu
Last changed: 2 January 1998. Slightly modified 26 September 2001.