In Jotto, the player tries to guess a secret five letter word. The game is similar to Mastermind in that each guess results in learning how many letters the guess has in common with the secret word. Mastermind is played with colored pegs while Jotto is played with words --- and in Jotto the position of letters in common is not important, just the number of letters the secret word and a user's guess have in common.
For this assignment you will write a program that lets the user guess a secret word. For each guess the user makes, she should be given feedback about how many letters her guess has in common with the secret word. If she cannot guess the secret word within ten tries then she loses. If she guesses the secret word exactly, then she wins. At the start of the game, you should pick a secret word from a file of possible words and remember it throughout the game.
To start, your should snarf this assignment, jotto.
Here are some runs illustrating how your program
might appear to the user. Take this run as a guideline, not
a requirement, of how your program should work. User entered input is in italics.
Here is another run from a working program.
guess #1: fruit
fruit has 1 letter in common with secret word
guess #2: snake
snake has 2 letters in common with secret word
guess #3: piano
piano has 3 letters in common with secret word
guess #4: sonic
sonic has 2 letters in common with secret word
guess #5: panic
panic has 2 letters in common with secret word
guess #6: snack
snack has 2 letters in common with secret word
guess #7: manor
manor has 3 letters in common with secret word
guess #8: slide
slide has 0 letters in common with secret word
guess #9: guano
guano has 5 letters in common with secret word
YOU WIN!!!
guess #1: brown
brown has 0 letters in common with secret word
guess #2: plaid
plaid has 2 letters in common with secret word
guess #3: dowel
dowel has 1 letter in common with secret word
guess #4: prawn
prawn is not in the dictionary, try another word
guess #4: brine
brine has 0 letters in common with secret word
guess #5: fruit
fruit has 0 letters in common with secret word
guess #6: track
track has 1 letter in common with secret word
guess #7: ghost
ghost has 2 letters in common with secret word
guess #8: sharp
sharp has 2 letters in common with secret word
guess #9: stale
stale has 3 letters in common with secret word
guess #10: lasts
lasts has 4 letters in common with secret word
YOU LOSE!!!
The secret word was: glass
The program you write should allow the user to make 15 guesses. If the word has not been guessed it should be revealed to the user (in the run shown above the user was only allowed ten guesses; likewise, you may want to use a number lower than 15 when testing). Note, words not in the dictionary should not count towards these 15 and should not be reported back as having letters in common with the secret word.
To have the user play Jotto you will write code in jotto.cpp to play a game, so that the user tries to guess the computer's secret word as shown in the sample runs above.
You will also need to create a class Dictionary which is specified in dictionary.h and implemented in dictionary.cpp. An example of its use is given in the jotto.cpp file you are given.
The class Dictionary, whose header is in dictionary.h and reproduced below, allows you to get a random (secret) word and to check if a word the user enters is in the dictionary. The dictionary is loaded via the Dictionary::LoadWords method.
class Dictionary
{
public:
Dictionary ();
void LoadWords (const string& filename); // read a file of words
bool Contains (const string& word); // true iff word in dictionary
string GetSecretWord (); // select word at random
private:
// not shown
};
To illustrate what happens with a guess, suppose the secret word is
"horse". For each of four different guesses, the number of
letters in common is shown in the table below.
| guess | common letters | count |
|---|---|---|
| mirth | r,h | 2 |
| moose | o,s,e | 3 |
| short | s,h,o,r | 4 |
| seems | s,e | 2 |
Once a particular letter is counted as in common, it cannot be counted again. Note in the table above that "seems" has only two letters in common with "horse".
For more information about this algorithm, see this weekly problem.
In implementing the function commonCount you'll need some way to avoid counting a letter more than once if it is in common to both words. Using the string method replace (examples of which can be found in stringreplace.cpp), the second argument in the call to method replace should be 1, and the third argument will be a single-character string that does not normally occur in words, e.g., something like "@".
This assignment is worth 30 points.
| criteria | points | goals |
|---|---|---|
| dictionary class | 10 | works correctly and well written |
| jotto game | 15 | works correctly and is well written |
| README | 5 | completeness |
Submit your project as 03_jotto, using any of the standard methods. Always be sure to submit all .h and .cpp files, even if you didn't modify them. Do not forget to submit a README too according to the directions given on the assignment page.