title: Combat Game - C++, Given a Spawner Class, How can I spawn stronger enemy every round
body: <p>I have a problem in my OOP homework. I have to code a 1 v 1 battle.</p>
  
  <p>This is a Survival Game where in the player fights continuously with enemies until the player dies. The Player gets stronger as he advances through. The enemy also grows stronger every round.</p>
  
  <p>I have created two Classes, The Unit and the Spawner.
  We cannot use Inheritance nor Polymorphism.
  We Have to use only the simple definition of the class.</p>
  
  <p>My problem is that i cannot think of a SIMPLE way to make the class Spawner spawn a stronger enemy every round. </p>
  
  <p>I have thought of overloading the Unit's Contructor but i have trouble manipulating that. </p>
  
  <p>Would anyone please help?</p>
  
  <p>Here's my Unit Header file (Unit.h):</p>
  
  <pre><code>#pragma once
  
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;ctime&gt;
  
  
  using namespace std;
  
  // Class Types
  const string CLASS_WARRIOR  = "Warrior" ;
  const string CLASS_ASSASSIN = "Assassin";
  const string CLASS_MAGE     = "Mage"    ;
  
  // Class Choices
  const int CHOICE_WARRIOR    = 1;
  const int CHOICE_ASSASSIN   = 2;
  const int CHOICE_MAGE       = 3;
  
  // Multipliers
  const double MULTIPLIER_DAMAGE  = 0.50f;
  const double MULTIPLER_HEAL     = 0.30f;
  
  // Advantage
  const bool ADVANTAGE = true;
  
  // Win Bonus
  const int BONUS_THREE   = 3;
  const int BONUS_FIVE    = 5;
  
  // Minimum and maximum values
  const int HIT_RATE_MAX = 80;
  const int HIT_RATE_MIN = 20;
  const int DAMAGE_MIN = 1;
  
  // Hit or miss
  const bool ATTACK_HIT = true;
  const bool ATTACK_MISS = false;
  
  class Unit
  {
  public:
      // Constructors
      Unit(string name, int classChoice);
  
      // Getters and setters
      string getName();
      string getClass();
  
      int getHp();
      int getMaxHp();
      int getPower();
      int getVitality();
      int getAgility();
      int getDexterity();
  
      int getDamage();
      int getHeal();
      int getBonusDamage();
      bool getFirstToAttack();
      bool getHit();
  
      void setClassStats(const int classChoice);
  
      // Primary Actions
      void attack(Unit* target);
  
      // Secondary Actions
      void check(const Unit* target); // Inspects the enemy
      void lvlUp();               // Grants this Unit Bonus Stats on Victory
  
  
  private:
      // Info
      string mName;
      string mClass;
  
      // Basic Stats
      int mHp;
      int mMaxHp;
      int mPower;
      int mVitality;
      int mAgility;
      int mDexterity;
  
      // Derived Stats
      int mDamage;
      int mHitrate;
      int  mHeal;
      int  mBonusDamage;
  
      // Miscellaneous 
      bool mAdvantage;    // If the player has the advantage
      string mTargetClass;// This Unit keeps in mind the class of this Unit's Opponent for reference
      int mChanceOfHit;   // The random chance if the attack will Hit/Miss
      bool mFirstToAttack;//if this unit will attack first
      bool mHit;          // if the attack is a hit or miss
  };
  </code></pre>
  
  <p>Here's my Unit.cpp file (Unit.cpp):</p>
  
  <pre><code>#include "Unit.h"
  
  
  Unit::Unit(string name, int classChoice)
  {
      mName = name;
  
      setClassStats(classChoice);
  }
  
  string Unit::getName()
  {
      return mName;
  }
  
  string Unit::getClass()
  {
      return mClass;
  }
  
  int Unit::getHp()
  {
      return mHp;
  }
  
  int Unit::getMaxHp()
  {
      return mMaxHp;
  }
  
  int Unit::getPower()
  {
      return mPower;
  }
  
  int Unit::getVitality()
  {
      return mVitality;
  }
  
  int Unit::getAgility()
  {
      return mAgility;
  }
  
  int Unit::getDexterity()
  {
      return mDexterity;
  }
  
  int Unit::getDamage()
  {
      return mDamage;
  }
  
  int Unit::getHeal()
  {
      return mHeal;
  }
  
  int Unit::getBonusDamage()
  {
      return mBonusDamage;
  }
  
  bool Unit::getFirstToAttack()
  {
      return mFirstToAttack;
  }
  
  bool Unit::getHit()
  {
      return mHit;
  }
  
  void Unit::setClassStats(const int classChoice)
  {
      if (classChoice == CHOICE_WARRIOR) {
          mClass      = CLASS_WARRIOR;
          mMaxHp = 20;
          mHp = mMaxHp;
          mPower      = 15;
          mVitality   = 10;
          mAgility    = 7;
          mDexterity  = 7;
  
          return;
      }
  
      else if (classChoice == CHOICE_ASSASSIN) {
          mClass      = CLASS_ASSASSIN;
          mMaxHp = 15;
          mHp         = mMaxHp;
          mPower      = 17;
          mVitality   = 8;
          mAgility    = 12;
          mDexterity  = 10;
  
          return;
      }
  
      else if (classChoice == CHOICE_MAGE) {
          mClass      = CLASS_MAGE;
          mMaxHp          = 30;
          mHp = mMaxHp;
          mPower      = 12;
          mVitality   = 12;
          mAgility    = 9;
          mDexterity  = 8;
  
          return;
      }
  
      throw exception("Error! Class not part of this game. ");
  }
  
  void Unit::attack(Unit * target)
  {
      // Determine Whether the attack will Hit/Miss Based on the hit rate
      mChanceOfHit = rrand() % 100 + 1;
  
      if (mChanceOfHit &gt; mHitrate) {
          mHit = ATTACK_MISS;
          return;
      }
  
  
      // Deducts the targets Hp by the Damage given by this Unit (if the attack didn't miss)
      target-&gt;mHp -= mDamage;
      mHit = ATTACK_HIT;
  
      // if target HP is negative, set it to zero
      if (target-&gt;mHp &lt; 0) target-&gt;mHp = 0;
  }
  
  void Unit::check(const Unit * target)
  {
      // The Unit Keeps in Mind his target's Class
      if (target-&gt;mClass == CLASS_WARRIOR)        mTargetClass = CLASS_WARRIOR;
      else if (target-&gt;mClass == CLASS_ASSASSIN)  mTargetClass = CLASS_ASSASSIN;
      else if (target-&gt;mClass == CLASS_MAGE)      mTargetClass = CLASS_MAGE;
  
  
      // Checks if the Unit is Advantageous Against his Target
      if (mClass == CLASS_WARRIOR) {
          if (target-&gt;mClass == CLASS_ASSASSIN) mAdvantage = ADVANTAGE;
          else mAdvantage = false;
      }
  
      else if (mClass == CLASS_ASSASSIN) {
          if (target-&gt;mClass == CLASS_MAGE) mAdvantage = ADVANTAGE;
          else mAdvantage = false;
      }
  
      else if (mClass == CLASS_MAGE) {
          if (target-&gt;mClass == CLASS_WARRIOR) mAdvantage = ADVANTAGE;
          else mAdvantage = false;
      }
  
      // Determine if this Unit Will Attack first
      if (mAgility &gt;= target-&gt;mAgility) mFirstToAttack = true;
      else mFirstToAttack = false;
  
      // Determines Damage, Bonus Damage ( If Applicable ), and Hit Rate Based on targets stats
      mDamage = mPower - target-&gt;mVitality;
      if (mDamage &lt; DAMAGE_MIN) mDamage = DAMAGE_MIN;
  
      mBonusDamage = mDamage * MULTIPLIER_DAMAGE;
  
      // Evaluates Damage Based on advantage
      if (mAdvantage) mDamage += mBonusDamage;
  
      mHitrate = ((float)mDexterity / (float)target-&gt;mAgility) * 100;
  
      //  Clamps the Hit Rate within the Hit Rate Range
      if (mHitrate &gt; HIT_RATE_MAX) mHitrate = HIT_RATE_MAX;
      else if (mHitrate &lt; HIT_RATE_MIN) mHitrate = HIT_RATE_MIN;
  }
  
  void Unit::lvlUp()
  {
      // Determine Win Bonus Based on the target that he kept in mind
      if (mTargetClass == CLASS_WARRIOR) {
          mMaxHp += BONUS_THREE;
          mVitality += BONUS_THREE;
      }
  
      else if (mTargetClass == CLASS_ASSASSIN) {
          mAgility += BONUS_THREE;
          mDexterity += BONUS_THREE;
      }
  
      else if (mTargetClass == CLASS_MAGE) {
          mPower += BONUS_FIVE;
      }
  
      // Heals the player 30% of his Maximum HP
      mHeal = mMaxHp * MULTIPLER_HEAL;
      mHp += mHeal;
  }
  </code></pre>
  
  <p>Here's my Spawner Header file (Spawner.h):</p>
  
  <pre><code> #pragma once
  
   #include "Unit.h"
  
   class Spawner
   {
   public:
        Spawner();
        ~Spawner();
         void spawn(Unit*&amp; unit);
  
    private:
        Unit* mUnit;
        int mRandomClass;
   };
  </code></pre>
  
  <p>Here's my Spawner.cpp file (Spawner.cpp):</p>
  
  <pre><code>#include "Spawner.h"
  
  
  
  Spawner::Spawner()
  {
  }
  
  Spawner::~Spawner()
  {
      delete mUnit;
      mUnit = NULL;
  }
  
  void Spawner::spawn(Unit *&amp; unit)
  {
      mRandomClass = rand() % 3 + 1;
  
      unit = new Unit("Enemy", mRandomClass);
      mUnit = unit;
  }
  </code></pre>
  
  <p>And finally here is my main.cpp:</p>
  
  <pre><code>#include "Unit.h"
  #include "Spawner.h"
  
  // Create player
  Unit* createPlayer();
  
  // Display Stats
  void displayStats(Unit* unit);
  
  // Play 1 round
  void playRound(Unit*player, Unit* enemy);
  
  // Simulate 1 attack (implement Higher agility gets to atttack first)
  void combat(Unit* player, Unit* enemy);
  
  
  
  void main()
  {
      srand(time(NULL));
      Unit* player = createPlayer();
      Unit* enemy = NULL;
  
      Spawner *spawner = new Spawner();
  
      int round = 1;
  
      while (true) {
          cout &lt;&lt; "Round " &lt;&lt; round &lt;&lt; endl;
          spawner-&gt;spawn(enemy);
  
          player-&gt;check(enemy);
          enemy-&gt;check(player);
  
          playRound(player, enemy);
  
          // if player is dead, end the game
          if (player-&gt;getHp() == 0) break;
  
          // else, add stats
          player-&gt;lvlUp();
  
          delete enemy;
          enemy = NULL;
  
  
          cout &lt;&lt; "You defeted an enemy" &lt;&lt; endl;
          system("pause&gt;nul");
          system("cls");
  
          round++;
      }
  
      cout &lt;&lt; "You were defeted! " &lt;&lt; endl;
      cout &lt;&lt; "You survived until Round " &lt;&lt; round &lt;&lt; endl;
      displayStats(player);
  
      delete player;
      player = NULL;
      delete spawner;
      spawner = NULL;
      system("pause&gt;nul");
      system("cls");
  
  
  }
  
  Unit* createPlayer()
  {
      Unit* unit = NULL;
      string name;
      int classChoice;
  
      cout &lt;&lt; "Enter your Name: ";
      getline(cin, name);
      system("cls");
  
      cout &lt;&lt; "Pick a class: " &lt;&lt; endl
          &lt;&lt; "\t [ 1 ] Warrior" &lt;&lt; endl
          &lt;&lt; "\t [ 2 ] Assassin" &lt;&lt; endl
          &lt;&lt; "\t [ 3 ] Mage" &lt;&lt; endl
          &lt;&lt; endl;
      cin &gt;&gt; classChoice;
  
      while (classChoice &gt; 3 || classChoice &lt;= 0) {
          cout &lt;&lt; "INVALID INPUT! Please try again" &lt;&lt; endl;
          cout &lt;&lt; "Pick a class: " &lt;&lt; endl
              &lt;&lt; "\t [ 1 ] Warrior" &lt;&lt; endl
              &lt;&lt; "\t [ 2 ] Assassin" &lt;&lt; endl
              &lt;&lt; "\t [ 3 ] Mage" &lt;&lt; endl
              &lt;&lt; endl;
          cin &gt;&gt; classChoice;
      }
  
      unit = new Unit(name, classChoice);
  
      return unit;
  }
  
  void displayStats(Unit* unit)
  {
      cout &lt;&lt; "=========================================================================================" &lt;&lt; endl
          &lt;&lt; "Name: " &lt;&lt; unit-&gt;getName() &lt;&lt; " \t \t \t HP: " &lt;&lt; unit-&gt;getHp() &lt;&lt; " / " &lt;&lt; unit-&gt;getMaxHp() &lt;&lt; endl
          &lt;&lt; "Class: " &lt;&lt; unit-&gt;getClass() &lt;&lt; endl
          &lt;&lt; "==========================================================================================" &lt;&lt; endl
          &lt;&lt; "POW: " &lt;&lt; unit-&gt;getPower() &lt;&lt; endl
          &lt;&lt; "VIT: " &lt;&lt; unit-&gt;getVitality() &lt;&lt; endl
          &lt;&lt; "AGI: " &lt;&lt; unit-&gt;getAgility() &lt;&lt; endl
          &lt;&lt; "DEX: " &lt;&lt; unit-&gt;getDexterity() &lt;&lt; endl
          &lt;&lt; endl;
  }
  
  
  void playRound(Unit*player, Unit* enemy)
  {
      while (player-&gt;getHp() &gt; 0 &amp;&amp; enemy-&gt;getHp() &gt; 0) {
          displayStats(player);
          displayStats(enemy);
          system("pause&gt;nul");
  
          combat(player, enemy);
          system("cls");
      }
  
  }
  
  void combat(Unit* player, Unit* enemy)
  {
      if (player-&gt;getFirstToAttack()) {
          player-&gt;attack(enemy);
          if (player-&gt;getHit() == ATTACK_MISS) cout &lt;&lt; player-&gt;getName() &lt;&lt; "'s Attack Missed! " &lt;&lt; endl;
          else if (player-&gt;getHit() == ATTACK_HIT) cout &lt;&lt; player-&gt;getName() &lt;&lt; " dealt " &lt;&lt; player-&gt;getDamage() &lt;&lt; " Damage" &lt;&lt; endl;
          system("pause&gt;nul");
          if (enemy-&gt;getHp() == 0) return;
  
          enemy-&gt;attack(player);
          if (enemy-&gt;getHit() == ATTACK_MISS) cout &lt;&lt; enemy-&gt;getName() &lt;&lt; "'s Attack Missed! " &lt;&lt; endl;
          else if (enemy-&gt;getHit() == ATTACK_HIT) cout &lt;&lt; enemy-&gt;getName() &lt;&lt; " dealt " &lt;&lt; enemy-&gt;getDamage() &lt;&lt; " Damage" &lt;&lt; endl;
          system("pause&gt;nul");
          return;
      }
  
  
      else if (enemy-&gt;getFirstToAttack()) {
          enemy-&gt;attack(player);
          if (enemy-&gt;getHit() == ATTACK_MISS) cout &lt;&lt; enemy-&gt;getName() &lt;&lt; "'s Attack Missed! " &lt;&lt; endl;
          else if (enemy-&gt;getHit() == ATTACK_HIT) cout &lt;&lt; enemy-&gt;getName() &lt;&lt; " dealt " &lt;&lt; enemy-&gt;getDamage() &lt;&lt; " Damage" &lt;&lt; endl;
          system("pause&gt;nul");
          if (player-&gt;getHp() == 0) return;
  
          player-&gt;attack(enemy);
          if (player-&gt;getHit() == ATTACK_MISS) cout &lt;&lt; player-&gt;getName() &lt;&lt; "'s Attack Missed! " &lt;&lt; endl;
          else if (player-&gt;getHit() == ATTACK_HIT) cout &lt;&lt; player-&gt;getName() &lt;&lt; " dealt " &lt;&lt; player-&gt;getDamage() &lt;&lt; " Damage" &lt;&lt; endl;
          system("pause&gt;nul");
          return;
      }
  }
  </code></pre>