1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#include <iostream>
#include <string>
#include <iterator>
#include <exception>
#include <map>
enum CHRT {FstChrt = 0, Hero = FstChrt, Orc, Goblin, Rat, noChrts};
enum ATTKS {FstAttk = 0, Punch = FstAttk, Scratch, Sword, Rifle, Knife, NoAttks};
class Attack {
public:
Attack() = default;
Attack(ATTKS attk, const std::string& name, int power, int level) : mattk(attk), mName(name), mPower(power), mLevel(level) {}
std::string GetName() const {
return mName;
}
int GetPower() const {
return mPower;
}
ATTKS getId() const {
return mattk;
}
void SetName(const std::string& name) {
mName = name;
}
void IncrementLevel() {
++mLevel;
}
int GetLevel() const {
return mLevel;
}
private:
std::string mName;
int mPower { };
int mLevel { };
ATTKS mattk {};
};
using Attacks = std::map<ATTKS, Attack>;
class Character {
public:
Character() = default;
Character(CHRT chrt, const std::string& name, int health)
: mchrt(chrt), mName(name), mHealth(health) {}
Attack& GetAttackById(ATTKS attck) {
if (auto ret {mAttacks.find(attck)}; ret != mAttacks.end())
return ret->second;
throw std::runtime_error("Index error");
}
const Attack& GetAttackById(ATTKS attck) const {
if (auto ret { mAttacks.find(attck) }; ret != mAttacks.end())
return ret->second;
throw std::runtime_error("Index error");
}
Attacks& GetAttacks() {
return mAttacks;
}
const Attacks& GetAttacks() const {
return mAttacks;
}
size_t numAttacks() const {
return mAttacks.size();
}
void LearnAttack(Attack attack) {
mAttacks[attack.getId()] = std::move(attack);
}
std::string GetName() const {
return mName;
}
private:
std::string mName;
int mHealth {};
Attacks mAttacks {};
CHRT mchrt {};
};
using Characters = std::map<CHRT, Character>;
void ShowAttacks(const Characters& enemyList);
int main() {
Character player(Hero, "Hero", 100);
Characters enemyList { {Orc, {Orc, "orc", 100}},
{Goblin, {Goblin, "Goblin", 70}},
{Rat, {Rat, "Rat", 10}} };
// Default values.
const Attack punch(Punch, "Punch", 7, 1);
const Attack scratch(Scratch, "Scratch", 3, 1);
const Attack orcSword(Sword, "Orc Sword", 21, 1);
const Attack rifle(Rifle, "Rifle", 29, 1);
const Attack knife(Knife, "Knife", 11, 1);
enemyList[Orc].LearnAttack(punch);
enemyList[Orc].LearnAttack(orcSword);
enemyList[Goblin].LearnAttack(punch);
enemyList[Goblin].LearnAttack(scratch);
enemyList[Rat].LearnAttack(scratch);
player.LearnAttack(knife);
player.LearnAttack(rifle);
player.LearnAttack(punch);
player.GetAttackById(Knife).IncrementLevel();
enemyList[Rat].GetAttackById(Scratch).IncrementLevel();
ShowAttacks(enemyList);
}
void ShowAttacks(const Characters& enemyList) {
for (const auto& [cid, enemy] : enemyList) {
std::cout << "Enemy " << enemy.GetName() << ". Attack List Size: " << enemy.numAttacks() << '\n';
if (enemy.numAttacks() == 0)
std::cout << enemy.GetName() << " has no attacks!\n\n";
else
for (const auto& [attid, attk] : enemy.GetAttacks())
std::cout << attk.GetName() << " - LVL: " << attk.GetLevel() << '\n';
}
}
|