Microsoft Word – lab9.docx
Your semester-long assignment, much of which you have already completed or are in the process of completing, is to write a computer program which plays a game of Hangman using this “Evil Hangman” algorithm.
- A string-handling module. You have already completed your string module for this purpose.
- A vector of strings, which you implemented in a previous lab.
- An array of vectors of String_Ptr containing the entire dictionary read from the “dictionary.txt” file. The dictionary file contains an unabridged dictionary with over 120,000 words. The array of Vectors of String_Ptr should hold 30 Vectors (to hold vectors of words of lengths 1 to 29), each of which holds String_Ptr objects, which each hold a word. TA CHECKPOINT 1 Demonstrate to your TA that you can get your array of vectors of String_Ptr to store all of the words in the dictionary in vectors where all the words in that vector are the same length. Have your program print out the size of each one of these vectors (essentially providing a count of how many words of each length that you have). This should have been done in the previous lab.
- An AssociativeArray module, as will be described in next labs, which allows using String_Ptr data as the “key” or “index” to locate a particular Vector of String_Ptrs. The AssociativeArray must be based on (implemented using) a binary search tree. There will be no duplicate keys. When a new String_Ptr data item is added to the AssociativeArray, a “key” value (also a String_Ptr object) will be given. That “key” is located in the binary tree (or added to the binary tree), and the data String_Ptr is added to the Vector associated with that “key”. Look-up by “key” yields a Vector of all of the String_Ptr that have been added to the Vector associated with that key.
Your Hangman program, then, should do the following (which you will do part of it in this
1.Prompt the user for a word length, re-prompting as necessary until she enters a number such that there’s at least one word that’s exactly that long. That is, if the user wants to play with words of length -42 or 137, since no English words are that long, you should reprompt her.
2.Prompt the user for a number of guesses, which must be an integer greater than zero. Don’t worry about unusually large numbers of guesses – after all, having more than
26 guesses is clearly not going to help your opponent!
3.Prompt the user for whether she wants to have a running total of the number of words remaining in the word list. This completely ruins the illusion of a fair game that you’ll be cultivating, but it’s quite useful for testing (and grading!)
TA CHECKPOINT 2: Demonstrate to your TA that you program can correctly do the above steps. You should show how you program does not accept invalid numbers for the word length and guesses. You are only required to do till point number 3 above in this lab. The remaining points will be done in later labs.
4. Play a game of Hangman using the Evil Hangman algorithm, as described below:
g. 5. Ask if
a. Select the Vector of all words in the English language whose length matches the input length.
b.Print out how many guesses the user has remaining, along with any letters the player has guessed and the current blanked-out version of the word. If the user chose earlier to see the number of words remaining, print that out too.
c.Prompt the user for a single letter guess, re-prompting until the user enters a letter that she hasn’t guessed yet. Make sure that the input is exactly one character long and that it’s a letter of the alphabet.
d.Partition the words in the dictionary into groups by word family. The word family is the “key” in the AssociativeArray, and the words (already contained in String_Ptrs from having been read into the Array of Vectors of String_Ptrs initially), are added to the Vector in the AssociativeArray which is associated with the given “key” word family.
e.Find the most common “word family” in the remaining words, remove all words from the word list that aren’t in that family, and report the position of the letters (if any) to the user. If the word family doesn’t contain any copies of the letter, subtract a remaining guess from the user.
f.If the player has run out of guesses, pick a word from the word list and display it as the word that the computer initially “chose.”
g.If the player correctly guesses the word, congratulate her.
the user wants to play again and loop accordingly.
Advice, Tips, and Tricks
Watch out for gaps in the dictionary. When the user specifies a word length, you will need to
check that there are indeed words of that length in the dictionary. You might initially assume that if the requested word length is less than the length of the longest word in the dictionary, there must be some word of that length. Unfortunately, the dictionary contains a few “gaps.” The longest word in the dictionary has length 29, but there are no words of length 27 or 26. Be sure to take this into account when checking if a word length is valid.