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
|
#include <map>
#include <iostream>
#include <exception>
struct Stats {
int attack;
int defense;
// etc.
};
// add two Stats together
Stats operator+ (const Stats& lhs, const Stats& rhs) {
Stats s;
s.attack = lhs.attack + rhs.attack;
s.defense = lhs.defense + rhs.defense;
// for all other stats...
return s;
}
class Player {
public:
Player() : level(1) {}
int getLevel() const { return _level; }
void setLevel(int level) {
_level = level;
calcStats();
}
void setStats(const Stats& stats) {
_baseStats = stats;
calcStats();
}
// other things, like attacking, choosing moves, etc etc etc.
private:
int _level;
Stats _baseStats; // to calculate your stats from
Stats _currStats; // your actual stats
void calcStats() {
// calculate stats based off _baseStats and _level
}
};
int main() {
// you can give these actual names for races and classes if you like
const std::map<std::string, Stats> races {
{ "race1", { 10, 8, /* ... */ }},
{ "race2", { 13, 5, /* ... */ }},
// ...
};
const std::map<std::string, Stats> classes {
{ "class1", { 3, -2, /* ... */ }},
{ "class2", { -4, 5, /* ... */ }},
// ...
};
// so on for each sub class you might have
std::string race, class;
int level;
std::cout << "Enter a race: ";
std::cin >> race;
std::cout << "Enter a class: ";
std::cin >> class;
std::cout << "Enter your level: ";
std::cin >> level;
Player p;
// using a try-catch block, alternately you could check to see if
// it is a valid class before you set the stats of your player
try {
p.setLevel(level);
p.setStats(races.at(race) + classes.at(class));
}
catch (const std::exception&) {
std::cout << "That wasn't a valid race/class!\n";
// you could loop here, I'm just quitting:
return 1;
}
// use your player
return 0;
}
|