CSCI1120 Introduction to Computing Using C++

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.


Figure 1: Drawing Sequence of the Hangman Diagram

Program Specification

You shall write your program in two source files Hangman.cpp and playgame.cpp. The former is the implementation of the class Hangman, while the latter is a client program of class Hangman which performs the game play. It is recommended that you finish the Hangman class first beforewriting the client program. When you write the Hangman class, implement the member functionsand test them individually one by one. Your two files will be graded separately, so you should not mixthe functionalities of the two files.

Class Hangman (Hangman.cpp)

You are given the interface of the Hangman class in the header file Hangman.h. You shall not modify the contents of this header file. Descriptions of the class are given below.
class Hangman {
public:
Hangman(string fname, unsigned s);
int getWon() const;
int getLost() const;
string getSecretWord() const;
void printGame() const;
int guessOneLetter(char ch);
void restart();
private:
void importDictionary(string fname);
vector<string> dictionary;
int won, lost;
string secretWord;
string wordToGuess;
vector<char> guessedLetters;
int chances;
};

Private Data Members

vector<string> dictionary;

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;

A Hangman object can be used to played multiple Hangman games. These two data members storethe number of games that were won and lost respectively since creating the object. string secretWord;
A string storing the secret word of the current game.

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.

int chances;

The number of remaining chances in the current game. Initially, there are six chances.

Constructor and Member Functions

Hangman(string fname, unsigned s);

This constructor creates and initializes a Hangman object. The parameter fname denotes the name of a dictionary file containing a pool of words for the game. A few sample dictionary files are provided for your development and testing. You can assume that all words in the dictionary consist of lowercase letters only. The parameter s is the random seed for initializing the random number generator. To initialize the Hangman object, the following tasks shall be carried out.

1. The parameter dictionary file shall first be imported to the data member vector dictionary, using the importDictionary() member function to be described later.

2. The data members won and lost shall be initialized to 0. (No games have been won or lost at the beginning.)

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.

int getWon();
int getLost();
Returns the number of games that were won and lost so far resectively.
string getSecretWord();

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.

void printGame();

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.)

void restart();

This member function starts/restarts a fresh Hangman game by performing the following tasks.

1. Generate a random integer k in the range 0 … n − 1 (inclusive) using k = rand() % n, where n is the number of words in the dictionary. (Do NOT use srand() here! Just use rand().)
2. Set the secret word as the word in index k of the dictionary vector (dictionary.at(k)).
3. Set the word to guess as all underscores ‘_’ with the same length as the secret word. E.g.,
suppose the secret word is “boy”m then the word to guess shall be “___” (three underscores).
4. Set the guessedLetters vector as size 0. (No letters are guessed when a game starts.)
5. Set the number of remaining chances as 6.

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.

Client Program (playgame.cpp)

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.

  1. 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.
  2. Create a Hangman object from the input file name and seed.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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

In the following sample run, the blue text is user input and the other text is the program output. You can try the provided sample program for other input. Your program output shall be exactly the same as the sample program (same text, symbols, letter case, spacings, etc.). Note that there is a space after the ‘?’ and ‘:’ in the program printout. This sample run is applicable to Visual Studio only. Additional sample runs applicable to Visual Studio and Xcode are provided in Blackboard.


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.

发表评论

电子邮件地址不会被公开。 必填项已用*标注