Skip to content
Snippets Groups Projects

Adding death, collision and statistics with their tests

23 files
+ 63191
62766
Compare changes
  • Side-by-side
  • Inline

Files

+ 92
6
@@ -21,6 +21,9 @@ Animal::Animal() {
x = y = 0;
cumulX = cumulY = 0.;
probabilityOfFatalCollision = ((double) rand() / (RAND_MAX));
life = 10000 * ((double) rand() / (RAND_MAX));; // must be initialized randomly
orientation = static_cast<double>( rand() )/RAND_MAX*2.*M_PI;
speed = static_cast<double>( rand() )/RAND_MAX*MAX_SPEED;
@@ -35,6 +38,9 @@ Animal::Animal( const Animal & a ){
cout << "const Animal (" << identity << ") par copie" << endl;
probabilityOfFatalCollision = ((double) rand() / (RAND_MAX));
life = 10000 * ((double) rand() / (RAND_MAX));; // must be initialized randomly
x = a.x;
y = a.y;
cumulX = cumulY = 0.;
@@ -45,10 +51,53 @@ Animal::Animal( const Animal & a ){
Animal::~Animal( void ){
delete[] color;
cout << "dest Animal" << endl;}
if (color != NULL){
delete[] color;
}
cout << "dest Pet" << endl;
}
Animal& Animal::operator=(Animal&& p) noexcept
{
// Guard self assignment
if (this == &p)
return *this; // delete[]/size=0 would also be ok
cout << "Affectation Pet(" << p.getIdentity() << ")" << endl;
identity = p.getIdentity();
probabilityOfFatalCollision = p.getProbabilityOfFatalCollision();
life = p.getLife(); // must be initialized randomly
x = p.x;
y = p.y;
cumulX = cumulY = 0.;
orientation = p.orientation;
speed = p.speed;
color = p.color;
p.color = NULL;
return *this;
}
// copy assignment
Animal& Animal::operator=(const Animal& p) noexcept
{
cout << "Affectation par copie" << endl;
// Guard self assignment
if (this == &p)
return *this;
identity = p.getIdentity();
probabilityOfFatalCollision = p.getProbabilityOfFatalCollision();
life = p.getLife(); // must be initialized randomly
x = p.x;
y = p.y;
cumulX = cumulY = 0.;
orientation = p.orientation;
speed = p.speed;
memcpy( color, p.color, 3*sizeof(T) );
return *this;
}
void Animal::initCoords( int xLim, int yLim ){
x = rand() % xLim;
@@ -83,8 +132,11 @@ void Animal::move( int xLim, int yLim ){
void Animal::action( Environment & myEnvironment ){
move( myEnvironment.getWidth(), myEnvironment.getHeight() );}
this->decrement();
if (life > 0) {
move( myEnvironment.getWidth(), myEnvironment.getHeight() );
}
}
void Animal::draw( UImg & support ){
double xt = x + cos( orientation )*AFF_SIZE/2.1;
@@ -103,3 +155,37 @@ bool Animal::isDetecting( const Animal & a ) const{
dist = std::sqrt( (x-a.x)*(x-a.x) + (y-a.y)*(y-a.y) );
return ( dist <= LIMIT_VIEW );}
void Animal::decrement() {
// decrement the life of the animal
--life;
}
void Animal::onCollision(){
double proba = ((double) rand() / (RAND_MAX));
if (proba < this->getProbabilityOfFatalCollision()) {
cout << this->getIdentity() << " dies by collision" << endl;
life = 0;
}
}
int Animal::getLife() const {
return life;
}
int Animal::getIdentity() const {
return identity;
}
double Animal::getProbabilityOfFatalCollision() const {
return probabilityOfFatalCollision;
}
std::tuple<int, int> Animal::getCoordinates(){
return std::make_tuple(this->x,this->y);
}
// ############################## for tests ########################################
void Animal::setLife(int i){
life = i;
}
Loading