From 04921a7a380cbaa8e9deabd3467c14a1873b1788 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 2 Apr 2022 18:12:19 +0200 Subject: [PATCH] =?UTF-8?q?Ajouts=20et=20modifications=20-=20Initialise=20?= =?UTF-8?q?le=20plateau=20avec=20l'ID=20de=20l'herbe=20-=20Affiche=20le=20?= =?UTF-8?q?caract=C3=A8re=20correspondant=20=C3=A0=20l'ID=20au=20lieu=20de?= =?UTF-8?q?=20l'ID=20et=20supprime=20les=20bordures=20int=C3=A9rieur=20-?= =?UTF-8?q?=20map=20des=20ID<->Lettre=20stock=C3=A9=20dans=20la=20classe?= =?UTF-8?q?=20Organisme,=20lettre=20ajout=C3=A9s=20lors=20de=20la=20constr?= =?UTF-8?q?uction=20de=20la=20classe=20correspondante?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- univers.cpp | 48 +++++++++++++++++++++++++----------------------- univers.hpp | 6 ++++++ 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/univers.cpp b/univers.cpp index 78dc079..b971ec4 100644 --- a/univers.cpp +++ b/univers.cpp @@ -3,7 +3,11 @@ // -------- 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 + _plateau = new int[_tailleUnivers]; + + // Remplie toutes les valeurs du tableau avec l'ID de l'herbe + Herbe herbe; + std::fill_n(_plateau, _tailleUnivers, herbe.id()); // On ajoute tous les index possibles for(int i = 0; i < _tailleUnivers; i++) @@ -61,7 +65,7 @@ void Univers::modificationOrganisme(Organisme * organisme, int index) { } void Univers::affichage(void) const noexcept { - for(int i = 0; i < _n * 5; i++) + for(int i = 0; i < _n * 4; i++) if(i == 0) std::cout << "┌"; else @@ -70,13 +74,18 @@ void Univers::affichage(void) const noexcept { std::cout << "│ "; for(int i = 0; i < _tailleUnivers; i += _n) { - for(int j = 0; j < _n; j++) - printf("%2d │ ", _plateau[i + j]); + for(int j = 0; j < _n; j++) { + std::cout << Organisme::correspondance(_plateau[i + j]); + if(j == _n - 1) + std::cout << " │ "; + else + std::cout << " "; + } if(i != _tailleUnivers - _n) std::cout << std::endl << "│ "; } std::cout << std::endl; - for(int i = 0; i < _n * 5; i++) + for(int i = 0; i < _n * 4; i++) if(i == 0) std::cout << "└"; else @@ -91,25 +100,22 @@ bool Univers::enVie(void) const noexcept { // -------- Organisme -------- -Organisme::Organisme(void) { +Organisme::Organisme(void) { } +char Organisme::correspondance(int id) { + return _correspondance[id]; } - // -------- Animal -------- -Animal::Animal(void): Organisme() { +Animal::Animal(void): Organisme() { } -} - -Animal::~Animal(void) { - -} +Animal::~Animal(void) { } // -------- Herbe -------- Herbe::Herbe(void): Organisme() { - + _correspondance[id()] = ' '; } short Herbe::id(void) const noexcept { @@ -120,7 +126,7 @@ short Herbe::id(void) const noexcept { // -------- Sel -------- Sel::Sel(void): Organisme() { - + _correspondance[id()] = 'S'; } short Sel::id(void) const noexcept { @@ -131,12 +137,10 @@ short Sel::id(void) const noexcept { // -------- Mouton -------- Mouton::Mouton(void): Animal() { - + _correspondance[id()] = 'M'; } -Mouton::~Mouton(void) { - -} +Mouton::~Mouton(void) { } bool Mouton::carnivore(void) const noexcept { return false; @@ -150,12 +154,10 @@ short Mouton::id(void) const noexcept { // -------- Loup -------- Loup::Loup(void): Animal() { - + _correspondance[id()] = 'L'; } -Loup::~Loup(void) { - -} +Loup::~Loup(void) { } bool Loup::carnivore(void) const noexcept { return false; diff --git a/univers.hpp b/univers.hpp index 2c9147f..a0bb0b7 100644 --- a/univers.hpp +++ b/univers.hpp @@ -5,6 +5,7 @@ #include #include #include +#include class Organisme; class Animal; @@ -36,6 +37,9 @@ class Univers { }; class Organisme { + protected: + static inline std::map _correspondance; + public: Organisme(void); @@ -44,6 +48,8 @@ class Organisme { * Si `ID > 0` -> vivant * Si `ID == 0` -> Herbe */ virtual short id(void) const noexcept = 0; + + static char correspondance(int); }; class Animal: public Organisme {