summaryrefslogtreecommitdiff
path: root/platypus/platypus.cpp
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-05-23 23:43:44 -0700
committerFuwn <[email protected]>2022-05-23 23:43:44 -0700
commitd3c2e7d9350c25c7edfaa5acf4872c8156b17779 (patch)
tree49536012e63e1c9fbda25ee54264e807d1207f50 /platypus/platypus.cpp
downloadwk7_platypusproject-d3c2e7d9350c25c7edfaa5acf4872c8156b17779.tar.xz
wk7_platypusproject-d3c2e7d9350c25c7edfaa5acf4872c8156b17779.zip
feat: initial commit
Diffstat (limited to 'platypus/platypus.cpp')
-rw-r--r--platypus/platypus.cpp113
1 files changed, 113 insertions, 0 deletions
diff --git a/platypus/platypus.cpp b/platypus/platypus.cpp
new file mode 100644
index 0000000..5dcc66a
--- /dev/null
+++ b/platypus/platypus.cpp
@@ -0,0 +1,113 @@
+// platypus.cpp
+//
+
+#include "platypus.h"
+#include <fstream>
+#include <iostream>
+#include <string>
+
+Platypus::Platypus() {
+ // Initialise the platypus with default state
+ weight = 0.;
+ age = 0;
+ alive = false;
+ mutant = false;
+ name = " ";
+ gender = " ";
+
+ srand(static_cast<unsigned int>(time(nullptr)));
+ rand();
+}
+
+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;
+}
+
+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;
+ }
+ }
+}
+
+void Platypus::fight(Platypus &p1) {
+ // Only let two platypi 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 the platypi per the fight results
+ if (static_cast<float>(randomVal) < fightRatio) {
+ alive = false;
+ } else {
+ p1.alive = false;
+ }
+ }
+}
+
+void Platypus::eat() {
+ // Only increment the platypus' weight if the platypus is alive
+ if (this->alive) {
+ this->weight +=
+ static_cast<float>(.1 + (rand() / static_cast<double>(RAND_MAX)) *
+ (5. - .1)) /
+ this->weight;
+ }
+}
+
+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;
+ std::ifstream namesFile;
+ std::vector<std::string> names;
+
+ // If the platypus is is male, pick a random male name; ...
+ if (newGender) {
+ namesFile.open("mnames.txt");
+ } else {
+ // ... otherwise, pick a female name
+ namesFile.open("fnames.txt");
+ }
+
+ while (std::getline(namesFile, newName)) {
+ names.emplace_back(newName);
+ }
+
+ // Return a new platypus with a random state
+ return {static_cast<float>(.1 + (rand() / static_cast<double>(RAND_MAX)) *
+ (1. - .1)),
+ 0, names[static_cast<unsigned long long>(rand()) % names.size()],
+ newGender ? "m" : "f"};
+ }
+
+ // Since the birthing platypus is dead, return an uninitialized (dead)
+ // platypus.
+ return {};
+}