My bad. Focused on CreateDinosaur and skipped the rest.
There is more:
1 2 3 4 5 6 7 8 9 10 11 12
|
private:
int mDinosaurHealth; // #1
int mDinosaurAttackPower; // #2
std::string mDinosaurAttackName; // #3
std::string mDinosaurName; // #4
std::vector<std::pair<std::string, int>> mDinosaurAttacks; // #5
};
Dinosaur::Dinosaur( std::string dinosaurName, int dinosaurHealth )
: mDinosaurName(dinosaurName), // #4
mDinosaurHealth(dinosaurHealth) // #1
{}
|
1. The order of initialization of members is dictated by the order in the definition.
A compiler should warn about this constructor.
Fixing and adding the implicit ones:
1 2 3 4 5 6 7
|
Dinosaur::Dinosaur( std::string dinosaurName, int dinosaurHealth )
: mDinosaurHealth(dinosaurHealth),
mDinosaurAttackPower(),
mDinosaurAttackName(),
mDinosaurName(dinosaurName),
mDinosaurAttacks()
{}
|
2. What is the logic of having both {mDinosaurAttackName, mDinosaurAttackPower} and the list of mDinosaurAttacks?
* ShowAllDinosaurAttacks() shows whole list. Good.
* GetDinosaurAttackPower() returns either value from latest call to AddAttack() or the default.
* ChooseRandomAttack() does nothing?
One could have a simpler cache, for example:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
private:
size_t current {};
std::vector<std::pair<std::string, int>> mDinosaurAttacks;
};
ChooseRandomAttack() {
current = ...
}
GetDinosaurAttackPower() {
if ( mDinosaurAttacks.empty() ) return 0;
else return mDinosaurAttacks[current].second;
}
|
Edit:
There are at least three possibilities:
1. A newborn dinosaur has no attacks. It can learn some. You have to handle the null set.
2. Every dinosaur has one (same default) attack. They can learn some more.
3. Every dinosaur is born with all attacks. They could still learn more. Constructor requires a list.