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

Assignment Specifications The game called Hangman is a spelling game: one player

ID: 3784246 • Letter: A

Question

Assignment Specifications

The game called Hangman is a spelling game: one player sets up a word or phrase, represented by a row of dashes. If the guessing player suggests a letter which occurs in the word, the other player writes it in all its correct positions. If the suggested letter does not occur in the word, the other player draws one element of the hanged man stick figure as a tally mark. The game is over when:

The guessing player completes the word, or guesses the whole word correctly

The other player completes the diagram.

You must implement a simplified version of hangman in which the player gets 7 incorrect guesses (corresponding to drawing the “hanged man”).
You are required to implement the specified functions and utilize any provided functions.

Hangman Algorithm

Get phrase to be solved from the user (using getline instead of the input operator >> to allow for the presence of spaces in the phrase)

Clear output window so phrase is not seen, using system("CLS")

Setup unsolved phrase so game has a separate string with dashes in place of characters

(call setupUnsolved function)

Display the unsolved phrase

Play Game until phrase is completely guessed, or all 7 incorrect guesses are used up

Get a valid letter guess from user (call getGuess function - see below for meaning of “valid guess”)

(After initial phrase has been set, user input should ONLY occur in the getGuess function)

Update a string consisting of all characters guessed so far

Take action on the guess:

Correct: update unsolved phrase using the updateUnsolved function

Incorrect: increment the wrong guess count

Clear the screen again, using system("CLS")

Output game status:

updated unsolved phrase - phrase with dashes for still-unguessed letters

list of characters guessed so far

number of wrong guesses left

Output game over message (You lost! or Congratulations!!)




Additional Requirements and Hints

Implement each function and get feedback/credit for each before doing main game

Clear screen by using the command system("CLS");

All user input statements should be followed by outputting a blank line.

All user inputs and corresponding prompts after initial phrase setup occur only in body of getGuess - so there are no input or output statements in setupUnsolved or updateUnsolved

All phrases can have lowercase and uppercase alphabetic characters and valid guesses will use only lowercase alphabetic characters (i.e. in our testing, we will enter only lower case characters as valid guesses; we will, of course, input non-alpha characters to test for invalid guesses).

The player gets 7 wrong guesses

You should be utilizing a single puzzle variable to hold the current state of the game, and assigning to it the return value from setupUnsolved initially, and updateUnsolved subsequently.

Use the string member function at(): Remember, at() can be used on either side of the assignment operator (=), to either “read” the value at a specified position of the string (right side); or to “write” a new value into a position of the string (left side).
The function at() “throws an exception” when you attempt to access a non-existent location, unlike the [] syntax; this can be a great help when debugging.

bool isalpha(char): In order to check if a guess is valid - i.e. if it is an alphabetic character - you can use this built-in predicate function that takes a single character as an argument.
Requires that you include the <cctype> library.

Note: The functions setupUnsolved and updateUnsolved are “complementary” to each other (one replaces letters with dashes, the other replaces those dashes with letters).
So once you’ve got one working, the other should be real simple!

Functions

The following 3 functions are required, exactly as declared.
You may, if you wish, define other functions in addition to these.

You must define, implement and correctly invoke these 3 functions exactly as declared here.
You may wish to copy & paste the following into your source code, and comment out the function declarations you are not yet ready to implement.

/// @brief Puts dashes in place of alphabetic characters in the phrase.

/// @param phrase the phrase to be solved

/// @return the phrase with all alphabetic characters converted to dashes

string setupUnsolved(string phrase);


/// @brief Replaces the dashes with the guessed character.

/// @param phrase the phrase to be solved

/// @param unsolved the phrase with dashes for all unsolved characters

/// @param guess the char containing the current guessed character

/// @return the new unsolved string with dashes replaced by new guess

string updateUnsolved(string phrase, string unsolved, char guess);


/// @brief Gets valid guess as input.

///

///    A guess is taken as input as a character. It is valid if

///    1) it is an alphabetic character; and

///    2) the character has not already been guessed

///

/// @param prevGuesses the string containing all characters guessed so far

/// @return a valid guess and only a valid guess as a character

char getGuess(string prevGuesses);


Example Run

(user input has been bolded and underlined for emphasis; c by itself is clearScreen output.
Note the two invalid guesses: ‘l’ is invalid because it is already in the list of previously guessed characters; and 8 is invalid because it is not alphabetic
Note also that the original non-alphabetic character ‘.’ was not replaced by a dash)

Enter phrase: hello.

c

Phrase: -----.

Enter a guess: h

c

Phrase: h----.

Guessed so far: h

Wrong guesses left: 7

Enter a guess: e

c

Phrase: he---.

Guessed so far: he

Wrong guesses left: 7

Enter a guess: l

c

Phrase: hell-.

Guessed so far: hel

Wrong guesses left: 7

Enter a guess: d

c

Phrase: hell-.

Guessed so far: held

Wrong guesses left: 6

Enter a guess: l

Invalid guess! Please re-enter a guess: 8

Invalid guess! Please re-enter a guess: o

c

Phrase: hello.

Guessed so far: heldo

Wrong guesses left: 6

Congratulations!!

Explanation / Answer

#include <iostream.h> // For input/output
#include <fstream.h> // For file input/output
#include <string.h> // For strcpy
#include <time.h> // For time
#include <stdlib.h> // For toupper and tolower

