Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due
CSCI1120 Introduction to Computing Using C++, Fall 2024/25
Department of Computer Science and Engineering, The Chinese University of Hong Kong
Introduction
The objective of this assignment is to practice object-oriented programming. You will write a class and a client program to play a word guessing game called Hangman.
In a Hangman game, there is a secret word and a player tries to guess the word by suggesting letters. The word to guess is initially a sequence of underscores ‘_’, revealing the number of letters of the secret word. If the player guesses a letter that occurs in the word, then all occurrences of the letter would be unveiled in the correct positions. If the guessed letter does not occur in the word, one stroke of the hangman diagram is drawn. The diagram will be completed with six strokes. Figure 1 shows the drawing sequence of the hangman diagram. The player wins if she unveils all letters of the secret word before the hangman diagram is completed. You will implement a class called Hangman and then a client program that plays the game.
Program Specification
Class Hangman (Hangman.cpp)
Private Data Members
A vector of strings storing a number of words. The secret word of a Hangman game will be drawn randomly from this dictionary vector.
int won, lost;
string wordToGuess;
A string storing the current state of the word to guess. Initially, it consists of l underscores ‘_’, where l is the length of the secret word. Throughout a game, the correctly guessed letters will be gradually unveiled in this string.
vector<char> guessedLetters;
A vector storing all the letters that are already guessed in the current game. Initially, the vector contains no letters at all. Throughout the game, letters guessed by the player will be gradually added to the vector according to the guess order.
The number of remaining chances in the current game. Initially, there are six chances.
Constructor and Member Functions
Hangman(string fname, unsigned s);
1. The parameter dictionary file shall first be imported to the data member vector dictionary, using the importDictionary() member function to be described later.
3. The random number generator shall be initialized using the random seed s. (srand(s))
4. Start a fresh Hangman game by initializing the secret word, word to guess, guessed letters, and remaining chances, using the restart() member function to be described later.
Returns the secret word of the current game if the secret word is already correctly guessed or the number of remaining chances is 0. Returns the empty string “” otherwise. Note that this member function does not simply return the secret word, to avoid exposing the answer to clients easily.
Prints the state of the current Hangman game, which consists of (1) the hangman diagram, (2) the word to guess, (3) all the failed letters (that is, all previously guessed letters except the successfully guessed letters), and (4) the number of remaining chances. The following shows one example printing.
The failed letters are printed in their guessing order. That is, the earlier a letter is failed, the earlier it is printed. They are not necessarily in alphabetical order.
The format of the hangman diagram depends on the number of remaining chances and is shown below.
int guessOneLetter(char ch);
Performs the player action of guessing the letter ch as follows. If the letter ch was already guessed before, this member function shall perform nothing and return 2. (See return value below.)
Otherwise, it shall mark the letter ch as guessed, by adding ch to the end of the vector guessedLetters, and unveil all occurrences of ch in the word to guess. E.g., suppose the secret word is “absorb” and the word to guess is “a_s_r_”. Then, guessing letter ‘b’ shall update the word to guess to become “abs_rb”. If there is no occurrence of ch in the secret word, this is a failed guess and the number of remaining chances shall decrease by one. After guessing letter ch, this member function shall return an integer depending on the following possibilities:
- Return 0 if there is no more chance for the player. The data member lost shall also be increased by one.
- Return 1 if the player has completed the word to guess successfully. The data member won shall also be increased by one.
- Return 2 if ch was already previously guessed.
- Return 3 otherwise.
In this member function, you can assume that parameter ch is always a lowercase letter. (It will be the caller’s responsibility to validate this.)
This member function starts/restarts a fresh Hangman game by performing the following tasks.
Note: The random number sequences generated using Visual Studio and Xcode are different due to different library implementations. Even for the same program and same seed, the two platforms may generate different sets of random integers.
(Provided) void importDictionary(string fname);
This is a private member function written for you already and you shall not modify its contents. The parameter fname is the name of a dictionary file containing a pool of words for the game. The member function reads all the words from the file (as strings) and add them at the end of the datamember vector dictionary one by one. At the end of the function, the words would be stored atindices 0 to n − 1 of dictionary, where n is the number of words in the file. In case the file cannot be opened for reading, the program would terminate.
Your main program is a client of the Hangman class. You create a Hangman object here and call its member functions to implement the following program flow.
- The program starts with prompting the user to enter a string and an integer for a data file name and a random seed respectively. You can assume that the file name contains no space characters,and the integer is non-negative.
- Create a Hangman object from the input file name and seed.
- Print the game state and prompt the user to make a letter guess. You can assume that the user always enters a character for this input. But when the user enters a non-letter (all charactersother than a–z and A–Z), the program shall give a warning message and prompt the user to enteragain repeatedly until the input is a letter.
- The input letter is converted to lowercase if needed. (That is, typing, say, A or a have the sameeffect.) The letter is then used to make a guess. If the letter was already guessed in previouslyrounds, print an alert message.
- Repeat steps 3 and 4 until the game is over, that is, either the user completes the word to guess or there is no more remaining chance.
- When a game is over, print (1) the final game state, (2) a winning (“Bingo!”) or losing (“Oh no!”)message, and (3) also the secret word.
- Then prompt and ask the user if she wants to start another game. You can assume that this inputis always a character. If the user enters Y or y, then restart a new game and go back to step 3.
- Any other input means no new game is needed. Finally, print (1) the number of games played, (2)the number of games won, and (3) the number of games lost.
Some Points to Note
- ➢ You cannot declare any global variables in all your source files (except const ones).
- Remember to #include "Hangman.h" in both Hangman.cpp and playgame.cpp.
- You can write extra functions in any source files if necessary. However, extra member functions (instance methods), no matter private or public, are not allowed.
- Your Hangman class shall not contain any cin statements. All user inputs shall be done in the client program (playgame.cpp) only.
- The Hangman class shall not contain any cout statements except in the printGame() and importDictionary() member functions. You have cout statements in the client program playgame.cpp also.
Sample Run
Submission and Marking
- Your program file names should be Hangman.cpp and playgame.cpp. Submit the two files in Blackboard. You do not have to submit Hangman.h.
- Insert your name, student ID, and e-mail as comments at the beginning of all your files.
- Besides the above information, your program shall include suitable comments as documentationin all your files.
- You can submit your assignment multiple times. Only the latest submission counts.
- Your program should be free of compilation errors and warnings.
- Do NOT share your work to others and do NOT plagiarize. Those who shared their work and/or plagiarized others work shall be penalized.