Zookal
Zookal

We’d love to hear any feedback or comment from you!

© 2011-2021 Zookal Pty Ltd

View question and answer

From our collection of questions and answers
Engineering · Computer Science
Question details

I need to put this C++ code in separate files, such as the main.cpp, the Class.h (header file) and the Class.cpp

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

int BodyParts = 6; // There are 6 body parts: head, torso, 2 arms and 2 legs

// Declaring the class for our game
class Hangman{
private:
char letter;
int limb_extractor = 0, right_guess, lenght;
// The limb_extractor is 0 because we must declare that the game starts with 0 limbs
// As the player guesses wrong, the limbs increases
string word, words[10];
// We must declare the necessary variables for the player inputs
// and keep track of the misses

public:
// Function that returns the right guess to fill the blank with the correct letter
int fill_blank(char letter_input, string hidden_word, string &guess_word){
right_guess = 0;
lenght = hidden_word.length();
for(int i=0; i<lenght; i++){
if (letter_input == guess_word[i])
return 0; // If the user inputs the same letter twice, it will return 0

if (letter_input == hidden_word[i]){
guess_word[i] = letter_input;
right_guess++; // If the letter the player inputs is in the secret word, the score increases
}
}
return right_guess;
}

// Constructor
Hangman(){
// Declaring the ten 3 letter words
words[0] = "cat"; words[1] = "pen";
words[2] = "fan"; words[3] = "lot";
words[4] = "can"; words[5] = "ham";
words[6] = "dog"; words[7] = "rat";
words[8] = "hat"; words[9] = "fun";

// Function to randomize the word to appear so the player does not memorize the order
srand(time(NULL));
int x = rand()%10;
word = words[x];

// The secret word will be hidden in asterisks
string disguise(word.length(), '*');

// Calling the Game function
Game(disguise);
}

//Define the play function
void Game(string disguise){
// User interface
cout << "This is a simple Hangman Game. ";
cout << "Each word consists of 3 letters only. ";
cout << "You have 6 chances to guess wrong. ";
cout << "Good luck!" << endl;

// While loop will end if the player guesses wrong 6 times or if he guesses right three times
while (limb_extractor < BodyParts){
cout << endl << " " << disguise << endl;
cout << "Guess a letter: ";
cin >> letter;
if (fill_blank(letter, word, disguise)==0){
cout << " That letter is incorrect :( " << endl;
limb_extractor++;
}else
cout << " That letter is correct :)"<< endl;
cout << " You have " << BodyParts - limb_extractor << " lives left." << endl;

if (word==disguise){
cout << " You won! ";
cout << "The secret word was: " << word << endl;
break;
}
}

if(limb_extractor == BodyParts){
cout << " Game Over. " << endl;
cout << "The secret word was: " << word << endl;
}

}
};

int main(){
// Calls class with object's name
Hangman Project;
return 0;
}

#include <iostream> #include <cstdlǐb> #include <ctime> #include <string> using namespace std; int BodyParts-6; // There are 6 body parts: head, torso, 2 arms and 2 legs Declaring the class for our game class Hangmanf private char letter: int limb extractor-0, right guess, lenght; The 1imb extractor is because we must declare that the game starts with 0 1limbs 7As the player guesses wrong, the limbs increases string word, words [10]: We must declare the necessary variables for the player inputs /and keep track of the misses public: /Function that returns the right guess to fill the blank with the correct letter int fill _blank (char letter_input, string hidden word, string &guess_word) f right_guess0: lenght -hidden word.length ) for (int i-o: <lenght; i++) if (letter_inputguess wordi) return 0: 1/ If the user inputs the same letter twice, it will return if (letter_input- hidden_word[i]) guessword [i] letterinput ; right_guess++: 77 If the letter the player inputs is in the secret word, the score increases - - return right_guess:

/Constructor Hangman () t Declaring the ten 3 letter words words [0] cat, words [1] = pen. words [2] -fan; words[3]lot words [4] can words [5]ham words [6] dog, words [7] rat. words [8] = hat, words [9] fun ; 7Function to randomize the word to appear so the player does not memorize the order srand (time (NULL)); int x rand ( ) %10; word words [x] ; /The secret word will be hidden in asterisks string disguise (word.length ),* Calling the Game function Game (disguise) //Define the play function void Game (string disguise) f User interface cout < This is a simple Hangman Game. ; cout << Each word consists of 3 letters only. cout << You have 6 chances to guess wrong.In cout << Good luck! << endl: 7While loop will end if the player guesses wrong6 times or if he guesses right three times while (limb_extractor < BodyParts)ti cout << endl <<<< disguise << endl: cout<<Guess a letter: cin >> letter; if (fill blank (letter, word, disguise)0) f cout << nThat letter is incorrect << endl limb extractor++: else cout <<nThat letter is correct :)<< endl; cout < nYou have << BodyParts limb extractor << lives left. << endl;

if (word disguise) cout<< nYou won!n cout << The secret word was<<word << endl: break if (limb_extractorBodyParts) cout <<Game Over.In << endl: cout << The secret word was: <<word << endl; int main) Calls class Hangman Project: return 0; with objects name
/*

OUTPUT:

This is a simple Hangman Game.
Each word consists of 3 letters only.
You have 6 chances to guess wrong.

Good luck!

***
Guess a letter: c

That letter is correct :)

You have 6 lives left.

c**
Guess a letter: a

That letter is correct :)

You have 6 lives left.

ca*
Guess a letter: t

That letter is correct :)

You have 6 lives left.

You won!
The secret word was: cat

Process returned 0 (0x0) execution time : 6.825 s
Press any key to continue.

*/

Answer
Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.

Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.