#include "univers.hpp" // -------- Univers -------- 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) { if(animaux.size() > indexLibres.size()) throw std::domain_error("Trop d'organismes pour l'univers."); 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 > _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; } // -------- Organisme -------- Organisme::Organisme(void) { } // -------- Animal -------- Animal::Animal(void): Organisme() { } // -------- Herbe -------- Herbe::Herbe(void): Organisme() { } short Herbe::id(void) const noexcept { return 0; } // -------- Sel -------- Sel::Sel(void): Organisme() { } short Sel::id(void) const noexcept { return -1; } // -------- Mouton -------- Mouton::Mouton(void): Animal() { } bool Mouton::carnivore(void) const noexcept { return false; } short Mouton::id(void) const noexcept { return 1; } // -------- Loup -------- Loup::Loup(void): Animal() { } bool Loup::carnivore(void) const noexcept { return false; } short Loup::id(void) const noexcept { return 2; }