In: Computer Science
C++ language
You will create a Hangman class.
Possible private member variables:
int numOfCharacters; //for the secret word
char * secretWord;
char *guessedWord;
public:
//please create related constructors, getters, setters,constructor()
constructor()
You will need to initialize all member variables including the two dynamic variables.
destructor()
Please deallocate/freeup(delete) the two dynamic arrays memories.
guessALetter(char letter)
1.the function will check if the letter guessed is included in the word.
2. display the guessed word with related field(s) filled if the letter guessed matches the some of the letters of the word
gameResult()
1. Display how many times guessed so far
2. Display how many times with the wrong letter guessed3. Display "you win" if all letters are filled in the guess word4. Display "you lost" if the times of guessing the wrong letter reaches 6 (Note: 6 body parts - head, body, left arm, right arm, left leg, right leg)
main()
Instantiate an object of Hangman.Ask the host: how many letters in the guess word.
Ask the host: what is the guess word?
Design a loop for the player to play the guess game
1. The loop should exit if the number of wrong guess reaches 6
2. inside the loop, there should be a condition to allow the player to exit, say "Do you want to continue playing (y|n)?". If the user chooses 'n', exit the loop.
3. Inside the loop, you may do some function chaining calls, something like:
obj.guessALetter(guessLetter).gameResult()
Screenshot of code (copy - pastable code is given at the end)
Sample Outputs :
Copy Pastable Code :
#include<bits/stdc++.h>
using namespace std;
class Hangman
{
private :
int numOfCharacters;
char *secretWord; //to store secret word
char *guessedWord; //to store current guessed word. will have - for
unguessed letters
int guesses; //stores total number of guesses
int wrongGuesses; //stores number of wrong guesses
public :
Hangman(int l, char *word) //constructor
{
numOfCharacters = l;
secretWord = new char[l];
strcpy(secretWord, word); //set secret word to what host told
us
secretWord[l] = '\0';
guessedWord = new char[l];
fill(guessedWord, guessedWord+l, '-'); //set guessed word initially
to all -
guesses = 0;
wrongGuesses = 0;
}
~Hangman() //destructor
{
delete [] secretWord;
secretWord = NULL;
delete [] guessedWord;
guessedWord = NULL;
}
Hangman guessALetter(char letter)
{
bool found = false; //stores true is letter matched in secret
word
for(int i = 0; secretWord[i] != '\0'; i++)
{
if(secretWord[i] == letter) //if letter present in secret
word
{
if(guessedWord[i] == '-') //and has not been previously
matched
{
found = true; //then found set
guessedWord[i] = letter; //and guessed word updated to show matched
letter
break;
}
}
}
if(found == false) //if letter not matched wrong guess
increased
wrongGuesses++;
guesses++;
return *this; //returns the object this function was called
on
}
int gameResult()
{
if(strcmp(secretWord, guessedWord) == 0) //if entire word has been
matched
{
cout<<"\nYou Win\n";
return numOfCharacters;
}
if(wrongGuesses == 6) //if wrong guesses limit has been
reached
{
cout<<"\nYou Lose\n";
return 0;
}
cout<<"\nTotal guesses till now :
"<<guesses<<endl;
cout<<"Wrong guesses till now :
"<<wrongGuesses<<endl;
//cout<<"Guessed word is :
"<<guessedWord<<endl;
return 0;
}
int getWrongGuesses() //getter
{
return wrongGuesses;
}
int getNumOfCharacters() //getter
{
return numOfCharacters;
}
};
int main()
{
int l;
char word[40];
cout<<"To Host : How many letters in the Guess Word? ";
cin>>l;
cout<<"To Host : What is guess word? ";
cin>>word;
Hangman h(l, word);
char ch = 'y';
char g;
//loop till wrong guesses are less than limit 6
while(h.getWrongGuesses() < 6)
{
cout<<"\nTo Player : Do you want to keep playing (y|n) :
";
cin>>ch;
if(ch == 'n')
break;
cout<<"\nGuess a letter : ";
cin>>g;
//if user wins exit
if( h.guessALetter(g).gameResult() == h.getNumOfCharacters())
{
break;
}
}
return 0;
}
Hope this helped!