Question

using C++

Requirements: . Classes you must create o Monster must be an abstract class Each monster has . an attack) o randomly generateo FireDragon Inherits from Dragon has all the attributes of a Dragon type is Fire Dragon After the initial 4 or 5 attacks a

Requirements: . Classes you must create o Monster must be an abstract class Each monster has . an attack) o randomly generate a number between 0 and strength for each attack (Strength 10 would generate 0-10) add up all the damage done return total damage o o e a number of attacks . a number for strength used during attacking . a string representing the type » accessor for type o Goblin inherits from Monster type is "Goblin" has 1 attack has strength of 30 n when attacking has a 10% chance of doing double damage o Troll inherits from Monster type is "Troll" has 2 attacks has strength 60 when attacking has a 20% of doing 100 damage for each attack . o Dragon inherits from Monster type is "Dragon" has 4 attacks has strength 100 After the initial 4 attacks a dragon has a 50% chance of gaining an additional attack o IceDragon Inherits from Dragon has all the attributes of a Dragon type is "lce Dragon" After all attacks are made (including the possible 1 additional) it has a 50% chance of the total damage being doubled
o FireDragon Inherits from Dragon has all the attributes of a Dragon type is 'Fire Dragon" After the initial 4 or 5 attacks are complete, has a 50% chance of attacking all over again (includes a new chance of having an additional attack as all Dragons have) a Each class must be defined in a header and implementation file named for the class (".hpp and ".cpp). All filenames must be all lowercase (Ice Dragon -> icedragon) . . Each classes' attack() function must output describing each attack as well as return total damage. See attached example text for good formatting. Each header must have include guards All numbers need to be integers lab5.cpp o This file will demonstrate your monsters' attacks o Use rand() from o Use srand(1234) in main o Create an array of 100 Monster pointers o Randomly fill the array with objects of each type Each type of Monster should have the same chance of being generated (20%-rand() % 5) See above examples for ideas of how to do this Loop through the entire array and call attack() on each element of the array and output results free dynamic memory o o
0 0
Add a comment Improve this question Transcribed image text
Answer #1

dragon.cpp

#include "dragon.hpp"
#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout;

//default constructor, initializes member variables
Dragon::Dragon()
{
   this->attacks = 4;
   this->strength = 100;
   this->type = "Dragon";
}

int Dragon::attack()
{
  
   int specialAttack = 1;//set variable so Dragon has the chance
                       //to get exactly 1 extra attack
   int total = 0;
  
   bool flag = false;//flag to loop until true
   do
   {      
       int attack = rand() % this->strength + 1;//attack is equal to random amount of strength
       cout << "Dragon attacks for " << attack << "\n";
       this->attacks--;//decrement number of attacks
       total += attack; //count the total

      
       //will only be looked at if the specialAttack is equal to 1
       if (this->attacks == 0 && specialAttack == 1)
       {
           int special = rand() % 2;//will generate either a 0 or 1
           if (special == 1)//50% chance of it being 1
           {
               this->attacks++;//increment attacks
               cout << "Dragon gains an extra attack!\n";
               specialAttack = 0;//set variable to 0 so it wont enter if statement again

           }
       }

       //condition to end while loop
       if (this->attacks == 0)
       {
           flag = true;//loop will end
       }
      
      
   } while (flag == false);

   cout << "Total Damage: " << total << "\n";
   return total;//return total damage done
}

//getTyep returns a string that represents the type of Monster. It takes no parameters
string Dragon::getType()
{
   return this->type;
}

dragon.hpp
#include "monster.hpp"
#ifndef DRAGON_HPP
#define DRAGON_HPP

class Dragon : public Monster
{
private:
   //member variables
   string type;
   int attacks;
   int strength;
public:
   //default constructor
   Dragon();

   //attack function
   int attack();

   //getType function
   string getType();
};

#endif

firedragon.cpp
#include "firedragon.hpp"
#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout;

//Default constructor, uses Dragon to set member variables
FireDragon::FireDragon()
   : Dragon()
{
   this->attacks = 4;
   this->strength = 100;
   this->type = "Fire Dragon";
}

int FireDragon::attack()
{
   int specialAttack = 1;//set variable so Dragon has the chance
                       //to get exactly 1 extra attack

   int doubleAttacks = 1;//int to see if the fire dragon gets to attack again

   //initialize total to 0
   int total = 0;

   bool flag = false;//flag to loop until true
   do
   {
       int attack = 1 + (rand() % this->strength);//attack is equal to random amount of strength
       cout << "Fire Dragon attacks for " << attack << "\n";
       this->attacks--;//decrement number of attacks
       total += attack; //count the total


       //will only be looked at if the specialAttack is equal to 1
       if (this->attacks == 0 && specialAttack == 1)
       {
           int special = rand() % 2;//will generate either a 0 or 1
           if (special == 1)//50% chance of it being 1
           {
               this->attacks++;//increment attacks
               cout << "Fire Dragon gains an extra attack!\n";
               specialAttack = 0;//set variable to 0 so it wont enter if statement again

           }
       }

       //will only be looked at if the doubleAttacks
       //this will determine if the dragon gets to attack all over again
       if (this->attacks == 0 && doubleAttacks == 1)
       {
           int special2 = rand() % 2;//will generate either a 0 or 1
           if (special2 == 1)//50% chance of it being 1
           {
               this->attacks = 4;//increment attacks
               cout << "Fire Dragon gets to attack ALL OVER AGAIN!\n";
               doubleAttacks = 0;//set variable to 0 so it wont enter if statement again
           }
       }

       //condition to end while loop
       if (this->attacks == 0)
       {
           flag = true;//loop will end
       }


   } while (flag == false);


   cout << "Total Damage: " << total << "\n";
   return total;//return total damage done
}

//getTyep returns a string that represents the type of Monster. It takes no parameters
string FireDragon::getType()
{
   return this->type;
}

firedragon.hpp
#include "dragon.hpp"
#ifndef FIREDRAGON_HPP
#define FIREDRAGON_HPP

class FireDragon : public Dragon
{
private:
   //member variables
   string type;
   int attacks;
   int strength;
public:
   //default constructor
   FireDragon();

   //attack function
   int attack();

   //getType function
   string getType();
};

#endif

goblin.cpp
#include "goblin.hpp"
#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout;

//default constructor. Initializes member variables
Goblin::Goblin()
{
   this->attacks = 1;
   this->strength = 30;
   this->type = "Goblin";
}

int Goblin::attack()
{
   //initialize the variable total that will be used to keep track of all damage done
   int total = 0;

   //set flag to determine if the total damage needs to be doubled
   bool flag = false;

   //loop until attacks = 0. For goblin, this is always only going to be on iteration
   while (this->attacks > 0)
   {
       //need to decrement attacks each time it lops
       int attack = 1 + (rand() % this->strength);//attack is equal to random amount of strength

       cout << "Goblin attacks for " << attack << " damage\n";

       //increment the total damage
       total += attack;

       //special is the Goblins special attack. He has a 10%
       //chance to do twice as much damage
       int special = rand() % 10;
       if (special == 1)//10% chance 0 - 9, or 10 numbers
       {
           cout << "Goblin doubles damage!\n";
           flag = true; //set flag to true
       }
       this->attacks--;//decrement the member variable attacks
   }

   //if flag is true, the total damage is increased times 2
   if (flag == true)
       total = total * 2;

   //display the total damage dealt
   cout << "Total Damage: " << total << "\n";
   return total;//return integer value total  
}
//getTyep returns a string that represents the type of Monster. It takes no parameters
string Goblin::getType()
{
   return this->type;
}

goblin.hpp
#include "monster.hpp"
#ifndef GOBLIN_HPP
#define GOBLIN_HPP

class Goblin : public Monster
{
private:
   //member variables
   string type;
   int attacks;
   int strength;
public:
   //default constructor
   Goblin();

   //attack function
   int attack();

   //getType function
   string getType();
};

#endif

icedragon.cpp
#include "icedragon.hpp"
#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout;

//Default constructor, uses Dragon to set member variables
IceDragon::IceDragon()
   : Dragon()
{
   this->attacks = 4;
   this->strength = 100;
   this->type = "Ice Dragon";
}

int IceDragon::attack()
{
   int specialAttack = 1;//set variable so Dragon has the chance
                           //to get exactly 1 extra attack

   //initialize total to 0
   int total = 0;

   bool flag = false;//flag to loop until true
   do
   {
       int attack = 1 + (rand() % this->strength);//attack is equal to random amount of strength
       cout << "Ice Dragon attacks for " << attack << "\n";
       this->attacks--;//decrement number of attacks
       total += attack; //count the total


       //will only be looked at if the specialAttack is equal to 1
       if (this->attacks == 0 && specialAttack == 1)
       {
           int special = rand() % 2;//will generate either a 0 or 1
           if (special == 1)//50% chance of it being 1
           {
               this->attacks++;//increment attacks
               cout << "Ice Dragon gains an extra attack!\n";
               specialAttack = 0;//set variable to 0 so it wont enter if statement again
           }
       }

       //condition to end while loop
       if (this->attacks == 0)
       {
           flag = true;//loop will end
       }

   } while (flag == false);

   //integer to determine if damage is doubled
   int totalDamage = rand() % 2;
   if (totalDamage == 1)//50% chance
   {
       cout << "Ice Dragon DOUBLES damage!\n";
       total = total * 2;//double the total damage
   }

   cout << "Total Damage: " << total << "\n";
   return total;//return total damage done
}
//getTyep returns a string that represents the type of Monster. It takes no parameters
string IceDragon::getType()
{
   return this->type;
}

icedragon.hpp

#include "dragon.hpp"
#ifndef ICEDRAGON_HPP
#define ICEDRAGON_HPP

class IceDragon : public Dragon
{
private:
   //member variables
   string type;
   int attacks;
   int strength;
public:
   //default constructor
   IceDragon();

   //attack function
   int attack();

   //getType function
   string getType();
};

#endif

makefile

#target dependencies
#   rule to build
#

all: prog

prog: troll.o myll.o icedragon.o goblin.o firedragon.o dragon.o proj3.o
   g++ monster.hpp troll.o icedragon.o myll.o goblin.o firedragon.o dragon.o proj3.o -o prog

proj3.o: proj3.cpp
   g++ -c proj3.cpp

goblin.o: goblin.cpp
   g++ -c goblin.cpp

troll.o: troll.cpp
   g++ -c troll.cpp

dragon.o: dragon.cpp
   g++ -c dragon.cpp

icedragon.o: icedragon.cpp
   g++ -c icedragon.cpp

firedragon.o: firedragon.cpp
   g++ -c firedragon.cpp

myll.o: myll.cpp
   g++ -c myll.cpp

clean:
   rm *.o prog


moster.hpp
#include <iostream>
#include <string>
using std::string;
#ifndef MONSTER_HPP
#define MONSTER_HPP

class Monster
{
protected:
   //member variables
   int attacks;
   int strength;
   string type;
public:
   //attack function
   virtual int attack() = 0;

   //accessor for type
   virtual string getType() = 0;
};

#endif

myll.cpp
#include "myll.hpp"
#include <iostream>
using std::cout;

myLL::~myLL()
{
   Node *current = head;//make new node and set it to head
   Node *next;//create next Node

   while (current != NULL)//use while loop to loop until NULL comes up
   {
       next = current->next;//set next to current->next. Starts at begging, goes till end
       delete current;//delete the current node
       current = next;//set current back to next
   }
   head = NULL;//set head to NULL
   cout << "List Destroyed!\n";//output that the list has been destroyed
}

void myLL::displayList()
{
   Node *current = head;
   if (current == NULL)//nothing in the list
       cout << "List is empty!\n";

   while (current != NULL)//cout until current != NULL (end of list)
   {
       cout << current->value->getType() << "\n";
       current = current->next;//move to next position
   }
}


void myLL::addValue(Monster *monster)
{
   Node *newNode = new Node;//create a new Node
   newNode->value = monster;//set the value to the Monster passed in
   newNode->next = NULL;//set the next variable to NULL

   if (head == NULL)//head is NULL
       head = newNode;//set head equal to the newNode already created

   else
   {
       Node *temp2 = head;//create a new pointer that starts at head
       while (temp2->next != NULL)//loop until NULL is reached
       {
           temp2 = temp2->next;//move through the list
       }//end of while loop
       temp2->next = newNode;//set the
   }
}

void myLL::removeLast()
{
   if (head == NULL)//nothing there
       cout << "Nothing in the list!\n";

   else if (head->next == NULL)//last item in the list
   {
       delete head;
       head = NULL;
       cout << "Removed from the list. There are now NO elements LEFT!\n";
   }
   else
   {
       Node *nextToEnd = head;//create two Node pointer variables and point them to
       Node *end = head->next;//to head and the value after head.

       while (end->next != NULL)//loop until the next pointer is NULL
       {
           nextToEnd = end;
           end = end->next;
       }
       delete end;//delete
       nextToEnd->next = NULL;//set to NULL
       cout << "Removed from the list!\n";
   }
}

void myLL::attack()
{
   Node *current = head;
   if (current == NULL)//nothing in the list
       cout << "List is empty!\n";

   else if (head->next == NULL)//1 item in the list
       head->value->attack();

   else
   {
       while (current != NULL)//cout until current != NULL (end of list)
       {
           current->value->attack();
           current = current->next;//move to next position
       }
   }
}

myll.hpp

#include "monster.hpp"
#ifndef MYLL_HPP
#define MYLL_HPP

class myLL
{
protected:
   //create a Node structure
   struct Node
   {
       Monster *value;//declare Monster pointer named value
       Node *next;//declare Node element pointer of next

   }; Node *head;
public:
   //constructor that sets the head of the list to NULL
   myLL() { head = NULL; }

   //destructor
   ~myLL();

   //displayList outputs all of the types of monsters in the list
   void displayList();

   //add / remove functions
   void addValue(Monster *);
   void removeLast();

   //uses the attack method for each of the Monster and displays their attacks
   void attack();


};


#endif

proj3.cpp
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "myll.hpp"
#include "monster.hpp"
#include "goblin.hpp"
#include "troll.hpp"
#include "dragon.hpp"
#include "icedragon.hpp"
#include "firedragon.hpp"

void displayMenu();
int getInput();
Monster* addMonster();

int main() {
   srand(static_cast<unsigned int>(time(NULL)));
   myLL list;
   bool done = false;
   int choice = 0;
   Monster *tempMonster = NULL;
   while (done != true) {
       displayMenu();
       choice = getInput();
       switch (choice) {
       case 1:
           list.displayList();
           break;
       case 2:
           tempMonster = addMonster();
           list.addValue(tempMonster);
           break;
       case 3:
           list.removeLast();
           break;
       case 4:
           std::cout << std::endl;
           list.attack();
           break;
       case 5:
           done = true;
           break;
       default:
           std::cout << "Not a valid choice" << std::endl;
           break;
       }
   }
}

void displayMenu() {
   std::cout << std::endl;
   std::cout << "What would you like to do?" << std::endl;
   std::cout << "1) Display list" << std::endl;
   std::cout << "2) Add Monster" << std::endl;
   std::cout << "3) Remove Last Monster" << std::endl;
   std::cout << "4) Monsters Attack!" << std::endl;
   std::cout << "5) Quit" << std::endl;
}

int getInput() {
   int choice;
   std::cout << "Choice: ";
   std::cin >> choice;
   return choice;
}

Monster* addMonster() {
   int monsterType;
   Monster *temp = NULL;
   std::cout << "What type of Monster do you wish to add?" << std::endl;
   std::cout << "1) Goblin" << std::endl;
   std::cout << "2) Troll" << std::endl;
   std::cout << "3) Dragon" << std::endl;
   std::cout << "4) Ice Dragon" << std::endl;
   std::cout << "5) Fire Dragon" << std::endl;

   do {
       std::cout << "Choice: ";
       std::cin >> monsterType;
       switch (monsterType) {
       case 1:
           return new Goblin;
       case 2:
           return new Troll;
       case 3:
           return new Dragon;
       case 4:
           return new IceDragon;
       case 5:
           return new FireDragon;
       default:
           std::cout << "Not a valid choice, try again" << std::endl;
           break;
       }
   } while (true);
}

troll.cpp
#include "troll.hpp"
#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout;

//default constructor. Initializes member variables
Troll::Troll()
{
   this->attacks = 2;
   this->strength = 60;
   this->type = "Troll";
}

int Troll::attack()
{  
   //initialize the variable total that will be used to keep track of all damage done
   int total = 0;

   //loop until attacks = 0. For Troll class, will always be 2 iterations.
   while (this->attacks > 0)
   {
       //set flag to determine if there is a critical hit
       bool flag = false;

       //determine the amount of attack damage is done and store it in variable "attack"
       int attack = 1 + (rand() % this->strength);//attack is equal to random amount of strength

       //Troll's special makes his attack a critical hit. 20% chance
       //If the number is shown, then it is a crit and the attack is set to 100
       int special = rand() % 5;
       if (special == 1)//20% chance
       {
           flag = true;//set flag to true (Crit happened)      
           attack = 100;//set attack to 100
       }

       if (flag == true)//crit happened
           cout << "Troll attacks for CRITICAL damage " << attack << "\n";//display damage

       else
           cout << "Troll attacks for " << attack << " damage\n";

       //increment the total damage
       total += attack;
       this->attacks--;//decrement the member variable attacks
   }

   //display the total damage dealt
   cout << "Total Damage: " << total << "\n";
   return total;//return integer value total  
}

//getTyep returns a string that represents the type of Monster. It takes no parameters
string Troll::getType()
{
   return this->type;
}

troll.hpp
#include "monster.hpp"
#ifndef TROLL_HPP
#define TROLL_HPP

class Troll : public Monster
{
private:
   //member variables
   string type;
   int attacks;
   int strength;
public:
   //default constructor
   Troll();

   //attack function
   int attack();

   //getType function
   string getType();
};

#endif

Add a comment
Know the answer?
Add Answer to:
using C++ Requirements: . Classes you must create o Monster must be an abstract class Each monster has . an attack) o randomly generate a number between 0 and strength for each attack (Strength 10 wo...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 5: Monster Factory (10 points) (Game Dev) Create a Monster class that maintains a count...

    Problem 5: Monster Factory (10 points) (Game Dev) Create a Monster class that maintains a count of all monsters instantiated and includes a static method that generates a new random monster object. In software engineering, a method that generates new instances of classes based on configuration information is called the Factory pattern. UML Class Diagram: Monster - name: String - health: int - strength: int - xp: int + spawn(type:String): Monster + constructor (name: String, health: int, strength: int, xp:...

  • Question 1. (Magician.java, Healer.java, Fighter.java, HeroTester.java) Consider the abstract hero class and the three subclasses shown...

    Question 1. (Magician.java, Healer.java, Fighter.java, HeroTester.java) Consider the abstract hero class and the three subclasses shown below. Review the hero class to see how it works (it’s in the project/package for you already). Your job is to implement the following subclasses. Each of the subclasses has an extra data field that relates to their specific hero talent. Some help with these special talents and other methods are below. Fighter: ‐The fighter has an extra data field called Strength and a...

  • C++ Inheritance Problem Step a: Suppose you are creating a fantasy role-playing game. In this game...

    C++ Inheritance Problem Step a: Suppose you are creating a fantasy role-playing game. In this game we have four different types of Creatures: Humans, Cyberdemons, Balrogs, and elves. To represent one of these Creatures we might define a Creature class as follows: class Creature { private: int type; // 0 Human, 1 Cyberdemon, 2 Balrog, 3 elf int strength; // how much damage this Creature inflicts int hitpoints; // how much damage this Creature can sustain string getSpecies() const; //...

  • HIGHEST SUBMISSION Vlew All Submissions Submission 17 Submitted on 6/10/2019 12 35 PM by Jenna Saleh...

    HIGHEST SUBMISSION Vlew All Submissions Submission 17 Submitted on 6/10/2019 12 35 PM by Jenna Saleh DESCRIPTION Objectives To write a classes based on sets of specifications To write a main class to be used in a multi-class Java program To use inheritance to extend a class (optional, EC) To override methods in subclasses (optional, EC) Groups You may work with a partner on this assignment. A header comment (In every Java file) should include authors (group members) and collaborators...

  • Summary task: C++ language; practice combining tools(functions, arrays, different kind of loops) to solve somewhat complex...

    Summary task: C++ language; practice combining tools(functions, arrays, different kind of loops) to solve somewhat complex problem. Description A Valiant Hero is about to go on a quest to defeat a Vile Monster. However, the Hero is also quite clever and wants to be prepared for the battle ahead. For this question, you will write a program that will simulate the results of the upcoming battle so as to help the hero make the proper preparations. Part 1 First, you...

  • Program must be written in C++: Create a parent class called CompSciProfessor. Each professor has name...

    Program must be written in C++: Create a parent class called CompSciProfessor. Each professor has name (type string), email (type string), and facultyId (type long). Specialize the CompSciProfessor class into two more classes: AdjunctProf, and TenureTrackProf classes. The specialized class AdjunctProf has three attributes of its own: degree (type char), NoOfTA (type int), and NoOfCourses (type int). The attribute ‘degree’ refers to the degree of the adjunct professor. You assign ‘B’ to represent bachelor degree, ‘M’ for Master degree, and...

  • C++ Project Overview The 18th season of the reality series Hell's Kitchen began airing on Sep 28th, 2018 on Fox. Suppose you are working for the boss, Chef Gordon Ramsay, your job is to create an...

    C++ Project Overview The 18th season of the reality series Hell's Kitchen began airing on Sep 28th, 2018 on Fox. Suppose you are working for the boss, Chef Gordon Ramsay, your job is to create an efficient system that generates a menu. Since in Hell's Kitchen, menu changes every day, your system should easily add items to the menu. Therefore, dynamically allocated arrays would be an excellent solution. The Dish Class (Header File) You need to create a struct called...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT