You are to create a class that will complete a program to play Hangman. You are given the main program and are to write a supporting class with the interface described below. The finished program provides an interactive hangman program using the text interface.
You are to create a class hangman which provides the record-keeping for a game of hangman. Do not place your class in the file where the main program is. Put it in a file named hang.h, which is included by the main program. You should also create a file hang.cpp which contains the bodies of the larger methods. Make sure your .h file follows the #ifndef/#define/#endif pattern discussed in class.
When creating a class in a .h file, it is usual practice to place the bodies of larger methods into an associated .cpp file. Larger usually means more that two or three lines of code. It is my requirement for this project that you put any body larger than five lines into a .cpp file, and at least one in any case.
These are the required public methods for your class. You may also provide any private methods you wish. Except for the constructor, each method is shown as called on some object of type hangman denoted h.
As the game progresses, the hangman object h will pass through several states. The methods change the object's state, and are affected by it. When created, but before the first call to set_word, the object is in the initial state, where it is basically useless. In this state, the method guess should not be called, and will return an error if so. All get methods which return integers return zero, and those which return strings return the empty string. Call set_word to move the object into the guessing state, when letters may be guessed (and the object is generally good for something). The object does not return to the initial state. It stays in guessing until the player either wins or loses, that is, either the word is guessed or there are six wrong guesses. Then it enters the complete state, and will stay there until the next call to set_word, when it again enters the guessing state. When entering the guessing state for some word, all the letters of that word are marked un-gessed. As guess is called during the guessing state, some letters become marked guessed.
When a word is guessed correctly, that is a success, and when the sixth wrong letter is guessed, that is a failure. The object must count successes and failures.
This method sets the word to be guessed to word. Each letter in word is marked un-guessed, and no letters have been guessed. Any non-alphabetic characters in word are removed, and any upper-case characters changed to lower case.
The object h will be in the guessing state after this call. If it was was already in the guessing state when set_word is called (didn't change from initial or complete), then assume the user gave up on the previous word being guessed, and count it as a failure.
The word should not be empty (after removing non-alphabetic characters). If it is, the call should be ignored, and have no effect at all.
| Return | |
|---|---|
| ERROR | The object is not in the guessing state. |
| BADCHR | ch is not a letter (digit or special character). |
| DUP | ch has already been guessed (since the last set_word). |
| Return | When, since the last set_word |
|---|---|
| WIN | ch was the last unguessed character in the word. |
| LOSE | ch was the sixth wrong guess. |
| FOUND | ch successfully guesses a letter, but there are more (game continues). |
| MISSED | ch was a wrong guess, but not the sixth (game continues). |
Here is a sample run. Please note: you are not responsible to code output. The downloaded main does all the printing and the stupid ASCII graphics. You should not be printing in your class.