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

Hangman Class 1.) The class HangMan should have following instance variables: pr

ID: 3829047 • Letter: H

Question

Hangman Class

1.) The class HangMan should have following instance variables:

private members:

2.) Constructor and Destructor

This class should have a constructor to initialize all the above instance variables.

The size of these arrays will be learned at runtime, meaning you will have to new them. You'll need to check the length of the string, here's how:

std::string testStr = "abc"; std::cout << testStr.length() << ' ';

You also need to write a destructor that delete all the arrays you new in the constructor.

3.) This class should have following public methods. Only these methods should be public.

int main(int argc, char** argv) { std::string myString(argv[0]); //OR std::string myString2 = argv[0]; }

4.) For each method write preconditions and postconditions.

5.) No methods of the HangMan class should output text to the screen, or ask the user for input. All of the user interface should be encoded in the GameDriver class. This division of labor is useful if, for example, you later wanted to write a new user interface for your hangman game that allowed it to run on the tablets, using a graphical user interface. The underlying hangman game logic could remain the same; only the user interface code would need to be replaced.


Can you teach me how to do the constructor and destructor parts?

Explanation / Answer

Hangman.h

====

/*
* Hangman.h
*
* Created on: 30-Apr-2017
*      Author: ajay
*/

#ifndef HANGMAN_H_
#define HANGMAN_H_
#include <string>

class Hangman {

private:
   std::string secretWord;
   char *disguisedWord;
   int disguisedWordSize;
   int guessCount;
   int missesCount;
   int MAX_MISSES_ALLOWED;
   char *missedMarkers;

public:
   Hangman(std::string);
   ~Hangman();
   bool guessCharacter(char);
   bool isGameOver();
   bool isFound();
   std::string getDisguisedWord();
   int getGuessCount();
   int getMissesCount();
   std::string getMissedMarker();
   int getWordLength();

};

#endif /* HANGMAN_H_ */

Hangman.cpp

====

/*
* Hangman.cpp
*
* Created on: 30-Apr-2017
*      Author: ajay
*/

#include "Hangman.h"

Hangman::Hangman(std::string secret) {
   secretWord = secret;
   guessCount = 0;
   missesCount = 0;
   MAX_MISSES_ALLOWED = 7;
   disguisedWordSize = secretWord.length();
   // Holds question marks extra 1 for '' NULL.
   disguisedWord = new char[disguisedWordSize+1];
   // Misses
   missedMarkers = new char[MAX_MISSES_ALLOWED+1];

   // Initialize with all '?' 's
   for (signed int i = 0; i < disguisedWordSize; ++i)
               disguisedWord[i] = '?';

   // Initialize with all 'O' 's
   for (int i = 0; i < MAX_MISSES_ALLOWED; ++i)
       missedMarkers[i] = 'O';
};

Hangman::~Hangman() {
   delete disguisedWord;
   delete missedMarkers;
};

/**
* After each guess this character will be called.
*/
bool Hangman::guessCharacter(char c)
{
   for (unsigned int i = 0; i < secretWord.length(); ++i)
   {
       if (disguisedWord[i] == '?' && secretWord.at(i) == c)
       {
           guessCount++;
           disguisedWord[i] = secretWord[i];
           return 1;
       }
   }
   missedMarkers[missesCount] = 'X';
   missesCount++;
   return 0;
};

/**
* Checks if the game is over.
*/
bool Hangman::isGameOver()
{
   return this->MAX_MISSES_ALLOWED <= this->missesCount;
};

/**
* Checks if the word is found
*/
bool Hangman::isFound()
{
   return this->secretWord.length() <= this->guessCount;
};

// GETTERS
std::string Hangman::getDisguisedWord() { return this->disguisedWord; };
std::string Hangman::getMissedMarker() { return this->missedMarkers; };
int Hangman::getGuessCount() { return this->guessCount; };
int Hangman::getMissesCount() { return this->missesCount; };
int Hangman::getWordLength() { return this->secretWord.length(); };

/*
* main.cpp
*
* Created on: 30-Apr-2017
*      Author: Rj
*/
#include <iostream>
#include "Hangman.h"
using namespace std;

int main(int argc, char** argv)
{
   //std::string myString(argv[0]);
   std::string myString("rjiscool");

   Hangman h1{myString};
   char res;

   while (! (h1.isGameOver() || h1.isFound()) )
   {
       cout << h1.getDisguisedWord() << endl;
       cout << "Lives: " << h1.getMissedMarker()
           << endl;
       cout << "Guess a character: ";
       cin >> res;
       h1.guessCharacter(res);
   }

   cout << "End of the game! :D" << endl;

   return 0;
}

I think as this text says a friend class can be made as a layer so that it takes all the control. But for this queston this answer will suffice. Play this hangman game and have fun. Don't forget to set your ide's std to c++14.

Cheers,

PH

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