From 7971c8b4f1cc852ee7aa44aac6f46df96066afc0 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 2 Apr 2022 13:08:26 +0200 Subject: [PATCH] =?UTF-8?q?Ajouts=20et=20modifications=20-=20Affichage=20d?= =?UTF-8?q?e=20l'univers=20-=20Vecteur=20qui=20liste=20les=20index=20libre?= =?UTF-8?q?s=20(rang=C3=A9s=20al=C3=A9atoirement)=20-=20ID=20maintenant=20?= =?UTF-8?q?des=20short?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- univers.cpp | 73 ++++++++++++++++++++++++++++++++++++----------------- univers.hpp | 15 ++++++----- 2 files changed, 59 insertions(+), 29 deletions(-) diff --git a/univers.cpp b/univers.cpp index b01d8af..22c1d83 100644 --- a/univers.cpp +++ b/univers.cpp @@ -2,43 +2,70 @@ // -------- Univers -------- -Univers::Univers(int m, int n): _m(m), _n(n), _tailleUnivers(m * n), _tailleMax(_tailleUnivers - _tailleUnivers / 4), _tour(0), _nbAnimaux(0) { +Univers::Univers(int m, int n): _m(m), _n(n), _tailleUnivers(m * n), _tour(0), _nbAnimaux(0) { _plateau = new int[m * n](); // initialise toutes les valeurs du tableau à 0 + + // On ajoute tous les index possibles + for(int i = 0; i < _tailleUnivers; i++) + indexLibres.push_back(i); + + // On mélange notre vecteur + std::random_device nombreAleatoire; + std::default_random_engine graine(nombreAleatoire()); + std::shuffle(indexLibres.begin(), indexLibres.end(), graine); + + // On en retire quelque un pour évité de saturé notre univers + while(indexLibres.size() > (uint64_t)_tailleUnivers - _tailleUnivers / 4) + indexLibres.pop_back(); } Univers::~Univers(void) { delete[] _plateau; + indexLibres.clear(); } void Univers::ajoutAnimaux(std::vector animaux) { - const int nbOrganismeFinales = _nbAnimaux + animaux.size(); - if(nbOrganismeFinales > _tailleMax) + if(animaux.size() > indexLibres.size()) throw std::domain_error("Trop d'organismes pour l'univers."); - /* ici crée un truc qui stocke tous les index possible - * pour éviter d'avoir a faire un random sur des index - * déjà occupé par autre chose. - * -> Quelque chose qui appartient directement à la classe ? - * -> map/filter? */ - - while(_nbAnimaux < nbOrganismeFinales) { - int i = rand() % _tailleUnivers; // case aléatoire - if(_plateau[i] == 0) { // si on est sur de l'herbe - _plateau[i] = animaux.back()->id(); // on place notre animal - animaux.pop_back(); // on le retire du vecteur - - _nbAnimaux++; - } + while(!animaux.empty()) { + _plateau[indexLibres.back()] = animaux.back()->id(); // on place notre animal + animaux.pop_back(); // on retire l'animal du vecteur + indexLibres.pop_back(); // on retire l'index du vecteur } } void Univers::ajoutOrganisme(Organisme * organisme, int index) { - if(index > _tailleMax) - throw std::range_error("Impossible de placer un organisme à l'index spécifié"); + if(index > _tailleUnivers) + throw std::range_error("Impossible de placer un organisme à l'index spécifié."); _plateau[index] = organisme->id(); // on place notre organisme } +void Univers::affichage(void) { + for(int i = 0; i < _n * 5; i++) + if(i == 0) + std::cout << "┌"; + else + std::cout << "─"; + std::cout << "┐" << std::endl; + + std::cout << "│ "; + for(int i = 0; i < _tailleUnivers; i += _n) { + for(int j = 0; j < _n; j++) + printf("%2d │ ", _plateau[i + j]); + if(i != _tailleUnivers - _n) std::cout << std::endl << "│ "; + } + + std::cout << std::endl; + for(int i = 0; i < _n * 5; i++) + if(i == 0) + std::cout << "└"; + else + std::cout << "─"; + std::cout << "┘" << std::endl; +} + bool Univers::enVie(void) const noexcept { return _nbAnimaux > 0; } @@ -64,7 +91,7 @@ Herbe::Herbe(void): Organisme() { } -int Herbe::id(void) const noexcept { +short Herbe::id(void) const noexcept { return 0; } @@ -75,7 +102,7 @@ Sel::Sel(void): Organisme() { } -int Sel::id(void) const noexcept { +short Sel::id(void) const noexcept { return -1; } @@ -90,7 +117,7 @@ bool Mouton::carnivore(void) const noexcept { return false; } -int Mouton::id(void) const noexcept { +short Mouton::id(void) const noexcept { return 1; } @@ -105,6 +132,6 @@ bool Loup::carnivore(void) const noexcept { return false; } -int Loup::id(void) const noexcept { +short Loup::id(void) const noexcept { return 2; } diff --git a/univers.hpp b/univers.hpp index 864db9b..d5f4cdd 100644 --- a/univers.hpp +++ b/univers.hpp @@ -3,6 +3,8 @@ #include #include +#include +#include class Organisme; class Animal; @@ -10,9 +12,10 @@ class Animal; class Univers { friend class Organisme; - const int _m, _n, _tailleUnivers, _tailleMax; + const int _m, _n, _tailleUnivers; int _tour, _nbAnimaux; int * _plateau; + std::vector indexLibres; public: Univers(int, int); @@ -40,7 +43,7 @@ class Organisme { * Si `ID < 0` -> non vivant * Si `ID > 0` -> vivant * Si `ID == 0` -> Herbe */ - virtual int id(void) const noexcept = 0; + virtual short id(void) const noexcept = 0; }; class Animal: public Organisme { @@ -56,7 +59,7 @@ class Herbe: public Organisme { Herbe(void); // Représenté par "0" dans l'univers - int id(void) const noexcept; + short id(void) const noexcept; }; class Sel: public Organisme { @@ -64,7 +67,7 @@ class Sel: public Organisme { Sel(void); // Représenté par "-1" dans l'univers - int id(void) const noexcept; + short id(void) const noexcept; }; class Mouton: public Animal { @@ -74,7 +77,7 @@ class Mouton: public Animal { bool carnivore(void) const noexcept; // Représenté par "1" dans l'univers - int id(void) const noexcept; + short id(void) const noexcept; }; @@ -85,7 +88,7 @@ class Loup: public Animal { bool carnivore(void) const noexcept; // Représenté par "2" dans l'univers - int id(void) const noexcept; + short id(void) const noexcept; }; #endif