I have a program here. It was a solution to a problem that\'s been solved. The p
ID: 3772841 • Letter: I
Question
I have a program here. It was a solution to a problem that's been solved. The problem is:
Create a pure virtual base class Animal.
The base class has one private string attribute: _name.
The base class has one constant string method, name, that returns the class’s name attribute.
The Animal class constructor has one string parameter, new_name. The constructor makes sure that new_name is a string that is neither empty nor filled with white-space characters. It throws an exception if it is not. The constructor should not contain any input or output statements.
The base class has three pure virtual Boolean methods: AreYouAMeatEater, DoYouHaveFourLegs and AreYouAMammal. We shall call these methods characteristic methods. A characteristic method returns true if an animal has the characteristic and false otherwise.
A derived class has only a default constructor. It invokes the Animal class’s one parameter constructor.
The derived classes define the base class’s pure virtual methods. The methods should not contain any input or output statements. The following table shows the values that the derived class methods return.
AreYouAMeatEater
DoYouHaveFourLegs
AreYouAMammal
Panther
True
True
True
Komodo Dragon
True
True
False
Sperm Whale
True
False
True
Rattle Snake
True
False
False
Cow
False
True
True
Green Iguana
False
True
False
Manatee
False
False
True
Sparrow
False
False
False
Create an array of eight pointers to Animals. Assign a pointer to a particular Animal to each element of the array. No two pointers may point to the same kind of Animal. This is, all pointers point to different animals.
Use the array to write the following game:
The game randomly selects an animal from the array.
Use the selected animal’s characteristic methods to display its characteristics.
From the information that the animal reveals about itself, the user tries to guess the animal’s name. If the user guesses the animal’s name then the player wins; otherwise the player loses.
Write a Boolean function to play the game. The function returns true if the player wins and false if the player loses.
Invoke the game function in a loop so that the player can play the game an arbitrary number of times. Count the number of times the player plays the game, the number of times the player wins and the number of times the player loses. Display the counts.
The solution goes:
//Animal.h
#ifndef Animal_H
#define Animal_H
#include <iostream>
#include <string>
using namespace std;
class SpaceCharacterException {
public:
SpaceCharacterException() { }
void showMessage() {
cout << " Name neither should contain spaces nor should be empty." << endl;
}
};
class Animal : public SpaceCharacterException {
private:
string name;
public:
Animal(string new_name);
string getName();
virtual bool AreYouAMeatEater() = 0;
virtual bool DoYouHaveFourLegs() = 0;
virtual bool AreYouAMammal() = 0;
};
#endif
//Animal.cpp
//#include "stdafx.h"
#include "Animal.h"
#include <iostream>
#include <string>
using namespace std;
Animal::Animal(string new_name) {
int count = 0;
try {
if (new_name.size() == 0)
throw new SpaceCharacterException();
else {
for (int i = 0; i<(int)new_name.length(); i++) {
if (new_name.at(i) == ' ')
count++;
if (count>1)
throw new SpaceCharacterException();
}
}
}
catch (SpaceCharacterException sce) {
sce.showMessage();
}
name = new_name;
}
string Animal::getName() {
return name;
}
//derivedAnimal.h
#ifndef derivedAnimal_H
#define derivedAnimal_H
#include "Animal.h"
class DerivedAnimal : public Animal {
public:
DerivedAnimal(string name) :Animal(name) { }
virtual bool AreYouAMeatEater();
virtual bool DoYouHaveFourLegs();
virtual bool AreYouAMammal();
};
#endif
//derivedAnimal.cpp
//#include "stdafx.h"
#include "Animal.h"
#include "derivedAnimal.h"
bool DerivedAnimal::AreYouAMeatEater() {
if (Animal::getName().compare("Panther") == 0)
return true;
else if (Animal::getName().compare("Komodo Dragon") == 0)
return true;
else if (Animal::getName().compare("Sperm Whale") == 0)
return true;
else if (Animal::getName().compare("Rattle Snake") == 0)
return true;
else if (Animal::getName().compare("Cow") == 0)
return false;
else if (Animal::getName().compare("Green Iguana") == 0)
return false;
else if (Animal::getName().compare("Manatee") == 0)
return false;
else if (Animal::getName().compare("Sparrow") == 0)
return false;
else
return false;
}
bool DerivedAnimal::DoYouHaveFourLegs() {
if (Animal::getName().compare("Panther") == 0)
return true;
else if (Animal::getName().compare("Komodo Dragon") == 0)
return true;
else if (Animal::getName().compare("Sperm Whale") == 0)
return false;
else if (Animal::getName().compare("Rattle Snake") == 0)
return false;
else if (Animal::getName().compare("Cow") == 0)
return false;
else if (Animal::getName().compare("Green Iguana") == 0)
return true;
else if (Animal::getName().compare("Manatee") == 0)
return true;
else if (Animal::getName().compare("Sparrow") == 0)
return false;
else
return false;
}
bool DerivedAnimal::AreYouAMammal() {
if (Animal::getName().compare("Panther") == 0)
return true;
else if (Animal::getName().compare("Komodo Dragon") == 0)
return false;
else if (Animal::getName().compare("Sperm Whale") == 0)
return true;
else if (Animal::getName().compare("Rattle Snake") == 0)
return false;
else if (Animal::getName().compare("Cow") == 0)
return true;
else if (Animal::getName().compare("Green Iguana") == 0)
return false;
else if (Animal::getName().compare("Manatee") == 0)
return true;
else if (Animal::getName().compare("Sparrow") == 0)
return false;
else
return false;
}
//AnimalGuessGame.cpp
//#include "stdafx.h"
#include "Animal.h"
#include "derivedAnimal.h"
#include <iostream>
#include <string>
#include <vector>
#include <time.h>
using namespace std;
bool playGame(vector<Animal*> ani, int randi);
int main() {
//vector to store Animal
vector<Animal*> animal;
srand(time(0));
int randi;
int count = 0;
int win = 0, lose = 0;
string conti = "y";
bool flag = false;
//string arry holding animal name
string animalName[]={"Panther","Komodo Dragon","Sperm Whale","Rattle Snake","Cow","Green Iguana","Manatee","Sparrow"};
for(int k1=0;k1<8;k1++)
{
//randomly generates the animal's name
randi = rand() % 8;
animal.push_back(new DerivedAnimal(animalName[randi]));
}
do {
cout << "Welcome to Animal Guess Game" << endl;
randi = rand() % 8;
flag = playGame(animal, randi);
if (flag) {
cout << " Congrats! You have guessed correctly!" << endl;
win++;
}
else {
cout << " Sorry! Wrong Guess! I am " << animal.at(randi)->getName() << "." << endl;
lose++;
}
count++;
cout << "Would you like to play another game? Press (y/Y) to continue: ";
getline(cin, conti);
cout << endl;
} while (conti.compare("y") == 0 || conti.compare("Y") == 0);
cout << "Total number of games played: " << count << endl;
cout << "Number of games won: " << win << endl;
cout << "Number of games lost: " << lose << endl;
system("pause");
return 0;
}
bool playGame(vector<Animal*> ani, int randi) {
string userGuess;
if (ani.at(randi)->AreYouAMeatEater())
cout << "I am a meat eater" << endl;
else
cout << "I am not a meat eater" << endl;
if (ani.at(randi)->DoYouHaveFourLegs())
cout << "I am have four legs" << endl;
else
cout << "I am don't have four legs" << endl;
if (ani.at(randi)->AreYouAMammal())
cout << "I am a mammal" << endl;
else
cout << "I am not a mammal" << endl;
cout << " First letter of each word should be capital." << endl;
cout << "Guess who am I? ";
getline(cin, userGuess);
if (userGuess.compare(ani.at(randi)->getName()) == 0)
return true;
else
return false;
}
The array was replaced with a vector because the assignment called for it. I would just like to know how to fulfill this part of the program:
Write the inverse of problem 1' game. The game randomly generates the animal's name and the player tries to guess the animal's characteristics.
AreYouAMeatEater
DoYouHaveFourLegs
AreYouAMammal
Panther
True
True
True
Komodo Dragon
True
True
False
Sperm Whale
True
False
True
Rattle Snake
True
False
False
Cow
False
True
True
Green Iguana
False
True
False
Manatee
False
False
True
Sparrow
False
False
False
Explanation / Answer
Complete Program:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.