Question

In: Computer Science

Need to design a program in C++ that plays hangman using classes (polymorphism and inheritance) Below...

Need to design a program in C++ that plays hangman using classes (polymorphism and inheritance)

Below is the code only need to add the classes and make the program run with at least one class of Polymorphism and Inheritance.

CODE:

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

int NUM_TRY=8; //A classic Hangman game has 8 tries. You can change if you want.
int checkGuess (char, string, string&); //function to check the guessed letter
void main_menu();
string message = "Play!"; //it will always display


int main(int argc, char *argv[])
{
string name;
char letter;
string word;
  

string words[] = //These are the list of 10 three lettered words.
{ //You can change them as per your requirement.
"man",
"van",
"tan",
"hop",
"pop",
"two",
"six",
"may",
"low",
"out",
};
  
srand(time(NULL));
int n=rand()% 10; //Random function to genterate random words from the given list
word=words[n];
  
  
string hide_m(word.length(),'X'); // This function is used to hide the actuall word to be guessed.
//The mask selected is 'X' so the word will show as 'XXX' till you guess the correct letters.
  
  
  
while (NUM_TRY!=0)
{
main_menu();
cout << "\n" << hide_m;
cout << "\nGuess a letter: ";
cin >> letter;
  
if (checkGuess(letter, word, hide_m)==0)
{
message = "Wrong letter.";
NUM_TRY = NUM_TRY - 1;
}
else
{
message = "NICE! You guessed a letter";
}
if (word==hide_m)
{
message = "Congratulations! You got it!";
main_menu();
cout << "\nThe word is : " << word << endl;
break;
}
}
if(NUM_TRY == 0)
{
message = "NOOOOOOO!...you've been hanged.";
main_menu();
cout << "\nThe word was : " << word << endl;
}
cin.ignore();// used to discard everything in the input stream
cin.get();
return 0;
}


