CS 1005, C-term 03
Homework 3
Alphabetic Mastermind

Due: February 11, 2003 at 11:59pm CHANGED!! Due: February 12, 2003 at 11:59pm

Assignment

In the game of Mastermind the computer selects a three-letter sequence in which all three letters are distinct. The task of the player is to keep making guesses until he or she guesses all three letters in the proper order. Each time the player makes a guess, the computer tells the player:

Thus, if the computer’s sequence was axo and the player guessed xao, the computer would respond with

1 hit
2 matches

 Write a C++ program to play the game of Mastermind. After the player guesses the correct sequence of letters, the program should display the total number of guesses made, then give the player the option to play again.

 

Before Starting

Read Chapter 3.

 

Details About the Program

Implementation & Design Restriction

Your program must include the following four functions:

 GenerateLetters

This function must call the random number generator and generate three distinct letters in the range a to z. Only lowercase letters are allowed.

 

GetGuesses

This function inputs 3 letters as the player’s guess. GetGuesses must  do some error checking on the input as follows:

 

Compare

This function should compare the computer’s generated letter sequence with the player’s guesses, and tally the number of hits and matches.

 

PrintResults

This function displays the number of hits and matches for a given guess.

 

You are invited to design and implement other functions if you wish. Every function must be documented with Pre- and Post-conditions (see documentation standard).

 

The program must also validate the user’s answer to the question “Would you like to play another game? Type Y or N (then hit <RETURN>):” Valid inputs are Y or y for yes and N or n for no. If the user types any invalid characters, the program must print out an error message “Invalid answer!  Try again” and give the user another opportunity to enter the input correctly.

 

Random Numbers

Use the C++ rand() function to generate the letters for the computer’s 3-letter sequence. Use the following code as an example of how to adjust the value returned by the rand function so that it ends up being a value between a and z.

char firstLetter;
firstLetter = 'a' + (char)  ((float) rand() / RAND_MAX * ('z' - 'a' + .5));

Don’t forget to initialize the random number generator using the srand function before calling the rand function. The code to do this is the same as the code we used in homework 2 and is shown below. If you skip this step you will discover that rand() returns the same sequence of random numbers for every execution of the program – good when you’re debugging the program, bad when you want to incorporate an element of surprise in the playing of the game.

// Seed the random-number generator with current time so that
// the first number will be different every time we start this program.
 srand( (unsigned)time( NULL ) );

 

Sample Execution

A sample output of the program would be:

MASTERMIND

 I will select a 3 letter sequence for you to guess.  All three letters will be distinct and will be lowercase. You must guess the three letters in the proper order.  Each time you make a guess, I will report the numbers of HITS and the number of MATCHES. One HIT means you guessed a correct letter in the correct position.   One MATCH means you guessed a correct letter, but in the wrong position.

Enter a 3 letter sequence:  abc

0 hits
0 matches

 

Enter a 3 letter sequence:  d1f

Illegal character!  Try again.

 

Enter a 3 letter sequence:  ddf

All letters must be distinct!  Try again.

 

Enter a 3 letter sequence:  def

1 hit
2 matches

 

Enter a 3 letter sequence:  dfe

Congratulations!  It took you 2 guesses to get the correct answer.

 

Would you like to play another game?  Type Y or N (then hit <RETURN>):  1

Invalid answer!  Try again.

 

Would you like to play another game?  Type Y or N (then hit <RETURN>):  n

 Game Over.







Include files

iostream
        allows
you to use cin, cout, endl

cstdlib
   
    allows you to use srand and rand

ctime
       
allows you to use time

Hints

This program is more complex than homework1 and homework2. Remember to Think First, Code Second! Spend some time designing your solution before trying to code it. Also, make sure you allow yourself enough time for this assignment. It will probably take 2 to 4 times longer than the homework1 and homework2 so please plan accordingly.

While developing the program, have the GenerateLetters function print out the computer’s “hidden” sequence. The program will be difficult to debug otherwise. Comment out this cout statement after you have finished debugging.

 

Program style and Comments

There's a lot more to programming than writing code that works. A good program is well-documented. Each program must contain in the comments your name and section number, and the date. The purpose of the program should be stated. Declared variables need to be explained with brief comments; use meaningful identifiers. Comments should be used to clarify obscure sections of code, but be careful - over-commenting can be worse than not including comments at all. Blank lines should separate major sections of the program. Indentation should make the program easy to read, and it should be used consistently. Refer to Documentation Guidelines for specific guidelines on how to document your programs for this course.

 

Deliverables

You must turn in your source code, a script file showing successful compilation and a sample execution of your program, and your documentation file called README. The command to use to turn in your files for this assignment is

/cs/bin/turnin submit cs1005 hw3 hw3.cxx hw3.script README
This assignment is due at 11:59pm on Tuesday Feb 11th, 2003.

 

Grading

The grade for this assignment will be based on 100 points. Point values will be awarded as follows:

Documentation (30 points)

Internal documentation

introductory comment, meaningful identifiers, proper use of indentation and blank lines, comments for variables, algorithm comments. Don’t forget that every function must be documented with pre-conditions and post-conditions.

External documentation

completion and submission of the CS department standard documentation file, (see Documentation Guidelines ). Name your documentation file README.

Robustness (10 points)

Design the program so that it is able to recover from errors in user input.

Testing (10 points)

The sample execution(s) in your script file should demonstrate the program’s features.

Functionality (30 points)

The program should behave as described. Your script file should show successful compilation. Programs must compile in order to receive points for Functionality.

Design (20 points)

We will be looking for a sound, modular design, and the proper use of parameters and local variables. The program must contain the four functions defined above.