#define MAX_WORD_SIZE 15 // The max size for words
#define MAX_WORDS 255 // The max number of words

void LoadFile(); // Prototypes for our functions
void RunGame();
void DrawGallows(int State);
  
typedef char String[MAX_WORD_SIZE]; // Make a char type with 15 chars
String Words[MAX_WORDS - 1]; // This array will hold our words
int Count;       //word count

// the main function of our hangman game
void main()
{
char Continue = 'Y'; // end game if = to 'N'
cout<<"Welcome to Hangman . . . Don't lose your head!"<<endl;

LoadFile(); // Load the data file

// Continue the game as long as the player wants
while(Continue == 'Y')
{
RunGame(); // Run the game
cout<<endl<<"Do you want to play again (Yes or No)? ";
cin>>Continue;
Continue = toupper(Continue);
}

// say good bye
cout<<endl<<"Thanks for playing."<<endl;
}

// This will load our file
void LoadFile()
{
char C;    //Used to find EOF
ifstream Datfile; //The in file

Count=0; //Set count to 0
  
// Open the data file
Datfile.open("words.dat");

//Loop till the end of file
while((C=Datfile.peek()) != EOF)
{
// Get the next word and then increment Count
Datfile>>Words[Count++];

// If we surpass the max exit and tell the user
if(Count > MAX_WORDS - 1)
{
   cout<<endl<<"Too many words in the file, stopping with "
    <<MAX_WORDS<<" Words."<<endl;
   Count = MAX_WORDS;
   break;
}

}

// We need to subtract one to get the correct number of words
Count--;

// Close the data file
Datfile.close();

}

// This function will run the game
void RunGame()
{
int Word;   // This will hold the subscript of our word
int Size;   // This will hold the length of our word
int State=1; // This holds the game state
int Subscript=0; // This will hold subscripts
char Guess[MAX_WORD_SIZE]; // this will hold their current word
char Copy[MAX_WORD_SIZE]; // This will hold a copy of the word
char Letter; //This will be their letter guess
int Correct=0; // This is a True/False value deciding if they got a good answer
  
// Seed and create a random number
srand((unsigned)time( NULL )); // use time as a seed
Word = rand() % Count;

// Make a local copy of the word
strcpy(Copy,Words[Word]);

Size = strlen(Words[Word]); // Get the word's size

// Create a null terminated string to represent the word as the
// player knows it.
for(; Subscript < Size; Subscript++)
{
Guess[Subscript] = '-';
}

// insert the null character
Guess[Subscript] = '';

// Go till the player is hung
while(State!=6)
{
DrawGallows(State); //Draw the gallows
cout<<Guess<<endl; // Draw Guess

cout<<"Guess a letter :";
cin>>Letter;

// We will use only lower case letters
Letter = tolower(Letter);
  
// Loop through the word
for(Subscript = 0; Subscript < Size; Subscript++)
{

   //if the guess is good tell the user and update Guess
   if(Copy[Subscript] == Letter)
   {
    Guess[Subscript] = Letter;
    Correct = 1;
    cout<<endl<<"Good Guess!";

    // If guess equals the word they won so exit
    if(strcmp(Words[Word],Guess) == 0)
    {
     cout<<endl<<"Yea, You survived and won!";
     return;
    }
   }
}

// If they didn't get aletter correct chide the user
if(Correct == 0)
{
   cout<<endl<<"Sorry, bad guess!";
   State++;
}

Correct = 0; // reset Correct

}

DrawGallows(State); //Draw the gallows at end of game
//They lost if they are here so tell them the answer.
cout<<"The word was : "<<Words[Word]<<endl<<endl;

}

// This will Draw the gallows according to the state
void DrawGallows(int State)
{
if(State==6)
{
// The \ will translate as '' because it is a special char
cout<<endl<<endl
   <<"   +----+     "<<endl
   <<"   |    |     "<<endl
   <<"   |    O     "<<endl
   <<"   |   /|\   "<<endl
   <<"   |   / \   "<<endl
   <<"   |Your Dead "<<endl
   <<" ============"<<endl<<endl;
}
else if(State==5)
{
cout<<endl<<endl
   <<"   +----+ "<<endl
   <<"   |    | "<<endl
   <<"   |    O "<<endl
   <<"   |   /|\ "<<endl
   <<"   |     \ "<<endl
   <<"   |       "<<endl
   <<" ============"<<endl<<endl;
}
else if(State==4)
{
cout<<endl<<endl
   <<"   +----+ "<<endl
   <<"   |    | "<<endl
   <<"   |    O "<<endl
   <<"   |   /|\ "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<" ============="<<endl<<endl;
}
else if(State==3)
{
cout<<endl<<endl
   <<"   +----+ "<<endl
   <<"   |    | "<<endl
   <<"   |    O "<<endl
   <<"   |   /| "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<" ============="<<endl<<endl;
}
else if(State==2)
{
cout<<endl<<endl
   <<"   +----+ "<<endl
   <<"   |    | "<<endl
   <<"   |    O "<<endl
   <<"   |    | "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<" ============="<<endl<<endl;
}
else if(State==1)
{
cout<<endl<<endl
   <<"   +----+ "<<endl
   <<"   |    | "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<" ============="<<endl<<endl;
}

}

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