summaryrefslogtreecommitdiff
path: root/platypus/platypus.cpp
blob: b5f92707f9224256e75b6952f537c74cf52f4471 (plain) (blame)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// platypus.cpp
//

#include "platypus.h"
#include <fstream>
#include <iostream>
#include <string>

// Ricky
Platypus::Platypus() {
  // Initialise the platypus with default state
  weight = 0.;
  age = 0;
  alive = false;
  mutant = false;
  name = " ";
  gender = " ";

  // Zoltan
  srand(static_cast<unsigned int>(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<float>(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<float>(.1 + (rand() / static_cast<double>(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<std::string> names;

    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 {};
}