diff options
| author | Fuwn <[email protected]> | 2022-05-23 23:43:44 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-05-23 23:43:44 -0700 |
| commit | d3c2e7d9350c25c7edfaa5acf4872c8156b17779 (patch) | |
| tree | 49536012e63e1c9fbda25ee54264e807d1207f50 | |
| download | wk7_platypusproject-d3c2e7d9350c25c7edfaa5acf4872c8156b17779.tar.xz wk7_platypusproject-d3c2e7d9350c25c7edfaa5acf4872c8156b17779.zip | |
feat: initial commit
| -rw-r--r-- | .gitignore | 15 | ||||
| -rw-r--r-- | build.ninja | 19 | ||||
| -rw-r--r-- | fnames.txt | 51 | ||||
| -rw-r--r-- | mnames.txt | 51 | ||||
| -rw-r--r-- | platypus/platypus.cpp | 113 | ||||
| -rw-r--r-- | platypus/platypus.h | 108 | ||||
| -rw-r--r-- | platypus/testPlatypus.cpp | 101 | ||||
| -rw-r--r-- | scripts/format | 3 |
8 files changed, 461 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..adf120e --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +# Visual Studio Code +.vscode + +# Artifacts +out + +# Ninja +.ninja_* + +# CLion +.idea + +# CMake +cmake-* +CMakeLists.txt diff --git a/build.ninja b/build.ninja new file mode 100644 index 0000000..87d0da8 --- /dev/null +++ b/build.ninja @@ -0,0 +1,19 @@ +cc = clang++ +cxxflags = -Ofast -std=c++20 -Weverything -Wno-c++98-compat-pedantic +out_dir = out +name = platypus +src_dir = $name +out_ext = .exe + +rule compile + command = $cc $cxxflags -c $in -o $out + +rule link + command = $cc $in -o $out$out_ext + +build $out_dir/platypus.o: compile $src_dir/platypus.cpp +build $out_dir/testPlatypus.o: compile $src_dir/testPlatypus.cpp + +build $out_dir/$name: link $out_dir/platypus.o $out_dir/testPlatypus.o + +default $out_dir/$name diff --git a/fnames.txt b/fnames.txt new file mode 100644 index 0000000..ff178d6 --- /dev/null +++ b/fnames.txt @@ -0,0 +1,51 @@ +Abby +Angel +Annie +Baby +Binky +Blue +Boo +Boots +Brandy +Callie +Candace +Caroline +Chelsea +Chloe +Cleo +Coco +Daisy +Dakota +Donna +Dooky +Dusty +Fluffy +Ginger +Katie +Lady +Libby +Lucy +Maggie +Missy +Misty +Mittens +Molly +Penny +Pepper +Precious +Prima +Princess +Pumpkin +Sadie +Samantha +Sammy +Sandy +Sasha +Sassy +Sheba +Sophie +Tasha +Tiddlywinkles +Tiger +Ursela +Xena
\ No newline at end of file diff --git a/mnames.txt b/mnames.txt new file mode 100644 index 0000000..a497db2 --- /dev/null +++ b/mnames.txt @@ -0,0 +1,51 @@ +Alex +Bailey +Bandit +Bear +Beau +Bill +Blitz +Boone +Buddy +Buster +Casey +Charlie +Cody +Duck +Duke +George +Gizmo +Harley +Hobo +Jack +Jake +Lucky +Max +Milo +Murphy +Oliver +Oreo +Oscar +Patch +Perry +Plato +Pug +Riley +Rocky +Rusty +Sam +Shadow +Shelby +Simba +Simon +Slick +Smokey +Snowball +Socks +Sparky +Spike +Sylvester +Taz +Tigger +Toby +Useless
\ No newline at end of file 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 {}; +} diff --git a/platypus/platypus.h b/platypus/platypus.h new file mode 100644 index 0000000..3e9ce2b --- /dev/null +++ b/platypus/platypus.h @@ -0,0 +1,108 @@ +// platypus.h +// + +#ifndef PLATYPUS_H +#define PLATYPUS_H +#include <random> +#include <string> +#include <utility> + +/** A platypus */ +class Platypus { +private: + /** The platypus' weight */ + float weight; + /** The platypus' age in months */ + short age; + /** The platypus' alive state */ + bool alive; + /** The platypus' mutant state */ + bool mutant; + /** The platypus' name */ + std::string name; + /** + * @brief The platypus' gender + * + * Restricted to 'm' or 'f' + * + */ + std::string gender; + +public: + /** A dead platypus */ + Platypus(); // dead platypus + /** A platypus with a custom state */ + Platypus(float _weight, short _age, std::string _name, std::string _gender) + : weight(_weight), age(_age), alive(true), mutant(false), + name(std::move(_name)), gender(std::move(_gender)) { + srand(static_cast<unsigned int>(time(nullptr))); + rand(); + } // constructor that you can pass values to so as to + // establish + // ~Platypus() {} + + // Setters + /** Mutate the platypus' weight */ + void setWeight(float _weight) { weight = _weight; } + /** Mutate the platypus' age */ + void setAge(short _age) { age = _age; } + /** Mutate the platypus' name */ + void setName(std::string _name) { name = std::move(_name); } + /** Mutate the platypus' gender */ + void setGender(std::string _gender) { gender = std::move(_gender); } + /** Mutate the platypus' alive state */ + void setAlive(bool _alive) { this->alive = _alive; } + /** Mutate the platypus' mutant state */ + void setMutant(bool _mutant) { this->mutant = _mutant; } + + // Getters + /** Get the platypus' weight */ + float getWeight() const { return weight; } + /** Get the platypus' age */ + float getAge() const { return age; } + /** Get the platypus' name */ + std::string getName() { return name; } + /** Get the platypus' gender */ + std::string getGender() { return gender; } + /** Get the platypus' alive state */ + bool getAlive() const { return this->alive; } + /** Get the platypus' mutant state */ + bool getMutant() const { return this->mutant; } + + void print(); + /** + * @brief Ages the platypus by one + * + * The platypus has a 2% chance of becoming mutant. + * + * The platypus has a (50% + its weight) chance of death. + * + */ + void ageMe(); + /** + * @brief Fight another platypus + * + * The platypus has a ((platypus / aggressor) * 50) chance of death where the + * platypus survives if a random number between one and one-hundred resolves + * to greater than the chance of death. + * + */ + void fight(Platypus &); + /** + * @brief Increases the weight of the platypus by the a random percent between + * 0.1 and 5.0 of the current weight. + * + */ + void eat(); + /** + * @brief Birth a new platypus with a random (alive, not mutant) state + * + * If the current platypus is not alive, `hatch`ing will + * return an uninitialized platypus. + * + * @return Platypus + */ + Platypus hatch() const; +}; + +#endif // PLATYPUS_H diff --git a/platypus/testPlatypus.cpp b/platypus/testPlatypus.cpp new file mode 100644 index 0000000..7085545 --- /dev/null +++ b/platypus/testPlatypus.cpp @@ -0,0 +1,101 @@ +// filename: testPlatypus.cpp +// summary: print out the paltypus + +#include "platypus.h" +#include <iostream> +using namespace std; + +int main() { + // Test `Platypus` constructors, setters, getters, `eat`ing, aging, and + // `print`ing + { + // Test constructors + Platypus p1; + Platypus p2(23.5, 5, "Perry", "m"); + + std::cout << "Platypus #1 after birth(default constructor):\n" << std::endl; + + // Test `print` + p1.print(); + + cout << endl; + + // Test setters + p1.setWeight(25.7f); + p1.setAge(4); + p1.setName("Optimus"); + p1.setGender("m"); + p1.setAlive(true); + p1.setMutant(false); + + std::cout << "Platypus #1 after changes to weight, age, name, gender, " + "alive, mutant:\n" + << std::endl; + + p1.print(); + + // Test getters + cout << "\nPlatypus #1 weight before eating: " << p1.getWeight() << endl; + + // Test `eat`ing + p1.eat(); + + cout << "Platypus #1 weight after eating: " << p1.getWeight() << endl; + + cout << "\nPlatypus #1 before aging: " << p1.getAge() + << ", old mutant status: " << p1.getMutant() + << ", old alive status: " << p1.getAlive() << endl; + + // Test aging + p1.ageMe(); + + cout << "Platypus #1 after aging: " << p1.getAge() + << ", new mutant status: " << p1.getMutant() + << ", new alive status: " << p1.getAlive() << endl + << endl; + } + + // Test `Platypus` `fight`ing and `hatch`ing + { + Platypus p1(22.8f, 4, "John", "m"); + Platypus p2(23.5, 5, "Perry", "m"); + + std::cout << "New platypus #1 before fight:\n" << std::endl; + + p1.print(); + + std::cout << "\nPlatypus #2 before fight:\n" << std::endl; + + p2.print(); + // Test `fight` + p1.fight(p2); + + std::cout << "\nNew platypus #1 after fight:\n" << std::endl; + + p1.print(); + + std::cout << "\nPlatypus #2 after fight:\n" << std::endl; + + p2.print(); + + std::cout << "\nPlatypus #3 hatched a platypus:\n" << endl; + + // Test hatch + Platypus p3(1, 2, "Becky", "f"); + Platypus hatched = p3.hatch(); + + hatched.print(); + + if (hatched.getWeight() < 1e-8f) { + std::cout << "\nThe hatched platypus looks to be dead, since the " + "platypus birthing it was dead too." + << std::endl; + } + + std::cout << "\nJust to test `getName` and `getGender` here's the name and " + "gender of the hatched platypus: " + << hatched.getName() << ", " << hatched.getGender() << std::endl; + } + + return 0; +} diff --git a/scripts/format b/scripts/format new file mode 100644 index 0000000..2e46c61 --- /dev/null +++ b/scripts/format @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +clang-format -i platypus/*.cpp platypus/*.h |