The game of Hangman is a popular game which consists of guessing a word or phras
ID: 3691735 • Letter: T
Question
The game of Hangman is a popular game which consists of guessing a word or phrase by choosing letters and finding out if the letter is contained in the word or phrase and where it is located (i.e. "Wheel of Fortune").
In C++, create a hangman game class. Include the following public member functions:
constructor - initializes all variables and sets up the display array
bool initializeFile(string filename) - reads words from file and stores into array for game, returns false if there is a file problem
void displayStatistics() - displays wins/losses/number of words read from file/number of words still available for play
bool newWord() - randomly chooses new word from list of words, returns false if no more words available
void displayGame() - displays the hangman body, word (with underscores as needed), letters still available
bool guess(char letter, bool& done, bool& won) - processes the user's guess, returns false if letter chosen was not available, done will return true when user is out of guesses, won will return true when user has correctly guessed the word
void revealWord() - displays the word with all letters
The game will work as follows:
The user will see a short description of the game and be allowed to play or not. Once the user chooses to play he/she must provide the name of a file containing a list of words. There will be no more than 100 words and they are all in UPPERCASE letters. A word is chosen randomly from this list for each game until all words have been used or the player chooses to quit.
When the game begins an empty noose is displayed with a series of separated underscore characters that indicate the number of letters in the word to be guessed and a list of the available letters of the alphabet.
As the user guesses a letter, the display updates the result by either putting a single "body part" on the hangman's noose (head, neck, arm, etc.) OR replaces an underscore with the properly guessed letter AND removes the used letter from the available list. This continues until the user runs out of body parts or completes the word.
Repeat as long as the user wishes to play or until the words run out, keeping track of the number of wins and losses.
Notes:
If a letter appears more than once in the word, it will replace more than one underscore.
Body pieces should appear in the following order: head, neck, one arm, other arm, torso, one leg, other leg.
Specifics on displaying the noose and body parts in class are available here:
/home/fac/ohsh/submit/16sq1230/files/HangmanASCIIArt.txt
Explanation / Answer
Answer:
#include <iostream>
#include <fstream>
#include<ctime>
#include<cstdlib>
#include<string>
using namespace std;
class Hangman
{
string arr[100];
string word_Guess, guessedWrd;
int inttries;
int wrdsAva;
int wrdsUsed;
public:
Hangman()
{
word_Guess="";
guessedWrd="";
wrdsAva=0;
wrdsUsed=0;
inttries=0;
}
bool initializeFile(string filename)
{
int kk=0;
ifstream fin(filename.c_str());
if(fin.is_open())
{
while(!fin.eof())
{
fin>>arr[kk];
kk++;
}
}
else
{
return false;
}
fin.close();
wrdsAva=kk;
return true;
}
void displayStatistics()
{
cout<<"word_Guesss read from file:"<<wrdsAva<<endl;
cout<<"word_Guesss Available to play:"<<wrdsAva-wrdsUsed<<endl;
}
bool newWord()
{
if(wrdsUsed<wrdsAva)
{
int n=rand()%wrdsAva;
word_Guess=arr[n];
cout<<word_Guess;
wrdsUsed++;
return true;
}
return false;
}
void displayGame ()
{
guessedWrd = "";
for (int k=0;k<string(word_Guess).size();k++)
guessedWrd=guessedWrd+"_";
}
bool guess (char letter,bool& done, bool& won)
{
int cort=0,k=0;
bool returng=false;
for (k=0;k<string(word_Guess).size();k++)
if (word_Guess[k]==letter)
{
guessedWrd[k]=letter;
cort=1;
returng=true;
}
if (cort!=1)
inttries++;
if(inttries>5)
done=true;
if( word_Guess==guessedWrd)
won=true;
cout<<guessedWrd<<endl;
return returng;
}
void revealWord()
{
cout<<"The Word is"<<word_Guess<<endl;
}
};
int main ()
{
bool done=false;
bool won=false;
char letter='c';
srand((unsigned)time(0));
Hangman h;
bool in=h.initializeFile("hangman.txt");
if(in)
{
while (letter!='q')
{
bool n=h.newWord ();
h.displayGame ();
while (!done && !won)
{
cout << "guess a letter! : " ;
cin >> letter;
bool g=h.guess(letter,done,won);
if(g)
cout<<"You guessed Correctly"<<endl;
else
cout<<"Wrong Guess"<<endl;
}
if(done)
{
cout<<"You Lost";
h.revealWord();
}
if(won)
cout<<"You win";
h.displayStatistics();
cout<<" Want 2 play again:"<<endl;
cin >> letter;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.