QuestionQuestion

Transcribed TextTranscribed Text

Superhero fight Part 1: Powers.h • This file has 4 sections: 1. Constant integers indicating power 2. A Power abstract class 3. Individual power classes (that inherit from the Power abstract class) 4. A powerFactory function • For the constant integers, create 6 constint variables, each representing a different Power. These will be used as the IDs for the Power classes. The different powers are: 1. Flight 2. Gadgets 3. Intelligence 4. Laser 5. Nationalism 6. Strength • The Power abstract class must have the following: 1. A protected member variable to hold the power’s description. 2. A getDescription function that receives no input and returns a std::string. Its job is to return the value in the Power’s member variable that holds the description. 3. A pure virtual function called getID that receives no input and returns an int. Its job (in the child classes of Power) to return the appropriate ID (one of the 6 const int variables) from the previous section. 4. A pure virtual function called use that receives no input and returns no value. It will (in the child classes of Power) use std::cout to display some description of what happens when the power is used. 5. A pure virtual function called fight that receives a pointer to another Power and returns an int. It will determine based on the other Power which “wins” a fight between the two, the current Power or the other Power. If the other Power wins, it will return -1. In the event of a tie, it returns 0. When the current power wins, it returns 1. • There should be 6 concrete class definitions, one for each power. They are to inherit from Power and should implement all of the pure virtual functions from Power. Additionally, each has a default constructor. For example, the class FlightPower would inherit from Power. It would have the default constructor (FlightPower()). Additionally it would have the functions getID, use, and fight. • Finally there is a function powerFactory that exists outside of all classes in the file Powers.h. It accepts a std::string as input and returns a pointer to a dynamically created Power variable. Its job is to determine, based on the std::string passed to it, which Power to create and return a pointer to that. Part 2: Powers.cpp • The getter from Power should return mDescription. • The constructor for each Power subclass should set the mDescription in the super class (Power). • The getID function in each Power subclass should return the appropriate const int variable that indicates which Power the class is. For example, the StrengthPower class would return the const int POWER_STRENGTH (or whatever you named it). • The use function in each Power subclass displays (to std::cout) some interesting text specific to each power. • The fight function in each Power subclass determines which Power subclass is passed to it (by using the getID function for the other Power variable). If the current Power wins, the function returns 1. The function returns a 0 in the event of a tie. The function returns a -1 if the current Power loses. The following matrix determines outcomes: Power Definitions: F: Flight G: Gadgets I: Superhuman intelligence L: Laser shooting eyes N: Nationalism S: Superhuman strength For example, if the Flight power called fight with the input of the Strength power, the result would be 1 (based on the table above). But if the input were the Laser power, the result would be -1. When the winner is determined, display (using std::cout) some interesting text to indicate the winner. The powerFactory function is not a class function. Based on the inputted std::string it dynamically creates a Power (using the new keyword) and returns a pointer to that new Power. The strings will match the original input from the heroes.txt file. For example, the string “intel” should generate the Power corresponding to Superhuman Intelligence. Part 3: Hero.h changes • For previous homework the Hero had a std::vector of std::string variables. That vector should now hold pointers to Power variables (std::vector<Power*>). • Add to the same class a public function called useRandomPower. It accepts no input and returns a pointer to a Power. • Add to Hero a functions called takeDamage, resetHealth, and printHealth. They all return void and receive no input. Part 4: Hero.h changes • The Hero constructor now must create a new Power*. In previous homework, you added to a std::vector a std::string that contains a Power name. Instead, use that Power name and the powerFactory function to create a Power* and add that to the std::vector that belongs to the Hero that contains the Power* variables. • The Hero function useRandomPower uses rand() to randomly pick a Power from the Hero’s std::vector containing the Power* variables. Once a random Power is selected, its name is displayed and its use function is called to display that it’s being used. Then return it. • The printPowers function will need to change to use the stored Power* vector instead of std::string variables. • The takeDamage function decrements the Hero’s current health by 1. • The resetHealth function sets the Hero’s current health back to the maximum health. Part 5: Driver.cpp changes • The loadHeroes should remain the same. Note that the user may call loadHeroes several times, so it might be a GoodIdea® to empty the vector of Hero* variables before populating it with the Hero variables constructed from the inputted text file. • The printRoster should require no changes. • Add a function called selectHero above the main function. It returns a pointer to a Hero and accepts the vector of Hero* variables a std::string containing the user’s input. The function must determine which Hero is selected based on the std::string input and return a pointer to that Hero. For example, if the std::string contains “5” then the return value will be the Hero* from the vector at index 5. • Add a function called heroCombat above the main function. It should operate as follows: 1. Display the names of the Hero variables in the std::vector of Hero* variables. 2. Prompt the user for 2 Heroes using the selectHero function. 3. Have the 2 Heroes fight until one runs out of health by: Each Hero chooses a random Power with the useRandomPower function. A “winner” of the round is determined by the fight function. The loser Hero loses a point of health with the takeDamage function. In the event of a tie both lose health. 4. Once a winner is determined display who won. • Now implement option 3 in main by calling the heroCombat function! Hints 1. It is a GoodIdea to program defensively. That means you should check to make sure the user’s input is valid. You aren’t guaranteed valid input anymore! 2. It is a BadIdea© to just quit your program. This assignment uses dynamic memory allocation (with the new keyword). You should be using delete on whatever you dynamically allocate – especially if you want full credit! A Note on Style Be sure to comment your code. Finally, Full Sample Output Below is sample output for a full run-through of the program. Your output should resemble the following – but doesn’t need to match it exactly (user input is in red). Choose an option: 1. Load Heroes 2. Print Hero Roster 3. Hero Fight! 4. Quit > 1 Enter the file to load: heroes.txt 10 heroes loaded. Choose an option: 1. Load Heroes 2. Print Hero Roster 3. Hero Fight! 4. Quit > 3 Here are the heroes and their indices: 0: Superman 1: Dr. Manhattan 2: Batman 3: Captain America 4: Cyclops 5: Iron Man 6: Thor 7: Hulk 8: Wonder Woman 9: Green Lantern --------------------------------------- Select your first hero: 0 Select your second hero: 2 --------------------------------------- Superman has 5 health Batman has 3 health Superman SMASH!! Batman flies away, maybe far from this place. FLIGHT WINS - They just fly away out of range of the attack. --------------------------------------- Superman has 4 health Batman has 3 health Superman SMASH!! Batman uses what's (hopefully) the right tool for the job. STRENGTH WINS - The gadget gets SMASHED. --------------------------------------- Superman has 4 health Page 5 of 5 Batman has 2 health Superman flies away, maybe far from this place. Batman uses what's (hopefully) the right tool for the job. GADGETS WINS - Rope snares make it tough to fly away. --------------------------------------- Superman has 3 health Batman has 2 health Superman SMASH!! Batman flies away, maybe far from this place. FLIGHT WINS - They just fly away out of range of the attack. --------------------------------------- Superman has 2 health Batman has 2 health Superman flies away, maybe far from this place. Batman uses what's (hopefully) the right tool for the job. GADGETS WINS - Rope snares make it tough to fly away. --------------------------------------- Superman has 1 health Batman has 2 health Superman shoots lasers from their eyes. PEWPEWPEW! Batman flies away, maybe far from this place. LASER WINS - They have a good sniper shot so flying doesn't help. --------------------------------------- Superman has 1 health Batman has 1 health Superman shoots lasers from their eyes. PEWPEWPEW! Batman uses what's (hopefully) the right tool for the job. GADGETS WINS - The lasers are blocked by the gadget. -------------- ------------------------- Batman WINS!!! Choose an option: 1. Load Heroes 2. Print Hero Roster 3. Hero Fight! 4. Quit > 4 Quitting...

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

Drivers.cpp:

#include "Hero.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

//Free the alocated memory for heroes
void deleteHeroes(std::vector<Hero *> list) {
Hero *hero;
for (int i = 0; i < list.size(); i++) {
hero = list[i];
hero->deletePowers(); //delete powers attached to hero
delete hero;
}
list.empty();
}

//read in file and add each hero as vector input
void loadHeroes(std::string filename, std::vector<Hero *> &list) {
Hero *hero;
std::ifstream infile(filename);
if (infile.is_open()) {
std::string newLine;
deleteHeroes(list); //empty the list for case of reloading
while (!infile.eof()) {
std::getline(infile, newLine);
hero = new Hero(newLine);
list.push_back(hero);
}
std::cout << list.size() << " heroes loaded." << std::endl;
}
//file does not exist
else {
std::cout << "File not found." << std::endl;
}
}

//iterate through heroes list and print each of their powers
void printRoster(std::vector<Hero *> list) {
std::cout << "The following " << list.size() << " heroes are loaded..." << std::endl;
for (int i = 0; i < list.size(); i++) {
std::cout << "--------------------" << std::endl;
list[i]->printPower();
}
}...
$35.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats