Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to finish a project for my c++ class. I have stumped into many error

ID: 653545 • Letter: I

Question

I am trying to finish a project for my c++ class. I have stumped into many errors while trying to debug the code that I have done. If you can see any errors that I need to fix, please let me know. Some of the files were already given to us and should not be changed.

//PROJECT ASSIGNMENT

The goal of the project is to create a fantasy game involving battles between two armies. Each army is composed of only one of the following creatures - demon, balrog, elf and orc. Each creature in the army is randomly assigned a single weapon from a collection of weapons. Each weapon has a pre-assigned number of damage points. Also, each creature is randomly assigned a health from 1 to 100. The damage inflicted by a creature depends on the type of creature. Demons have a 25% chance of inflicting a demonic attack, which is an additional 50 damage points. Elves have a 15% chance to inflict a magical attack that doubles the normal amount of damage. Balrogs are fast, so they get to attack twice. Orcs have no special powers.

The user launches the game by specifying the type and the number of creatures composing each army. A round of fight between the two armies is as follows-
1. Randomly pick up a creature (attacker) from the first army.
2. Randomly pick up a creature (victim) from the second army.
3. Subtract the attacker's weapon damage from the victim's health
4. Repeat step 1-3 with the attacker drawn from the second army and the victim drawn from the first army.

The statistics reported includes the total health of each army and the creature with the maximum health from each army.

The user is given an option to play another round. If however, the total health of either army is less than zero, the game stops. The creature name from the winning army is printed.

You are given the following header files-
1. creature.h - Base class for the creature. Other creatures must be derived by appropriately redefining the virtual function.
2. battle.h - Class for the battle. Data includes a vector of weapons, and a vector each for the two armies. All objects are dynamically allocated
3. Weapons.h - Class for the weapons.
4. creatureFactory.cpp - The implementation of a factory class that generates the appropriate object based on the user input
5. main.cpp - Describes the actual battle

Your goal is to complete the code by writing the following -
1. creature.cpp - Implementation of the Creature class
2. demon.h and demon.cpp - Declaration and implementation of the Demon class
3. elf.h and elf.cpp - Declaration and implementation of the Elf class
4. balrog.h and balrog.cpp - Declaration and implementation of the balrog class
5. battle.cpp - Implementation of the Battle class methods.
Note that due to polymorphism adding any new Creature type requires only update of the list in main.cpp and the creatureFactory class. The battle remains unchanged.

You are strongly encouraged to test each implementation file. So additional test files that you may have to write include weaponTest.cpp, creatureTest.cpp, demonTest.cpp, elfTest.cpp, balrogTest.cpp and battleTest.cpp.

The project is considered done when ./helmsDeep <name of first creature> <number in creatures in the first army> <name of second creature> <number of creatures in the second army> executes correctly for any combination of creatures.

//Main.cpp

#include <algorithm>
#include <stdlib.h>
#include "battle.h"

using std::find;

int main(int argc,char* argv[])
{
   const int MAX_CREATURES = 10;

   cout << endl << "Welcome to the great Battle of Helm's Deep" << endl;
  
   // Create a vector of creature types
   vector<string> creatureTypes;
   creatureTypes.push_back("elf");
   creatureTypes.push_back("demon");
   creatureTypes.push_back("balrog");
   creatureTypes.push_back("orcs");


   // Verify if the user input is correct
   if (argc != 5) {
       cout << "Wrong number of input arguments" << endl;
       cout << "Usage is ./helmsDeep <name of first creature> <number in creatures in the first army> <name of second creature> <number of creatures in the second army> " << endl;
       return -1;
   }
  
   string arg1(argv[1]);
   int arg2 = atoi(argv[2]);
   string arg3(argv[3]);
   int arg4 = atoi(argv[4]);

   std::vector<string>::iterator it;
   it = find(creatureTypes.begin(), creatureTypes.end(), arg1);
   if (it != creatureTypes.end())
           std::cout << "Found " << *it << endl;
   else {
           std::cout << "Creature not found " << endl;;
       return -1;
   }
   it = find(creatureTypes.begin(), creatureTypes.end(), arg3);
   if (it != creatureTypes.end())
           std::cout << "Found " << *it << endl;
   else {
           std::cout << "Creature not found " << endl;;
       return -1;
   }
   if ((arg2 < 0) || (arg4 < 0)) {
       cout << "Invalid number of creatures" << endl;
       return -1;
   }
   if ((arg2 > MAX_CREATURES) || (arg4 > MAX_CREATURES)) {
       cout << "Too many creatures. Only " << MAX_CREATURES << " allowed" << endl;
       return -1;
   }

  

   cout << endl << " The Battle of Helm's Deep starts now..." << endl;
      
   // Initialize the battle object
   Battle helms_deep;

   // Initialize the weapons
   helms_deep.createWeapons();

   // Create the two armies
   helms_deep.createArmyCreature1(arg1,arg2);
   helms_deep.createArmyCreature2(arg3,arg4);
   helms_deep.reportArmyCreature1();
   helms_deep.reportArmyCreature2();

   cout << endl << endl;

   // Print the intial stats
   cout << "Initial stats" << endl;
   helms_deep.reportStats();
   cout << endl;

   // Battle until user decides to stop
   char ans = 'n';
   int round = 0;
   do {
       helms_deep.creature1Attacks();
       helms_deep.creature2Attacks();
       round++;
       cout << " Stats at the end of round: " << round << endl;
       helms_deep.reportStats();
       cout << endl;
       if (!helms_deep.areCreaturesAlive()) {
           cout << "Creatures are dead " << endl;
           break;
       }
       cout << "Another round? (y/Y) " << endl;
       cin >> ans;
   } while ((ans == 'y') || (ans == 'Y'));

   // Output the winner
   cout << "The result of the battle is " << helms_deep.victor() << " wins" << endl;
   cout << " End of the Battle of Helm's Deep" << endl;

   return 0;
}

//Creature.h

#ifndef CREATURE_H
#define CREATURE_H

#include <iostream>
#include <string>
#include "weapons.h"


using std::string;

class Creature
{
   protected:
       string creature_type;
       Weapon* creature_weapon;
       int creature_health;
   public:
       Creature();
       Creature(string new_type, string weapon_type, int damage, int new_health);
       virtual ~Creature();
       string getCreatureType() {return creature_type;}
       virtual Weapon* getCreatureWeapon() {return creature_weapon;}
       int getHealth(){return creature_health;}
       void setCreatureType(string new_type) {creature_type = new_type;}
       void setWeapon(string weapon_type, int damage);
       void setHealth(int new_health) {creature_health = new_health;}
};

class CreatureCompare
{
public:
   bool operator() (Creature* creature1, Creature* creature2) const
   {
       return creature1->getHealth() > creature2->getHealth();
   }


};

//creatureFactory.cpp

class CreatureFactory {
   public:
       Creature* create(string creature_name);
};


#endif


//Creature.cpp

#include <iostream>
#include "creature.h"
#include "weapons.h"
#include <string>

using std::string;

Creature::Creature()
{
    creature_type="unknown";
    creature_weapon=NULL;
    creature_health = 0;
}

Creature::Creature(string new_type, string weapon_type, int damage, int new_health)
{

    creature_type = new_type;
    creature_weapon -> getWeaponType();
    creature_weapon -> getDamage();
    creature_health = new_health;
  
}

Creature::~Creature()
{
    delete [] creature_weapon;
}

void Creature::setWeapon(string weapon_type, int damage){
   //string w_type = getWeaponType();
   //int dmg = getDamage();
   creature_weapon -> setWeaponType(weapon_type);
   creature_weapon -> setDamage(damage);
   creature_weapon -> getWeaponType();
   creature_weapon -> getDamage();
  
}


//Weapons.h

#ifndef WEAPONS_H
#define WEAPONS_H

#include <iostream>
#include <string>

using std::string;

class Weapon
{
   private:
       string weapon_type;
       int damage;
   public:
       Weapon(){weapon_type = "unknown"; damage = 0;}
       Weapon(string new_weapon, int new_damage){weapon_type = new_weapon; damage = new_damage;}
       string getWeaponType() { return weapon_type;}
       int getDamage() { return damage;}
       void setWeaponType(string new_weapon) {weapon_type = new_weapon;}
       void setDamage(int new_damage) {damage = new_damage;}
};

#endif


//creatureFactory.cpp

#include "creature.h"
#include "demon.h"
#include "elf.h"
#include "balrog.h"
// Factory class to that creates creature objects
Creature* CreatureFactory::create(string creature_name)
{

   if (creature_name == "demon")
       return new Demon;
   else if (creature_name == "elf")
       return new Elf;
   else if (creature_name == "balrog")
       return new Balrog;
   else
       return new Creature;
          
}

//balrog.h

#ifndef BALROG_H
#define BALROG_H

#include <iostream>
#include <string>
#include "creature.h"
#include "weapons.h"

using std::string;

class Balrog : public Creature{
   public:
       Balrog();

       virtual Weapon* getCreatureWeapon();
      
};

#endif

//Balrog.cpp

#include <iostream>
#include <string>
#include "creature.h"
#include "weapons.h"
#include "balrog.h"

using std::string;
using std::cin;
using std::cout;

Weapon* getCreatureWeapon(){
       cout << "Balrog hits 2x as fast!" << endl;
           creature_weapon -> damage = damage*2;
}


//elf.h

#ifndef ELF_H
#define ELF_H

#include <iostream>
#include <string>
#include "creature.h"
#include "weapons.h"

using std::string;

class Elf : public Creature{
   public:
       Elf();
       ~Elf();
       virtual Weapon* getCreatureWeapon();
      
};

#endif


//elf.cpp

#include <iostream>
#include <string>
#include "creature.h"
#include "weapons.h"
#include "elf.h"

using std::string;
using std::cin;
using std::cout;
using std::endl;

Elf::Elf()
{
    creature_type="Elf";
    creature_weapon=NULL;
    creature_health = 0;
}

Elf::~Elf()
{
   delete creature_weapon;
}

Weapon* Creature::getCreatureWeapon(){
   int chance = rand()%100+1;
  
   if(chance>0 && chance<=15) {
       cout << "Magic attack inflicts 2x damage!" << endl;
           creature_weapon -> creature_weapon.setDamage(creature_weapon.getDamage()*2);
   }
}
  

}


//demon.h

#ifndef DEMON_H
#define DEMON_H

#include <iostream>
#include <string>
#include "creature.h"
#include "weapons.h"

using std::string;

class Demon : public Creature{
   public:
       Demon();
      
       virtual Weapon* getCreatureWeapon();
      
};

#endif


//demon.cpp

#include <iostream>
#include <string>
#include "creature.h"
#include "weapons.h"
#include "demon.h"

using std::string;
using std::cin;
using std::cout;

Weapon* getCreatureWeapon(){
   chance = rand()%100+1;
  
   if(chance>0 && chance<=25) {
       cout << "Demon inflicts 50 more damage points!" << endl;
           creature_weapon -> damage = damage+50;
   }
}

}

//battle.h

#ifndef BATTLE_H
#define BATTLE_H

#include "creature.h"
#include "weapons.h"
#include "time.h"
#include <vector>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;

class Battle {
   private:
       vector<Weapon*> weapon_types;
       vector<Creature*> creature1_army;
       vector<Creature*> creature2_army;
   public:
       Battle();
       Battle(const Battle& battleObj);
       ~Battle();
       void createWeapons();
       void createArmyCreature1(string name_creature, int num_creature);
       void createArmyCreature2(string name_creature, int num_creature);
       void reportStats();
       void reportWeaponTypes();
       void reportArmyCreature1();
       void reportArmyCreature2();
       void creature1Attacks();
       void creature2Attacks();
       bool areCreaturesAlive();
       string victor();


};

#endif

//battle.cpp

#include <iostream>
#include <string>
#include <vector>
#include "creature.h"
#include "weapons.h"
#include "battle.h"

using namespace std;

Battle::Battle(){
}

Battle::Battle(const Battle& battleObj){
  
}

Battle::~Battle(){
  
}

void Battle::createWeapons(){

   weapon_types.push_back(new Weapon("Sword", 5));
   weapon_types.push_back(new Weapon("Machete", 10));
   weapon_types.push_back(new Weapon("Greatsword", 20));
   weapon_types.push_back(new Weapon("Bow", 15));
   weapon_types.push_back(new Weapon("Spear", 15));


       /*switch(weapon_types){
           case 1: Weapon("Stick", 5);
               break;
           case 2: Weapon("Machete", 10);
               break;
           case 3: Weapon("Greatsword", 20);
               break;
           case 4: Weapon("Bow", 15);
               break;
           case 5: Weapon("Spear", 15);
       }*/
      
}

void Battle::createArmyCreature1(string name_creature, int num_creature){
   CreatureFactory Factory;
   for(unsigned int i=0; i < num_creature; i++){
       int weapon_chance = rand()%5;
       creature1_army[i] -> setHealth(rand()%100+1);
       creature1_army.push_back(Factory.create(name_creature));
       creature1_army[i] -> setWeapon(weapon_types[weapon_chance]->getWeaponType(),weapon_types[weapon_chance]->getDamage());
   }
}

void Battle::createArmyCreature2(string name_creature, int num_creature){
   CreatureFactory Factory;
   for(unsigned int i=0; i < num_creature; i++){
       int weapon_chance = rand()%5;
       string w_type = weapon_types[weapon_chance]->getWeaponType();
       int w_dmg = weapon_types[weapon_chance]->getDamage();
       creature2_army[i] -> setHealth(rand()%100+1);
       creature2_army.push_back(Factory.create(name_creature));
       creature2_army[i] -> setWeapon(w_type,w_dmg);
   }
}

void Battle::reportArmyCreature1(){
       cout << "Creatures of Army 1: " << creature1_army[0]<< endl;
      
   for(unsigned int i=0; i < creature1_army.size(); i++){
       cout << "Monster " << i+1 << " HP: " << creature1_army[i]-> getHealth() << "Weapon Type: " << creature1_army[i] -> getCreatureWeapon() -> getWeaponType() << "Damage of Weapon: " << creature1_army[i] -> getCreatureWeapon()-> getDamage() << endl;
   }
}

void Battle::reportArmyCreature2(){
       cout << "Creatures of Army 2: " << creature2_army[0]<< endl;
      
   for(unsigned int i=0; i < creature2_army.size(); i++){
       cout << "Monster " << i+1 << " HP: " << creature2_army[i]-> getHealth() << "Weapon Type: " << creature2_army[i] -> getCreatureWeapon()-> getWeaponType() << "Damage of Weapon: " << creature2_army[i] -> getCreatureWeapon()-> getDamage() << endl;
   }
}

void Battle::reportStats(){
       int Army1HP = creature1_army[0]-> getHealth();
       int Army2HP = creature2_army[0]-> getHealth();
       int MaxHP1 = creature1_army[0] -> getHealth();
       int MaxHP2 = creature2_army[0] -> getHealth();


   for(unsigned int i=1; i < creature1_army.size(); i++){
       Army1HP += creature1_army[i]-> getHealth();
   }
   cout << "Total HP for Army 1: " << Army1HP << endl;


   for(unsigned int i=1; i < creature1_army.size(); i++){
      
       if((creature1_army[i] -> getHealth()) > MaxHP1){
           MaxHP1 = (creature1_army[i] -> getHealth());
       }      
   }


   for(unsigned int i=0; i < creature1_army.size(); i++){
       if((creature1_army[i] -> getHealth()) == MaxHP1){
           cout << "Creature with most Health in Army 1 is Monster " << i << "with HP: " << MaxHP1 << endl;
       }
   }

  
   for(unsigned int i=1; i < creature2_army.size(); i++){
       Army2HP += creature2_army[i]-> getHealth();
   }
   cout << "Total HP for Army 2: " << Army2HP << endl;

  
   for(unsigned int i=1; i < creature2_army.size(); i++){
      
       if((creature2_army[i] -> getHealth()) > MaxHP2){
           MaxHP2 = (creature2_army[i] -> getHealth());
       }
   }

   for(unsigned int i=0; i < creature2_army.size(); i++){
       if((creature2_army[i] -> getHealth()) == MaxHP2){
           cout << "Creature with most Health in Army 2 is Monster " << i << "with HP: " << MaxHP2 << endl;
       }
   }
}

//void Battle::reportWeaponTypes(){}


void Battle::creature1Attacks(){
   int random = rand()%creature2_army.size();
   int newHealth = creature2_army[random] -> getHealth() - creature1_army[rand()%creature1_army.size()] -> getCreatureWeapon() -> getDamage();
   creature2_army[random] -> setHealth(newHealth);

   for(unsigned int i=0; i < creature2_army.size(); i++){
       if((creature2_army[i] -> getHealth()) <= 0){
           creature2_army.pop_back();
       }
   }
}

void Battle::creature2Attacks(){
   int random = rand()%creature1_army.size();
   int newHealth = creature1_army[random] -> getHealth() - creature2_army[rand()%creature2_army.size()] -> getCreatureWeapon() -> getDamage();
   creature1_army[random] -> setHealth(newHealth);

   for(unsigned int i=0; i < creature1_army.size(); i++){
       if((creature1_army[i] -> getHealth()) <= 0){
           creature1_army.pop_back();
       }
   }
}

bool Battle::areCreaturesAlive(){

   bool alive = true;

   if(creature1_army.size() == 0){
           alive = false;
   }


   if(creature2_army.size() == 0){
           alive = false;
   }
   return alive;
}

string Battle::victor(){
   int Army1HP = creature1_army[0]-> getHealth();
   int Army2HP = creature2_army[0]-> getHealth();


   for(unsigned int i=1; i < creature1_army.size(); i++){
       Army1HP += creature1_army[i]-> getHealth();
   }

   for(unsigned int i=1; i < creature2_army.size(); i++){
       Army2HP += creature2_army[i]-> getHealth();

}}

//Some Errors

In creatureFactory:

undefined reference to Elf()::elf()

undefined reference to Balrog()::Balrog()

undefined reference to Demon()::Demon()

In creature.cpp

undefined reference to main()

In battle.cpp

In function `Battle::createArmyCreature2(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)':
battle.cpp:(.text+0xbbc): undefined reference to `CreatureFactory::create(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
battle.cpp:(.text+0xc1f): undefined reference to `Creature::setWeapon(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
/tmp/ccXeS7F6.o: In function `Battle::createArmyCreature1(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)':
battle.cpp:(.text+0xd3f): undefined reference to `CreatureFactory::create(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
battle.cpp:(.text+0xdaf): undefined reference to `Creature::setWeapon(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
collect2: ld returned 1 exit status

In elf.cpp

elf.cpp:24: error: redefinition of

Explanation / Answer

in creaturefactory - do not use () in the declaration part - it should be ELf::elf() and so on

creature.cpp - main() should be defined - you have not defined a main();

in battle.cpp - the function parameters are different - what is being defined and what is being passed

in elf.cpp the function getc.. is being defined again - that should be a inheritance or it should have a different name

similarly you can debug the others

hope that is helpful

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote