diff --git a/includes/animal.hpp b/includes/animal.hpp index c2f2b4a..99a5d31 100644 --- a/includes/animal.hpp +++ b/includes/animal.hpp @@ -20,6 +20,10 @@ class Animal: public Organisme { template void rechercheEspece(int, std::vector &) noexcept; + // Renvoie la liste des animaux environnants + template + void animauxEnvirons(std::vector &) noexcept; + protected: const int m_age_max; // age maximale que peut atteindre l'organisme @@ -46,21 +50,23 @@ class Animal: public Organisme { // Renvoie la liste des cases accesible depuis la position de l'animal std::vector casesPossible(void) const noexcept; - // Renvoie la liste des animaux environnants template - void animauxEnvirons(std::vector &) noexcept; + void procreation(void) noexcept; const int genre; // genre, 0 = masculin, 1 = féminin + // Nombre de tour a attendre avant de pouvoir se reproduire de nouveau + const short m_attente_reproduction; + public: const int vitesse; // vitesse de l'organisme - // ID de l'univers, age max, faim max, vitesse, genre - Animal(int, int, int, int, int); - - // ID de l'univers, index dans l'univers, age max, faim max, vitesse, genre + // ID de l'univers, age max, faim max, vitesse, genre, rythme reproduction Animal(int, int, int, int, int, int); + // ID de l'univers, index dans l'univers, age max, faim max, vitesse, genre, rythme reproduction + Animal(int, int, int, int, int, int, int); + ~Animal(void); // Animal carnivore ? diff --git a/includes/animal_template.hpp b/includes/animal_template.hpp index 0aa50be..2a8297d 100644 --- a/includes/animal_template.hpp +++ b/includes/animal_template.hpp @@ -72,4 +72,49 @@ void Animal::animauxEnvirons(std::vector &animaux) noexcept { } } +template +void Animal::procreation(void) noexcept { + // Si l'animal est une femelle + if(genre == 1) { + std::random_device nombre_aleatoire; + std::default_random_engine graine(nombre_aleatoire()); + + if(m_reproduire == -1) { // si l'animal peut se reproduire + std::vector pretendants; + animauxEnvirons(pretendants); // recuperation des animaux libre aux alentours + + // S'il y a des prétendants + if(pretendants.size() > 0) { + // On choisit aléatoirement un prétendant + std::uniform_int_distribution aleatoire_pretendant(0, pretendants.size() - 1); + Espece * partenaire = pretendants[static_cast(aleatoire_pretendant(graine))]; + + m_partenaire = partenaire; + partenaire->m_partenaire = this; + + m_reproduire = 1; // accouchement dans 1 tour + } + } else { + if(m_reproduire >= 0) { // si l'animal est déjà entrain de se reproduire + --m_reproduire; + if(m_reproduire == -1) { // si reproduction terminée + static_cast(m_partenaire)->m_partenaire = nullptr; + m_partenaire = nullptr; + + std::vector cases_possible_enfant = this->casesPossible(); + std::uniform_int_distribution aleatoire_enfant(0, cases_possible_enfant.size() - 1); + + new Espece(m_univers_ID, cases_possible_enfant[static_cast(aleatoire_enfant(graine))]); + + m_reproduire -= m_attente_reproduction; // doit attendre avant de pouvoir se reproduire encore + } else { + if(m_reproduire == -1 - m_attente_reproduction) { + m_reproduire = -1; + } + } + } + } + } +} + #endif diff --git a/includes/mouton.hpp b/includes/mouton.hpp index 78c6c6a..688d88c 100644 --- a/includes/mouton.hpp +++ b/includes/mouton.hpp @@ -6,8 +6,6 @@ class Mouton: public Animal { const char _m_lettre = 'M'; // caractère représentant le mouton - const short m_attente_reproduction = 1; - // Défini la vitesse du mouton int generationVitesse(void) const noexcept; diff --git a/src/animal.cpp b/src/animal.cpp index 2033879..dd5a701 100644 --- a/src/animal.cpp +++ b/src/animal.cpp @@ -1,15 +1,19 @@ #include "../includes/animal.hpp" Animal::Animal(const int univers_ID, const int index, const int age_max, - const int faim_max, const int p_vitesse, int p_genre): Organisme(univers_ID, false, index), - m_age_max(age_max), m_faim_max(faim_max), - genre(p_genre), vitesse(p_vitesse) + const int faim_max, const int p_vitesse, int p_genre, + int rythme_reproduction): Organisme(univers_ID, false, index), + m_age_max(age_max), m_faim_max(faim_max), + genre(p_genre), m_attente_reproduction(rythme_reproduction), + vitesse(p_vitesse) { } Animal::Animal(const int univers_ID, const int age_max, - const int faim_max, const int p_vitesse, int p_genre): Organisme(univers_ID, false), - m_age_max(age_max), m_faim_max(faim_max), - genre(p_genre), vitesse(p_vitesse) + const int faim_max, const int p_vitesse, + int p_genre, int rythme_reproduction): Organisme(univers_ID, false), + m_age_max(age_max), m_faim_max(faim_max), + genre(p_genre), m_attente_reproduction(rythme_reproduction), + vitesse(p_vitesse) { } Animal::~Animal(void) { diff --git a/src/loup.cpp b/src/loup.cpp index 8d0d909..700c233 100644 --- a/src/loup.cpp +++ b/src/loup.cpp @@ -1,10 +1,10 @@ #include "../includes/loup.hpp" -Loup::Loup(const int univers_ID): Animal(univers_ID, 60, 10, Loup::generationVitesse(), Loup::choixGenre()) { +Loup::Loup(const int univers_ID): Animal(univers_ID, 60, 10, Loup::generationVitesse(), Loup::choixGenre(), 3) { m_correspondance[ID] = _m_lettre; } -Loup::Loup(const int univers_ID, const int index): Animal(univers_ID, index, 60, 10, Loup::generationVitesse(), Loup::choixGenre()) { +Loup::Loup(const int univers_ID, const int index): Animal(univers_ID, index, 60, 10, Loup::generationVitesse(), Loup::choixGenre(), 3) { m_correspondance[ID] = _m_lettre; } @@ -34,7 +34,7 @@ void Loup::action(void) { /* TODO */ // S'accouple si besoin - /* TODO */ + procreation(); if(m_age > m_age_max || m_faim > m_faim_max) { // meurt si trop vieux ou trop faim mortOrganisme(m_superposable); diff --git a/src/mouton.cpp b/src/mouton.cpp index 193183f..91ee17b 100644 --- a/src/mouton.cpp +++ b/src/mouton.cpp @@ -1,10 +1,10 @@ #include "../includes/mouton.hpp" -Mouton::Mouton(const int univers_ID): Animal(univers_ID, 50, 5, Mouton::generationVitesse(), Mouton::choixGenre()) { +Mouton::Mouton(const int univers_ID): Animal(univers_ID, 50, 5, Mouton::generationVitesse(), Mouton::choixGenre(), 1) { m_correspondance[ID] = _m_lettre; } -Mouton::Mouton(const int univers_ID, const int index): Animal(univers_ID, index, 50, 5, Mouton::generationVitesse(), Mouton::choixGenre()) { +Mouton::Mouton(const int univers_ID, const int index): Animal(univers_ID, index, 50, 5, Mouton::generationVitesse(), Mouton::choixGenre(), 1) { m_correspondance[ID] = _m_lettre; } @@ -39,44 +39,8 @@ void Mouton::action(void) { } } - // Si l'animal est une femelle - if(genre == 1) { - if(m_reproduire == -1) { // si l'animal peut se reproduire - std::vector pretendants; - animauxEnvirons(pretendants); // recuperation des animaux libre aux alentours - - // S'il y a des prétendants - if(pretendants.size() > 0) { - // On choisit aléatoirement un prétendant - std::uniform_int_distribution aleatoire_pretendant(0, pretendants.size() - 1); - Mouton * partenaire = pretendants[static_cast(aleatoire_pretendant(graine))]; - - m_partenaire = partenaire; - partenaire->m_partenaire = this; - - m_reproduire = 1; // accouchement dans 1 tour - } - } else { - if(m_reproduire >= 0) { // si l'animal est déjà entrain de se reproduire - --m_reproduire; - if(m_reproduire == -1) { // si reproduction terminée - static_cast(m_partenaire)->m_partenaire = nullptr; - m_partenaire = nullptr; - - std::vector cases_possible_enfant = this->casesPossible(); - std::uniform_int_distribution aleatoire_enfant(0, cases_possible_enfant.size() - 1); - - new Mouton(m_univers_ID, cases_possible_enfant[static_cast(aleatoire_enfant(graine))]); - - m_reproduire -= m_attente_reproduction; // doit attendre avant de pouvoir se reproduire encore - } else { - if(m_reproduire == -1 - m_attente_reproduction) { - m_reproduire = -1; - } - } - } - } - } + // S'accouple si besoin + procreation(); if(m_age > m_age_max || m_faim > m_faim_max) { // meurt si trop vieux ou trop faim mortOrganisme(m_superposable);