Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This programming projects asks you to develop a hangman game which has the follo

ID: 3867572 • Letter: T

Question

This programming projects asks you to develop a hangman game which has the following characteristics: IN C++

1. It is driven by a while loop from main that allows the user to play the game as many times as they wish.

2. Validates ALL user input. (each character input for the word guess must be an alpha, 'a' is the same as 'A', all blank spaces ignored, 'y' = 'Y' = ' y ', etc)

3. A list of at least 20 words for the game are kept in a file that is read in at the time the program starts. These words are then stored in an array. Use a random number generator to access into the array to select the secret word. Don't repeat words during one run of the program. (What type of C++ structure would best hold this? Parallel arrays? An array of structures?)

4. Once each game begins, show the gallows, and indicate the number of characters in the word, like this:

______

|   

|

|

____________ Word : _ _ _ _ _ _ _

That's just a suggestion as to how it may look. Actually, I am sure that you can do better than that.

For each wrong guess, draw a body part on the gallows and show it in a list of incorrect guesses. For each correct guess, fill in one of the blanks in the word.

______

| ( )

|

|

____________ Word : a _ _ _ _ _ Incorrect guesses: p

(this pictures shows 1 wrong guess and 1 correct guess).

5. The maximum number of moves before the person is hung would be 8 or 9, depending on your ability to draw head, neck, body, 2 legs, 2 arms...when the man is hung, the game is over. Or, the game is over when the user correctly guesses each letter in the word.

6. This program must have good modular design - using proper functions.

7. Pseudocode is absolutely required and I hope that you might even use it to design the code.

Explanation / Answer

Solution: See the code below:

-----------------------------------------------------------------

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <algorithm>
#include <cstring>

using namespace std;

int main() {
   char choice = 'y'; //choice whether to continue or not
   string wordlist_path = "./"; //path to file containing words. change it accordingly.
   string wordlist_filename = "wordlist.txt"; //name of file containing words. change it accordingly.
   string path = wordlist_path + wordlist_filename; //full path to file

   ifstream infile(path.c_str()); //handle for file
   //check if file can be opened or read.
   if (!infile) {
       cerr << "Error! Cannot open or read file. Aborting..." << endl;
       return EXIT_FAILURE;
   }
   string *words; //array to store words
   int num_words; //number of words in file
   //count words in file
   num_words = count(std::istreambuf_iterator<char>(infile),
           std::istreambuf_iterator<char>(), ' ') + 1;
   //cout<<num_words<<endl;
   words = new string[num_words]; //allocate array for words
   string line;
   //set file stream again to first line
   infile.clear();
   infile.seekg(0, ios::beg);
   int i = 0; //counter variable

   //read words into array from file
   while (getline(infile, line)) {
       words[i] = line;
       i++;
   }

   string secret_word = ""; //secret word
   int secret_word_index; //location of secret word in array

   //loop to play hangman game
   while (choice == 'y') {
       secret_word_index = (int) (rand() % num_words + 1); //generate a random number
       secret_word = words[secret_word_index]; //set secret word
       //cout<<secret_word<<endl;
       int chars_to_guess = strlen(secret_word.c_str()); //number of character to guess
       //cout<<chars_to_guess<<endl;
       //display gallows
       cout << " ___________" << endl;
       cout << " |" << endl;
       cout << " |" << endl;
       cout << " |" << endl;
       cout << "_______________" << endl;
       cout << "Word:";
       for (int i = 1; i <= chars_to_guess; i++) {
           cout << " _ ";
       }
       cout << endl;
       int max_tries = 8; //maximum number of tries
       char guess;
       int position_to_guess = 0;
       int num_incorrect_guess = 0;
       char *guessed_chars = new char[chars_to_guess];
       //loop to perform guesses
       for (int i = 1; i <= max_tries; i++) {
           //if all characters guessed properly
           if (chars_to_guess == 0) {
               cout << "Bravo! You guessed the word correctly." << endl;
               break;
           }
           //Enter character to guess
           cout << "Enter a char:";
           cin >> guess;
           //check character being guessed
           if (guess == secret_word.at(position_to_guess)) {
               //display latest status
               guessed_chars[position_to_guess] = guess;
               for (int i = 0; i <= position_to_guess; i++) {
                   cout << guessed_chars[i] << " ";
               }
               chars_to_guess--;
               position_to_guess++;
               for (int i = 1; i <= chars_to_guess; i++) {
                   cout << " _ ";
               }
               cout << endl;
           } else //if guessed incorrectly
           {
               cout << "Incorrect guess:" << guess << endl;
               num_incorrect_guess++;
               //display state of gallows
               if (num_incorrect_guess == max_tries) {
                   cout << "Limit reached!" << endl;
                   break;
               }
               cout << " ___________" << endl;
               for (int i = 1; i <= num_incorrect_guess; i++) {
                   cout << " |()" << endl;
               }
               cout << " |" << endl;
               cout << " |" << endl;
               cout << "_______________" << endl;
           }
       }
       cout << "Do you wish to continue?(y/n)";
       cin >> choice;
   }
   infile.close(); //closes file
   delete words; //delete words array
   return 0;
}

-----------------------------------------------

Sample wordlist:

--------------------------------------------------

hello
father
mother
sun
son
complete
today
welcome
meet
country
county
google
apple
internet
phone
quite
quiet
continue
marshall
garment

---------------------------------------

Output:

------------------------------------------------

    ___________
   |
   |
   |
_______________
Word: _ _ _ _ _ _
Enter a char:m
m _ _ _ _ _
Enter a char:o
m o _ _ _ _
Enter a char:t
m o t _ _ _
Enter a char:e
Incorrect guess:e
   ___________
   |()
   |
   |
_______________
Enter a char:h
m o t h _ _
Enter a char:r
Incorrect guess:r
   ___________
   |()
   |()
   |
   |
_______________
Enter a char:e
m o t h e _
Enter a char:r
m o t h e r
Bravo! You guessed the word correctly.
Do you wish to continue?(y/n)n

---------------------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote