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.
Create a class called hangman which performs the record-keeping for a hangman game. It keeps track of a word being guessed, which letters in that word have been successfully guessed, and which incorrect letters (letters not in the word) have been guessed. An attempt to guess a word is a round, and the round ends successfully if the word is guessed. It ends unsuccessfully after six incorrect letter guesses.
The object must keep track of the letters guessed within each round, and which were correct. It must also keep track of the total numbers of successful and unsuccessful rounds.
As the game progresses, the hangman object h will pass through several states. The methods change the object's state, and are effected by it. When created, but before the first call to set_word, the object is in the initial state. In this state, there is no word to guess, so the guess method 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. When set_word is called, that specifies a word to guess and starts a round. The object then enters the guessing state. It stays there until the the round ends (successfully or otherwise), when it enters the complete state, and will stay there until the next call to set_word, when it reenters the guessing state.
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, with appropriate parameters, so it looks more like a call than a declaration.
This method sets the word to be guessed to the string word and starts a new round. Each letter in word is marked un-guessed, and there are no incorrect letter guesses. 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 already in the guessing state when set_word is called, count that as an unsuccessful round, since it never reached completion.
The word should be non-empty after removing non-alphabetic characters. If it isn't, the call should be ignored, and have no effect at all.
|WIN||ch was the last un-guessed character in the word.|
|LOSE||ch was the sixth incorrect letter guess.|
|FOUND||ch was an un-guessed character in the word, but some remain.|
|MISSED||ch was an incorrect guess, but not the sixth.|
|DUP||ch has already been guessed.|
|BADCHR||ch is not a letter.|
|ERROR||The object is not in the guessing state.|
You are to create your class hangman in a file named hang.h, since this is the name the given main program expects. 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 thee lines into a .cpp file, and at least one in any case.
The basic commands to compile this two-part program would be
You may keep track of the needed values any way which works well for you. Of course, you will probably want to use a string for the current word, and integers for the various counts. I used a set to keep track of which characters had been guessed during a round, and a vector of bool to keep track of which characters in the current word have been marked guessed.
You might start by creating a simple class which just returns constants for each operation, and getting that to compile. Then start implementing the methods correctly.