Séparation des classes dans plusieurs fichiers

This commit is contained in:
Mylloon 2022-04-02 21:22:37 +02:00
parent 74145bb172
commit 895dd9e083
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
15 changed files with 164 additions and 137 deletions

15
includes/animal.hpp Normal file
View file

@ -0,0 +1,15 @@
#ifndef _ANIMAL_HPP_
#define _ANIMAL_HPP_ 1
#include "organisme.hpp"
class Animal: public Organisme {
public:
Animal(void);
virtual ~Animal(void);
// Animal carnivore ?
virtual bool carnivore(void) const noexcept = 0;
};
#endif

14
includes/herbe.hpp Normal file
View file

@ -0,0 +1,14 @@
#ifndef _HERBE_HPP_
#define _HERBE_HPP_ 1
#include "organisme.hpp"
class Herbe: public Organisme {
public:
Herbe(void);
// Représenté par "0" dans l'univers
short id(void) const noexcept;
};
#endif

17
includes/loup.hpp Normal file
View file

@ -0,0 +1,17 @@
#ifndef _LOUP_HPP_
#define _LOUP_HPP_ 1
#include "animal.hpp"
class Loup: public Animal {
public:
Loup(void);
~Loup(void);
bool carnivore(void) const noexcept;
// Représenté par "2" dans l'univers
short id(void) const noexcept;
};
#endif

17
includes/mouton.hpp Normal file
View file

@ -0,0 +1,17 @@
#ifndef _MOUTON_HPP_
#define _MOUTON_HPP_ 1
#include "animal.hpp"
class Mouton: public Animal {
public:
Mouton(void);
~Mouton(void);
bool carnivore(void) const noexcept;
// Représenté par "1" dans l'univers
short id(void) const noexcept;
};
#endif

22
includes/organisme.hpp Normal file
View file

@ -0,0 +1,22 @@
#ifndef _ORGANISME_HPP_
#define _ORGANISME_HPP_ 1
#include <map>
class Organisme {
protected:
static inline std::map<int, char> _correspondance;
public:
Organisme(void);
/* Renvoie l'ID de l'organisme
* Si `ID < 0` -> non vivant
* Si `ID > 0` -> vivant
* Si `ID == 0` -> Herbe */
virtual short id(void) const noexcept = 0;
static char correspondance(int);
};
#endif

14
includes/sel.hpp Normal file
View file

@ -0,0 +1,14 @@
#ifndef _SEL_HPP_
#define _SEL_HPP_ 1
#include "organisme.hpp"
class Sel: public Organisme {
public:
Sel(void);
// Représenté par "-1" dans l'univers
short id(void) const noexcept;
};
#endif

View file

@ -5,10 +5,9 @@
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <random> #include <random>
#include <map>
class Organisme; #include "animal.hpp"
class Animal; #include "organisme.hpp"
class Univers { class Univers {
friend class Organisme; friend class Organisme;
@ -36,68 +35,4 @@ class Univers {
bool enVie(void) const noexcept; bool enVie(void) const noexcept;
}; };
class Organisme {
protected:
static inline std::map<int, char> _correspondance;
public:
Organisme(void);
/* Renvoie l'ID de l'organisme
* Si `ID < 0` -> non vivant
* Si `ID > 0` -> vivant
* Si `ID == 0` -> Herbe */
virtual short id(void) const noexcept = 0;
static char correspondance(int);
};
class Animal: public Organisme {
public:
Animal(void);
virtual ~Animal(void);
// Animal carnivore ?
virtual bool carnivore(void) const noexcept = 0;
};
class Herbe: public Organisme {
public:
Herbe(void);
// Représenté par "0" dans l'univers
short id(void) const noexcept;
};
class Sel: public Organisme {
public:
Sel(void);
// Représenté par "-1" dans l'univers
short id(void) const noexcept;
};
class Mouton: public Animal {
public:
Mouton(void);
~Mouton(void);
bool carnivore(void) const noexcept;
// Représenté par "1" dans l'univers
short id(void) const noexcept;
};
class Loup: public Animal {
public:
Loup(void);
~Loup(void);
bool carnivore(void) const noexcept;
// Représenté par "2" dans l'univers
short id(void) const noexcept;
};
#endif #endif

5
src/animal.cpp Normal file
View file

@ -0,0 +1,5 @@
#include "../includes/animal.hpp"
Animal::Animal(void): Organisme() { }
Animal::~Animal(void) { }

9
src/herbe.cpp Normal file
View file

@ -0,0 +1,9 @@
#include "../includes/herbe.hpp"
Herbe::Herbe(void): Organisme() {
_correspondance[id()] = ' ';
}
short Herbe::id(void) const noexcept {
return 0;
}

15
src/loup.cpp Normal file
View file

@ -0,0 +1,15 @@
#include "../includes/loup.hpp"
Loup::Loup(void): Animal() {
_correspondance[id()] = 'L';
}
Loup::~Loup(void) { }
bool Loup::carnivore(void) const noexcept {
return true;
}
short Loup::id(void) const noexcept {
return 2;
}

View file

@ -1,4 +1,6 @@
#include "../includes/univers.hpp" #include "../includes/univers.hpp"
#include "../includes/mouton.hpp"
#include "../includes/loup.hpp"
void lancerSimulation(Univers ** univers, int m, int n, int nb_moutons, int nb_loups) { void lancerSimulation(Univers ** univers, int m, int n, int nb_moutons, int nb_loups) {
*univers = new Univers(m, n); *univers = new Univers(m, n);

15
src/mouton.cpp Normal file
View file

@ -0,0 +1,15 @@
#include "../includes/mouton.hpp"
Mouton::Mouton(void): Animal() {
_correspondance[id()] = 'M';
}
Mouton::~Mouton(void) { }
bool Mouton::carnivore(void) const noexcept {
return false;
}
short Mouton::id(void) const noexcept {
return 1;
}

7
src/organisme.cpp Normal file
View file

@ -0,0 +1,7 @@
#include "../includes/organisme.hpp"
Organisme::Organisme(void) { }
char Organisme::correspondance(int id) {
return _correspondance[id];
}

9
src/sel.cpp Normal file
View file

@ -0,0 +1,9 @@
#include "../includes/sel.hpp"
Sel::Sel(void): Organisme() {
_correspondance[id()] = 'S';
}
short Sel::id(void) const noexcept {
return -1;
}

View file

@ -1,4 +1,5 @@
#include "../includes/univers.hpp" #include "../includes/univers.hpp"
#include "../includes/herbe.hpp"
// -------- Univers -------- // -------- Univers --------
@ -96,73 +97,3 @@ void Univers::affichage(void) const noexcept {
bool Univers::enVie(void) const noexcept { bool Univers::enVie(void) const noexcept {
return _nbAnimaux > 0; return _nbAnimaux > 0;
} }
// -------- Organisme --------
Organisme::Organisme(void) { }
char Organisme::correspondance(int id) {
return _correspondance[id];
}
// -------- Animal --------
Animal::Animal(void): Organisme() { }
Animal::~Animal(void) { }
// -------- Herbe --------
Herbe::Herbe(void): Organisme() {
_correspondance[id()] = ' ';
}
short Herbe::id(void) const noexcept {
return 0;
}
// -------- Sel --------
Sel::Sel(void): Organisme() {
_correspondance[id()] = 'S';
}
short Sel::id(void) const noexcept {
return -1;
}
// -------- Mouton --------
Mouton::Mouton(void): Animal() {
_correspondance[id()] = 'M';
}
Mouton::~Mouton(void) { }
bool Mouton::carnivore(void) const noexcept {
return false;
}
short Mouton::id(void) const noexcept {
return 1;
}
// -------- Loup --------
Loup::Loup(void): Animal() {
_correspondance[id()] = 'L';
}
Loup::~Loup(void) { }
bool Loup::carnivore(void) const noexcept {
return true;
}
short Loup::id(void) const noexcept {
return 2;
}