blob: 5dcc66ad0c36037f95a8874cf953b88ceb3ede9a (
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>
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 {};
}
|