int checkGuess (char guess, string secretword, string &guessword)
{
int i;
int matches=0;
int len=secretword.length();
for (i = 0; i< len; i++)
{
  
if (guess == guessword[i])
return 0;
  
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}

void main_menu()
{
system("cls");
cout<<"\nHangman Game!";
cout << "\nYou have " << NUM_TRY << " tries to try and guess the word.";
cout<<"\n"+message;
}

//*end of code*

Hangman Game

*** Need at least to add one Class to the game code that uses Polymorphism and Inheritance.

Solutions

Expert Solution

/* Below is the modified program of yours with added feature of polymorphism and inheritance. Polymorphism because the function checkGuess () is overloaded because it is declared in base as well as derived class and inheritance is used because a base class is derived in derive class */

#include <iostream>
#include <cstdlib>
#include<ctime>
#include <string>
using namespace std;
class Base {
public:
int checkGuess (){}
int NUM_TRY=8;
string message = "Play!";
};   
class Derived: public Base //inheritance
{
public:
int checkGuess (char guess, string secretword, string &guessword) // polymorphism
{
int i;
int matches=0;
int len=secretword.length();
for (i = 0; i< len; i++)
{
  
if (guess == guessword[i])
return 0;
  
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}
void main_menu(int NUM_TRY,string message)
{
cout<<"\nHangman Game!";
cout << "\nYou have " << NUM_TRY << " tries to try and guess the word.";
cout<<"\n"+message;
}
};

int checkGuess (char guess, string secretword, string &guessword)
{
int i;
int matches=0;
int len=secretword.length();
for (i = 0; i< len; i++)
{
  
if (guess == guessword[i])
return 0;
  
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}
int main()
{string name;
char letter;
string word;
Derived d=Derived();
string words[] = //These are the list of 10 three lettered words.
{ //You can change them as per your requirement.
"man",
"van",
"tan",
"hop",
"pop",
"two",
"six",
"may",
"low",
"out",
};
  
srand(time(NULL));
int n=rand()% 10; //Random function to genterate random words from the given list
word=words[n];
  
  
string hide_m(word.length(),'X');
while (d.NUM_TRY!=0)
{
d.main_menu(d.NUM_TRY,d.message);
cout << "\n" << hide_m;
cout << "\nGuess a letter: ";
cin >> letter;
  
if (d.checkGuess(letter, word, hide_m)==0)
{
d.message = "Wrong letter.";
d.NUM_TRY = d.NUM_TRY - 1;
}
else
{
d.message = "NICE! You guessed a letter";
}
if (word==hide_m)
{
d.message = "Congratulations! You got it!";
cout << "\nThe word is : " << word << endl;
break;
}
}
if(d.NUM_TRY == 0)
{
d.message = "NOOOOOOO!...you've been hanged.";
d.main_menu(d.NUM_TRY,d.message);
cout << "\nThe word was : " << word << endl;
}
cin.ignore();// used to discard everything in the input stream
cin.get();
return 0;
}


Related Solutions

The purpose of this lab is to practice using inheritance and polymorphism. We need a set of classes for an Insurance company.
The purpose of this lab is to practice using inheritance and polymorphism.    We need a set of classes for an Insurance company.   Insurance companies offer many different types of policies: car, homeowners, flood, earthquake, health, etc.    Insurance policies have a lot of data and behavior in common.   But they also have differences.   You want to build each insurance policy class so that you can reuse as much code as possible.   Avoid duplicating code.   You’ll save time, have less code to...
In this program we are going to utilize multiple classes to demonstrate inheritance and polymorphism. We...
In this program we are going to utilize multiple classes to demonstrate inheritance and polymorphism. We will be creating a base class for our game character. The base class will be LifeForm. We will have derived classes of Human, Dragon, and Unicorn. LifeForm will have the following attributes: hitPoints – range 0 to 100 strength – range 0 to 18 You will need to provide a default constructor that initializes these attributes as follows: strength – 15 hitPoints – 100...
I need to see a working example using inheritance and the classes (below) The person class...
I need to see a working example using inheritance and the classes (below) The person class has first name last name age hometown a method called getInfo() that returns all attributes from person as a String The student class is a person with an id and a major and a GPA student has to call super passing the parameters for the super class constructor a method called getInfo() that returns all attributes from student as a String this methods has...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface called EmployeeInfo with the following constant variables: FACULTY_MONTHLY_SALARY = 6000.00 STAFF_MONTHLY_HOURS_WORKED = 160 2. Implement an abstract class Employee with the following requirements: Attributes last name (String) first name (String) ID number (String) Sex - M or F Birth date - Use the Calendar Java class to create a date object Default argument constructor and argument constructors. Public methods toString - returning a string...
Inheritance - Polymorphism One advantage of using subclasses is the ability to use polymorphism. The idea...
Inheritance - Polymorphism One advantage of using subclasses is the ability to use polymorphism. The idea behind polymorphism is that several different types of objects can have the same methods, and be treated in the same way. For example, have a look at the code we’ve included for this problem. We’ve defined Shape as an abstract base class. It doesn’t provide any functionality by itself, but it does supply an interface (in the form of .area() and .vertices() methods) which...
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the...
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the following members: • A field for the name of the ship (a string). • A field for the year that the ship was built (a string). • A constructor and appropriate accessors and mutators. • A toString method that overrides the toString method in the Object class. The Ship class toString method should display the ship’s name and the year it was built. Design...
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the...
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the following members: • A field for the name of the ship (a string). • A field for the year that the ship was built (a string). • A constructor and appropriate accessors and mutators. • A toString method that overrides the toString method in the Object class. The Ship class toString method should display the ship’s name and the year it was built. Design...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has a name property (string) Has a speech property (string) Has a constructor that takes two arguments, the name and the speech It has getSpeech() and setSpeech(speech) It has getName and setName(name) It has a method speak() that prints the name of the animal and the speech. o Example output: Tom says meow. Mouse Inherits the Animal class Has a constructor takes a name and...
Creat a theater booking system in java language using : 1.OOP objects -Classes 2.encapsulation 3.polymorphism 4.inheritance...
Creat a theater booking system in java language using : 1.OOP objects -Classes 2.encapsulation 3.polymorphism 4.inheritance 5.abstract class
write Java program has two classes ,( using Inheritance ) first class set ( id ,...
write Java program has two classes ,( using Inheritance ) first class set ( id , name ) and method output second class ( id , name , Mark 1 , Mark 2 , Mark 3 ) method total , average , grade , results ( if the student pass or not ) , and output method
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT