#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
class Platypus {
public :
Platypus(){
srand(time(0));
weight = 0.0;
age = 0;
name = "-";
gender = '-';
alive = false;
mutant = false;
}
Platypus(char gender, float weight, short age, string name){
srand(time(0));
this->weight = weight;
this->age = age;
this->name = name;
this->gender = gender;
alive = true;
mutant = false;
}
void print(){
string gen = (gender == 'm') ? "male" : "female";
printf("\nPlatypus %s is %s %d years old,\n",name.c_str(),gen.c_str(),age);
string life = (alive) ? "steel alive" : "already dead";
string mutantS = (mutant) ? "with mutation" : "without mutation";
printf("has %f pounds and %s %s \n",weight,life.c_str(),mutantS.c_str());
}
void age_me(){
if (alive == false)
return ;
age++;
_generateMutation();
_generateDeath();
}
void fight(Platypus * target){
if (alive == false)
return ;
float fightRatio = (weight / target->getWeight()) * 50.0f;
if (fightRatio > static_cast<float>(_grade()))
target->die();
else
die();
}
void eat(){
if (alive == false)
return ;
float weightAmount = (5.0f * _grade()) / 100.0f;
weightAmount = (weightAmount < 0.1f) ? 0.1f : weightAmount;
weight += weight * weightAmount;
}
Platypus * hatch(){printf("**\n");
if (alive == false)
return NULL;
char gender = ' ';
string name= "";
if (_grade() > 50){
gender = 'f';
name = nameF[_grade() % nameSize];
} else {
gender = 'm';
name = nameM[_grade() % nameSize];
}
float weight= static_cast<float>(_grade()) / 100.0f;
weight = (weight < 0.1f) ? 0.1f : weight;
short age= 0;
return (new Platypus(gender, weight, age, name));
}
void die(){
alive = false;
}
float getWeight() const{
return weight;
}
~Platypus(){}
private:
float weight;
short age;
string name;
char gender;
bool alive;
bool mutant;
const static int nameSize = 5;
const string nameM[nameSize]={"Bob","Jack","Clif","Rob","Bill"};
const string nameF[nameSize]={"Anna","Nanna","Ganna","Hanna","Sveta"};
void _generateDeath(){
if (_grade() <= (weight * 10))
alive = false;
}
void _generateMutation(){
if (_grade() <= 2)
mutant = true;
}
int _grade(){
return ( rand() % 100 + 1 );
}
};
void eatTest(Platypus * p){
printf("-----------EAT TEST-------------\n");
p->print();
p->eat();
printf("- - - - - - - - - - - - - - - - \n");
p->print();
printf("--------------------------------\n");
}
void ageTest(Platypus * p){
printf("------------AGE TEST------------\n");
p->print();
p->age_me();
printf("- - - - - - - - - - - - - - - - \n");
p->print();
printf("--------------------------------\n");
}
void fightTest(Platypus * p1, Platypus * p2){
printf("-----------FIGHT TEST-----------\n");
p1->print();
p2->print();
p1->fight(p2);
printf("- - - - - - - - - - - - - - - - \n");
p1->print();
p2->print();
printf("--------------------------------\n");
}
int main(int argc, char *argv[])
{
Platypus * root = new Platypus('f', 0.3f, 1, "Eva");
ageTest(root);
Platypus * childA = root->hatch();
eatTest(childA);
Platypus * childB = root->hatch();
eatTest(childB);
fightTest(childA, childB);
delete root;
delete childA;
delete childB;
return 0;
}
Comments