Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


CE221- C++ Programming

Assignment 2- 21/22 academic year

 

Introduction

This assignment comprises of two exercises. Files for use in this assignment are available on Moodle in a zip file. Submission ofthis assignment will be via FASER.

 In this assignment, you are asked to write a hangman game that: 

1)  Provides three levels of difficulty: easy, medium, and high.

2)  Provides two different sets of users: a) the player: those who will play the game. B) the managers: those who can adapt to the levels of difficulty in the game.

3)  Displays the top 10 highest scores and the user statistics.

4)  Provides tips for the gamers.

At this time, I will set the output format flexible. However, the gamer must have the following options:

1) Play game

2) Your statistics

3) Display top 10

4) Exit game

For managers, the following options should appear:

1) Reset game

2) Change game levels

3) Add another manager

4) Set the word file

5) Exit game

We have then 2 exercises that are about how the game works, and how to generate random words and tips for the gamers.

 

Exercise 1 (35%)

The hangman game (30%)

Within the folder ex1 in the zip file, you will find files called Person.h , Person.cpp, Gamer.h and Manager.h. These files provide a class to hold details of a person, and declarations for a derived class to hold details of a gamer and a manager. Such classes would generally be expected to have more data members but only those that are relevant to this assignment have been declared.

The Person class is complete, but you may, ifyou wish, add extra member functions to the class, e.g. an operator string method; BUTyou should not change any of the existing material, except where noted in this brief.

The  Gamer  class  and  Manager  class  declaration  simply  provide  declarations  for  member functions  that must be  implemented; you  must provide  complete  implementation for  these functions  in a file called Gamer.cpp and Manager.cpp, respectively. The behaviour of the functions should be as specified in the comments supplied in the header file. Again you may, if you wish, add extra member functions to the class (e.g. a method to calculate the average scores); ifyou do so the function bodies should be written in the .cpp file.

In a separate file, you should create your main function. You should then ask the username and identify what type of user he is, and direct to their respective options. Managers and gamers should be identifiable by a mechanism ofyour choice (here you can modify the existing one). It can be, as an example, name, or a given registration number. For gamers, if the name does not match to a previous name, then a new gamer is created, and a message should be output to the screen.  Create a template function login that works for both Managers and Gamers that attempt to open and read from a text file, whose name should be defined in the program, and in that file contains information for both managers and gamers. If the info of a particular line of that file is for a Gamer, it will contain the registration number and name and scores of a Gamer (in the order of easy, medium and high); these should be passed as arguments to the Gamer constructor to create a new gamer object, which should be added to a collection ofGamers. This collection may be a vector, a list or a set (from the standard template library).  If the info of a particular line of that file is for a Manager, it will contain a registration number and a manager name. You should create an object of the class Manager that holds who will be the Manager this time. Only an existing manager should add another manager, and the first manager should be you.

For the hangman game itself, you are shown a set of blank letters that match a word, and you must guess what these letters are to reveal the hidden word. You guess by typing letters are inputs – as you should check if the user attempts to input a word. If you pick a letter that is in the word, a letter is revealed from the blank letters; however, if you pick a letter that is not in the word, then a stickman is slowly drawn. With each wrong letter guess, the man is drawn more and more. When the man is finished, he is hung, and the game is lost. Therefore, the game is called 'Hangman'. If you can reveal all the letters in the word before the man is hung then you are successful, and the full word is revealed along with an image showing the meaning ofthe word.

The following example game illustrates a gamer trying to guess the word hangman using a strategy based solely on letter frequency. As the player continues, a part ofthe stick figure on the noose  is  added.  Once  a  full  body  is  drawn,  the  game  is  over,  and  the  player  lost.

The guessing player has lost this game as the diagram had been completed before all the letters were guessed. You are not required to display the stickman as shown; you may choose how to

display  the  word guess  and misses  but you  must  inform  the  user  the state  of  the stickman

somehow.

For the levels of difficulty, the following rules apply

Easy -> words with size between 1-5

Medium -> words with size between 6-10

Hard -> words with size 10 and above

 

These levels must be asked to the gamer right before the game itself starts (whether they want to play on the Easy, Medium or Hard modes).

Obtaining words will be explained in exercise 2.

 

The scores of the game should be defined by you. However, you will have three scores: one that is per game, but also an accumulated score and an average score. These should be presented to the Gamer when they wish to see their statistics The accumulated score is the sum of all scores a gamer has obtained. The average score should be separated by game levels and must display the mean score that a user has achieved per level. These scores must be frequently updated.

 

The manager can then select to change the range of each level of difficulty. They can change either the minimum  or/and the maximum values.  The minimum  and maximum  should be displayed to the Gamer when asking for a guess.

For the top 10 options, you must create an additional function in the main file. The function should print on the screen the highest top 10 scores for each difficulty level, but the output must highlight the score from that registration number. How you highlight the score is up to you. This will make sure we know where the user is in the top 10 if it really is in there. However, you must here use the STL sort algorithm to obtain the vector ofgamers with the highest score to the lowest, and only must display the first 10 elements ofthe vector. You can use more than one vector.

The main function must be controlled by a loop that reads the options from the user and guides the actions in the program itself. The loop must end when the user selects the “exit” option.

Challenge (5%)

The collection of Gamers now should be changed to a custom iterable container that operates similarly to a linked list. The top 10 now must combine the sort algorithm and the custom iterator for this class, and you must use iterators to display the top 10 as well.

 

Exercise 2 (35%)

Selecting random words and tips for the gamers (35%)

The game must provide tips to the user so that the word is guessed correctly. This tip consists of showing the word in a phrase, but of course, omitting the word. For example, if a word to guess is LOVE the tip should look like

Tip: “And Maria knew why her family helped her so much, it was

because they XXXX her”

Ifa gamer has already guessed a letter the tip should display such letters in the word. For example, if the gamer has guessed L and E, the tip should look like

Tip: “And Maria knew why her family helped her so much, it was

because they LXXE her”

Ifthere is more than one letter in the tip, it also should be treated in the same manner as explained above.

The program should read the name of the text file to be used that contains words and phrases. The program should terminate cleanly with an appropriate error message ifany of the files cannot be opened. A file containing a book is found in the zip file. You may use this book to get words randomly but also get the phrases containing the words. All you need to do is search in the book file. This will make your life way easier. However, you may produce your own file if you wish (but I will use the book to test your software).

For this  assignment,  a  “word”  should be regarded  as being  a  sequence  of non-whitespace characters containing more than one letter. Any punctuation at the beginning or end of a word should be removed (note that there may be more than once such punctuation symbol); punctuation symbols in the middle of a word and any digits are to be regarded as part ofthe word. You must use the function ispunct, defined in the header file cctype, to check if a character is a punctuation. All input should be converted to lower case, so ”IT”, ”It”, and ”it.” should all be converted to ”it”.

Your program must contain a class called ReadWords, each with a .cpp and a .h file. The header file for the ReadWords class and a partial implementation are provided in the ex2 folder in the zip file; you must use these and must not make any changes to the header file other than the required changes to the comments. Changes to the provided code in the cpp file are permitted.

You must then randomly select a word based on their size values that define the game level, in the sample file. Store words in the set named words. Once you know that word, you must search for phrases containing those words and store them into the phrases set of the ReadWords class. You must select a total of 10 words and 5 phrases.

It is recommended that you use the array words when searching for phrases; a simple input loop like that used in lab 4 (but inputting strings instead of numbers) will be sufficient for this – but

you must extend it so that phrases can be also obtained.

Challenge (5%)

Now you can help the Gamer even further by creating a list of possible words to be used by them. For example, words matching 't _ t _ _' are [titan, tithe, title, titus, total, tutor] and words matching '_ _ _ t t' are [brett, burtt, hiatt, knott, pratt, scott, wyatt]. The user then can have a clue of what types of words are expected. BUT, only help the user when he is getting closer to losing the game. You should create a new function for this, that returns an STL set containing the words to be used in helping the user.

 

Deliverables

You should submit a single zip file containing a single folder. The only file formats acceptable are .zip , .7z or a linux gzipped tar file. If you submit a file in any other format, you will lose 10% ofthe marks for this assignment.

This folder should contain a MAKE file, necessary for the compilation of the application. Also, you need to include all files necessary to make your program executable.

 

Marking Scheme

10% of the marks for this assignment will be awarded for programming style, 10% for executable file testing and 10% for structure/comments. If your code is submitted with compilation errors, you will automatically lose these marks.

Issues  that  may  lose  marks  for  efficiency  could  include  unnecessary  copying  of objects, unnecessary repetition of code. Your comments should say precisely what each function does and what each non-local variable represents; within function bodies, you should use only brief comments to say what groups oflines do – you must not say what every statement does.

The total mark available for style and comments will be proportional to the amount of the assignment that you have attempted, e.g. if you attempt only about one exercise the mark available for style and comments would be about 10%.

Ofthe remaining 70%, 35 marks will be awarded for exercise 1 and 35 for exercise 2.


I must compile the code using the MAKE file and execute the application for testing. Marks will be deducted from the remaining 70% if:

1)  The MAKE file does not compile the application properly. (5 marks)

2)  The executable file does not pass the functioning test. (5 marks)

3)  The implementation of the application does not follow specified instructions. (Remainder of the marks)

Needless to say, but: use the STL as much as you like but wisely. You should use as much as you like!