Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7.  
  8. class Enemy {
  9.     std::string mName;
  10.     int mHealth; // U rasponu[0 −100]
  11.     int mAttackDamage; // U rasponu[0−100]
  12. public:
  13.      Enemy(string,int);
  14.      int getAttackDamage() const { return mAttackDamage; }
  15.      int getHealth() const { return mHealth; }
  16.      string getName() const { return mName; }
  17.      virtual void heal(int amount);
  18.      virtual void takeDamage(int amount) = 0;
  19. };
  20.  
  21. class ShieldedEnemy :public Enemy {
  22.     float HitPercent;
  23.     int ShieldState;
  24. public:
  25.     ShieldedEnemy() :Enemy("default",80), HitPercent(0.2), ShieldState(100){}
  26.     ShieldedEnemy(string,int,float, int);
  27.     virtual void takeDamage(int amount);
  28. };
  29.  
  30. class IllegalExceptionError :public runtime_error {
  31.     ShieldedEnemy enemy;
  32. public:
  33.     IllegalExceptionError(ShieldedEnemy enemy1) :runtime_error("Error with enemy: "), enemy(enemy1) {}
  34.     int GetHealth() { return enemy.getHealth(); }
  35.     int GetAttack() { return enemy.getAttackDamage(); }
  36.     string GetName() { return enemy.getName(); }
  37. };
  38.  
  39. Enemy::Enemy(string name,int attack):mName(name),mHealth(100),mAttackDamage(attack){}
  40.  
  41. ShieldedEnemy::ShieldedEnemy(string name,int attack,float percent,int shield):Enemy(name,attack),HitPercent(percent),ShieldState(shield){}
  42.  
  43. void Enemy::heal(int amount) {
  44.     if (this->mHealth == 0) {
  45.         return;
  46.     }
  47.     else {
  48.         this->mHealth += amount;
  49.     }
  50. }
  51.  
  52. void ShieldedEnemy::takeDamage(int amount) {
  53.     float rand_dmg = (float)rand() / RAND_MAX * 1.0;
  54.     cout << rand_dmg << endl;
  55.     if (rand_dmg < this->HitPercent) {
  56.         return;
  57.     }
  58.     else {
  59.         if (this->ShieldState > 0 && this->ShieldState < amount) {
  60.             int direct_dmg = amount - this->ShieldState;
  61.             this->ShieldState = 0;
  62.             heal(-direct_dmg);
  63.         }
  64.         else if(this->ShieldState>0 && this->ShieldState >amount) {
  65.             this->ShieldState -= amount;
  66.         }
  67.         else {
  68.             heal(-amount);
  69.         }
  70.     }
  71. }
  72.  
  73. ShieldedEnemy* createEnemy(int n) {
  74.     ShieldedEnemy* enemies = new ShieldedEnemy[n];
  75.    
  76.         for (int i = 0; i < n; i++) {
  77.             //srand((unsigned)time(NULL));
  78.             float random_float = (float)rand() / RAND_MAX * 1.0;
  79.             int random_int = rand() % (100 - 0) + 0;
  80.             int random_attack = rand() % (100 - 0) + 0;
  81.             try {
  82.                 enemies[i] = ShieldedEnemy("Enemy" + to_string(i + 1), random_attack, random_float, random_int);
  83.                 if (enemies[i].getAttackDamage() < 50) {
  84.                     throw IllegalExceptionError(enemies[i]);
  85.                 }
  86.                 else {
  87.                     cout << enemies[i].getName() << " " << enemies[i].getHealth() << " " << enemies[i].getAttackDamage() << endl;
  88.                 }
  89.             }
  90.             catch (IllegalExceptionError exception) {
  91.                 cout<<exception.what()<<" "<< exception.GetName() << " " << exception.GetHealth() << " " << exception.GetAttack() << endl;
  92.             }
  93.         }
  94.    
  95.     return enemies;
  96. }
  97.  
  98. int main(void) {
  99.     srand(time(NULL));
  100.     ShieldedEnemy* enemy = createEnemy(10);
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement