// platypus.cpp // #include "platypus.h" #include #include #include // Ricky Platypus::Platypus() { // Initialise the platypus with default state weight = 0.; age = 0; alive = false; mutant = false; name = " "; gender = " "; srand(static_cast(time(nullptr))); } // Ricky void Platypus::print() { // Print out the platypus' state std::cout << "THE PLATYPUS.......\n"; std::cout << "The platypus weight is: " << this->weight << "." << std::endl << "The age of the platypus is: " << this->age << "." << std::endl << "The name of the platypus is: " << this->name << "." << std::endl << "The gender of the platypus is: " << this->gender << "." << std::endl << "Is the Platypus alive? " << (this->alive ? "yes" : "no") << "." << std::endl << "Is the Platypus mutant? " << (this->mutant ? "yes" : "no") << "." << std::endl; } // Zoltan void Platypus::ageMe() { // Only increment a platypus' age if it is alive if (this->alive) { // Increment the platypus' age this->age += 1; // Possibly mutate the platypus if (rand() % 101 < 2) { this->mutant = true; } // Possibly kill the platypus if (rand() % 101 < 10 * this->weight) { this->alive = false; } } } // Ricky void Platypus::fight(Platypus &p1) { // Only let two plats fight if they are both alive if (this->alive && p1.alive) { float fightRatio; int randomVal; // Calculate the winning platypus fightRatio = ((weight / p1.getWeight()) * 50); randomVal = rand() % 100 + 1; // Adjust a platypus per the fight results if (static_cast(randomVal) < fightRatio) { alive = false; } else { p1.alive = false; } } } // Zoltan void Platypus::eat() { // Only increment the platypus' weight if the platypus is alive if (this->alive) { this->weight += static_cast(.1 + (rand() / static_cast(RAND_MAX)) * (5. - .1)) / this->weight; } } // Zoltan Platypus Platypus::hatch() const { // Only birth a new platypus if the birthing platypus is alive if (this->alive) { int newGender = rand() & 1; std::string newName; // If the platypus is male, pick a random male name; otherwise, pick a // female name. std::ifstream namesFile(newGender ? "mnames.txt" : "fnames.txt"); std::vector names; while (std::getline(namesFile, newName)) { names.emplace_back(newName); } // Return a new platypus with a random state return {static_cast(.1 + (rand() / static_cast(RAND_MAX)) * (1. - .1)), 0, names[static_cast(rand()) % names.size()], newGender ? "m" : "f"}; } // Since the birthing platypus is dead, return an uninitialized (dead) // platypus. return {}; }