Have you heard of the game of Hangman Well you are going to

Have you heard of the game of Hangman? Well, you are going to write a program that will allow you to play a game of Hangman against the computer. After you play, you will be asked if you wish to play a brand new game, and if so, the program will start all over again. Here\'s how the program should work.

You will need 2 classes: a HangmanGame (driver) class and a Hangman (domain) class.

In the HangmanGame - driver class:

1. At the very beginning and after each game of Hangman, your program will display a menu that looks like this:

Do you wish to play a game of Hangman? (enter \"yes\" or \"no\")

If the user replies \"no,\" the program will end immediately.


2. Once the user replies \"yes,\" the driver will read a small file of 5 words, and will pass them as values to the constructor of the Hangman, when it instantiates a new Hangman - domain object.  In the constructor of the Hangman, the 5 values will be stored in 5 different variables. There is a method called selectSecretWord() which will be called to generate a random number between 1 and 5, used to select one of five words as the \"secret word\" for this game. The secret word will be available with a getter method of the Hangman object.   

3.)Back in the driver class, using a getter method from the Hangman object, the program will count the number of letters in the word, and display that many underscores on the screen, looking like this:

Here is the word for you to guess: _ _ _ _ _ _ _ (one underscore for each letter in the word)
You have x-number of guesses before you lose.  (x-number is calculated as the number of letters in the word times 3).

What is your next letter guess? (enter 1 letter only)

3.) Upon the user entering a letter, your program will check if the letter exists in the secret word (hint: use the String method indexOf(String str, int fromIndex) ). For every location where the letter was found in the word, put the letter in place of the underscore. Each time the user enters a letter, subtract 1 from the total number of guesses left. Keep displaying the same prompt as before, with the updates of the letters in the secret word and the count of guesses decrementing.

Here is the word for you to guess: _ a _ _ _ e _ (one underscore for each letter in the word).

You have X-number of guesses left before you lose.  (x-number is calculated as the number of letters in the word times 3).

What is your letter guess? (enter 1 letter only)

4.) The game is over when either the number of guesses left is equal to 0, or when all the letters in the secret word are correctly guessed. Display one of the following two messages, depending on whether the user guessed the word correctly or not:

You correctly guessed the word. Congratulations!

or

Sorry, you did not guess the word. Better luck next time.

5.) Then, display the original menu again, and if the user answers \"yes\" start all over again.

Do you wish to play a game of Hangman? (enter \"yes\" or \"no\")

If the user replies \"no,\" the program will end immediately. If the user replies \"yes,\" the program will loop again, calling the selectSecretWord() method of the Hangman domain class, to reset the secret word. Then, you will loop to step 3 above, and will start the guessing game all over again with the new word.

6.) Extra credit #1: Keep track of how many games of Hangman were won by the user out of the total number of games played.

7.) Extra credit #2: Write to an output file, with the secret word, and the number of guesses it took the user to win. The file\'s content should look like this:

Secret Word: Graduation Number of Guesses: 25

The output file should be appended to each time the program runs. It should not be overwritten.

The following message would be displayed when the user no longer wishes to keep playing:

You have won x out of y games of Hangman. Keep playing! (where x is the total number of times the secret word was guessed correctly, and y is the total number of games played.)

Solution

#include <iostream>

#include <fstream>

#include <string>

#include <ctime>

#include <cstdlib>

using namespace std;

int           instructions();

void        manual();

void        file( char* );

int           letterFill( char, char*, char* );

void        Unknown( char*, char* );

const int MAX_LENGTH=10;

void main()                                                                                                                                                                                                                                                                                                                                           

{

                bool done = false;

                char word[MAX_LENGTH];

                char unknown[MAX_LENGTH];

                char letter;

                char name[MAX_LENGTH];

                int wrong_guesses=0;

                int MAX_TRIES;

               

                // SWITCH: MANUAL vs. SOURCE FILE

    do

  {

        switch(instructions())

        {

            case 1:

            {

                manual();

                break;

            }

            case 2:

            {

                file(word);

                break;

            }

            default:

            {

                done = true;

                break;

            }

        }

    }  

                while(!done);

               

                cout << \"INPUT NUMBER OF GUESSES THE PLAYER IS ALLOWED: \" << endl;

                cin >> MAX_TRIES;

                Unknown(word, unknown);

                cout << endl << endl << \"HANGMAN\";

                cout << endl << endl << \"Each letter is represented by a star.\" << endl;

                cout << \"You have \" << MAX_TRIES << \" tries to try and guess the word.\";

                cout << \"ENTER GUESS WHEN READY\";

               

                while (letter !=\'N\' && letter !=\'n\')

                {

  // LOOP UNTIL GUESSES USED UP

                while (wrong_guesses < MAX_TRIES)

    {

     

      // DISPLAY UNKNOWN WORD

      cout << unknown << endl;

      cout << \"Guess a letter: \" << flush;

      cin >> letter;

     

      // REPLACE * W/ LETTER IF CORRECT,

      // INCREMENT WRONG GUESSES IF INCORRECT.

      if (letterFill(letter, word, unknown)==0)

        {

          cout << endl << \"Uh Oh! That\'s ONE Guess down!\" << endl;

              wrong_guesses++;

        }

                else

                                {

                                cout << endl << \"YAY! That letter is in the word\" << endl;

                                }        

     

      // GUESSES LEFT

                cout << \"Guesses Left: \" << MAX_TRIES-wrong_guesses << endl;

         

      // GUESSED WORD?

      if (strcmp(word, unknown) == 0)

        {

          cout << word << endl;

          cout << \"Yeah! You got it!\" << endl;

          exit(0);

        }

                cout << \"Ouch, you\'ve been hanged.\" << endl;

                cout << \"The word was : \" << word << endl;

                }

                }

system (“pause”);

}

// PROGRAM MENU

int instructions()  

{

    int select = 0;

                cout << endl << \"HANGMAN\" << endl << endl;

                cout << \"     PROGRAM MENU\" << endl;

                cout << \" Select option 1 or 2\" << endl << endl;

                cout << \"      1. INPUT WORD MANUALLY\" << endl;

                cout << \"      2. PLAY AGAINST THE COMPUTER\" << endl;

                cout << \"      3. EXIT PROGRAM BY INPUTING: N or n\" << endl << endl;

                cin >> select;

    return select;

}

//WORD FROM USER INPUT

void manual()

{

    string word;

   

    cout << endl << \"INPUT WORD: \" << endl;

    cin >> word;

}

void file(char *roc)

{

                ifstream fin(\"G:/wordsource.txt\");

                int x;

                int count=1;

                int word;

                int i = 0;

// INITIALIZE RANDOM GENERATOR

srand(time(0));

// RANDOM NUMBER GENERATOR

word = rand() % 20;

// MOVE TO CORRECT PLACE IN FILE

while (count < word)

    {

      fin >> x;

      if (x==0)

        {

          count++;

        }

    }

// READ IN WORD

do

{

    fin >> x;

    roc[i++] = char (x);

}

while (x);

}

int letterFill( char guess, char *secretword, char *guessword )

{

int i;

int matches=0;

for( i=0; i<MAX_LENGTH; i++ )

    {

      // END OF WORD

      if( secretword[i] == 0 )

        {

          break;

        }

     

      // GUESSED SAME LETTER TWICE

      if( guess==guessword[i] )

        {

          return 0;

        }

      // MATCH GUESS TO SECRET WORD

      if( guess==secretword[i] )

        {

          guessword[i] = guess;

          matches++;

        }

    }

return matches;

}

// INITIALIZE UNKNOWN WORD

void Unknown( char *word, char *unknown)

{

int i;

int length=strlen(word);

for( i=0; i<length; i++ )

    {

      unknown[i]=\'*\';

    }

unknown[i]=0;

}

Have you heard of the game of Hangman? Well, you are going to write a program that will allow you to play a game of Hangman against the computer. After you play
Have you heard of the game of Hangman? Well, you are going to write a program that will allow you to play a game of Hangman against the computer. After you play
Have you heard of the game of Hangman? Well, you are going to write a program that will allow you to play a game of Hangman against the computer. After you play
Have you heard of the game of Hangman? Well, you are going to write a program that will allow you to play a game of Hangman against the computer. After you play
Have you heard of the game of Hangman? Well, you are going to write a program that will allow you to play a game of Hangman against the computer. After you play
Have you heard of the game of Hangman? Well, you are going to write a program that will allow you to play a game of Hangman against the computer. After you play
Have you heard of the game of Hangman? Well, you are going to write a program that will allow you to play a game of Hangman against the computer. After you play

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site