Question

In: Computer Science

C++ language You will create a Hangman class. Possible private member variables: int numOfCharacters; //for the...

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()

Solutions

Expert Solution

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!


Related Solutions

For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and puts that into g->hidden_word. It sets...
C++ make a rational class that includes these members for rational -private member variables to hold...
C++ make a rational class that includes these members for rational -private member variables to hold the numerator and denominator values -a default constructor -an overloaded constructor that accepts 2 values for an initial fraction -member fractions add(), sub(), mul(), div(), less(), eq(), and neq() (less should not return true if the object is less than argument) -a member function that accepts an argument of type of ostream that writes the fraction to that open output stream do not let...
java programming Create a class named Money. It should have member variables for Member Variables Store...
java programming Create a class named Money. It should have member variables for Member Variables Store dollars and cents as members (both should be int). They should be accessible from only inside the class. Methods  Write a default constructor that sets members to 0.  Write a two-parameter constructor that sets members to the parameter values.  Write get/set methods for the member variables.  Write an override method for toString. The returned string should be formatted as a...
C# Language Create a class called evaluateValue that declares 3 integer class variables: zeroValue, positiveValue and...
C# Language Create a class called evaluateValue that declares 3 integer class variables: zeroValue, positiveValue and negativeValue. These should be declared as public and you should not use automatic properties to declare them. Your class should have a constructor that takes one integer argument. In the constructor you will code if statements to set one of the three class variables (indicators) to 1 if the number sent to the constructor is either equal to zero, negative, or positive. In the...
Write a C++ programs to: 1. Create a class called Student with four (4) private member...
Write a C++ programs to: 1. Create a class called Student with four (4) private member variables, name (string), quiz, midterm, final (all double type); 2. Include all necessary member functions for this class (at least 1 constructor, 1 get function, and 1 grading function – see #6); 3. Declare three (3) objects of Student type (individual or array); 4. Read from the keyboard three (3) sets of values of name, quiz, midterm, final and assign them to each object;...
Create a C# Application. Create a class object called “Employee” which includes the following private variables:...
Create a C# Application. Create a class object called “Employee” which includes the following private variables: firstN lastN idNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods: constructor properties CalcPay(): Calculate the regular pay and overtime pay. Create...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member variable are allowed Create accessor and mutator functions to set/return numerator denominator Create a function to set a fraction Create a function to return a fraction as a string ( common name ToString(), toString())  in the following format: 2 3/4 use to_string() function from string class to convert a number to a string; example return to_string(35)+ to_string (75) ; returns 3575 as a string Create...
C++ Classes & Objects Create a class named Student that has three private member: string firstName...
C++ Classes & Objects Create a class named Student that has three private member: string firstName string lastName int studentID Write the required mutator and accessor methods/functions (get/set methods) to display or modify the objects. In the 'main' function do the following (1) Create a student object "student1". (2) Use set methods to assign StudentID: 6337130 firstName: Sandy lastName: Santos (3) Display the students detail using get functions in standard output using cout: Sandy Santos 6337130
directions: use c++ Create a  ContactInfo class that contains the following member variables: name age phoneNumber The...
directions: use c++ Create a  ContactInfo class that contains the following member variables: name age phoneNumber The ContactInfo class should have a default constructor that sets name = "", phoneNumber = "", and age = 0. The ContactInfo class should have a constructor that accepts the name and phone number as parameters and sets name = <value of parameter name>, aAge = 0, and phoneNumber = <value of parameter phone number>. The ContactInfo class should have accessor and mutator functions for...
C++ Program Create Semester class (.h and .cpp file) with the following three member variables: ▪...
C++ Program Create Semester class (.h and .cpp file) with the following three member variables: ▪ a semester name (Ex: Fall 2020) ▪ a Date instance for the start date of the semester ▪ a Date instance for the end date of the semester The Semester class should have the following member functions 1. Create a constructor that accepts three arguments: the semester name, the start Date, and the end Date. Use default values for all the parameters in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT