Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <string>
- #include <ctime>
- using namespace std;
- class Enemy {
- std::string mName;
- int mHealth; // U rasponu[0 −100]
- int mAttackDamage; // U rasponu[0−100]
- public:
- Enemy(string,int);
- int getAttackDamage() const { return mAttackDamage; }
- int getHealth() const { return mHealth; }
- string getName() const { return mName; }
- virtual void heal(int amount);
- virtual void takeDamage(int amount) = 0;
- };
- class ShieldedEnemy :public Enemy {
- float HitPercent;
- int ShieldState;
- public:
- ShieldedEnemy() :Enemy("default",80), HitPercent(0.2), ShieldState(100){}
- ShieldedEnemy(string,int,float, int);
- virtual void takeDamage(int amount);
- };
- class IllegalExceptionError :public runtime_error {
- ShieldedEnemy enemy;
- public:
- IllegalExceptionError(ShieldedEnemy enemy1) :runtime_error("Error with enemy: "), enemy(enemy1) {}
- int GetHealth() { return enemy.getHealth(); }
- int GetAttack() { return enemy.getAttackDamage(); }
- string GetName() { return enemy.getName(); }
- };
- Enemy::Enemy(string name,int attack):mName(name),mHealth(100),mAttackDamage(attack){}
- ShieldedEnemy::ShieldedEnemy(string name,int attack,float percent,int shield):Enemy(name,attack),HitPercent(percent),ShieldState(shield){}
- void Enemy::heal(int amount) {
- if (this->mHealth == 0) {
- return;
- }
- else {
- this->mHealth += amount;
- }
- }
- void ShieldedEnemy::takeDamage(int amount) {
- float rand_dmg = (float)rand() / RAND_MAX * 1.0;
- cout << rand_dmg << endl;
- if (rand_dmg < this->HitPercent) {
- return;
- }
- else {
- if (this->ShieldState > 0 && this->ShieldState < amount) {
- int direct_dmg = amount - this->ShieldState;
- this->ShieldState = 0;
- heal(-direct_dmg);
- }
- else if(this->ShieldState>0 && this->ShieldState >amount) {
- this->ShieldState -= amount;
- }
- else {
- heal(-amount);
- }
- }
- }
- ShieldedEnemy* createEnemy(int n) {
- ShieldedEnemy* enemies = new ShieldedEnemy[n];
- for (int i = 0; i < n; i++) {
- //srand((unsigned)time(NULL));
- float random_float = (float)rand() / RAND_MAX * 1.0;
- int random_int = rand() % (100 - 0) + 0;
- int random_attack = rand() % (100 - 0) + 0;
- try {
- enemies[i] = ShieldedEnemy("Enemy" + to_string(i + 1), random_attack, random_float, random_int);
- if (enemies[i].getAttackDamage() < 50) {
- throw IllegalExceptionError(enemies[i]);
- }
- else {
- cout << enemies[i].getName() << " " << enemies[i].getHealth() << " " << enemies[i].getAttackDamage() << endl;
- }
- }
- catch (IllegalExceptionError exception) {
- cout<<exception.what()<<" "<< exception.GetName() << " " << exception.GetHealth() << " " << exception.GetAttack() << endl;
- }
- }
- return enemies;
- }
- int main(void) {
- srand(time(NULL));
- ShieldedEnemy* enemy = createEnemy(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement