From 9fe033ea88c2f705ec18c232873d056e0c229d72 Mon Sep 17 00:00:00 2001 From: Gaspard Coulet Date: Wed, 28 Apr 2021 23:05:53 +0200 Subject: Initial commit --- sem_3/Programm/jeu_de_la_vie/JDV0-bis-main.cpp | 14 ++ sem_3/Programm/jeu_de_la_vie/JDV0-bis.cpp | 149 +++++++++++++++++ sem_3/Programm/jeu_de_la_vie/JDV0-bis.h | 38 +++++ sem_3/Programm/jeu_de_la_vie/arch/population.cpp | 145 ++++++++++++++++ sem_3/Programm/jeu_de_la_vie/arch/population.h | 53 ++++++ .../jeu_de_la_vie/arch/population_vivante.cpp | 42 +++++ .../jeu_de_la_vie/arch/population_vivante.h | 18 ++ sem_3/Programm/jeu_de_la_vie/cellule.cpp | 95 +++++++++++ sem_3/Programm/jeu_de_la_vie/cellule.h | 55 ++++++ sem_3/Programm/jeu_de_la_vie/option.cpp | 45 +++++ sem_3/Programm/jeu_de_la_vie/option.h | 47 ++++++ .../jeu_de_la_vie/population-vivante-v2.cpp | 184 +++++++++++++++++++++ .../Programm/jeu_de_la_vie/population-vivante-v2.h | 60 +++++++ sem_3/Programm/jeu_de_la_vie/tabOptions.cpp | 58 +++++++ sem_3/Programm/jeu_de_la_vie/tabOptions.h | 22 +++ 15 files changed, 1025 insertions(+) create mode 100644 sem_3/Programm/jeu_de_la_vie/JDV0-bis-main.cpp create mode 100644 sem_3/Programm/jeu_de_la_vie/JDV0-bis.cpp create mode 100644 sem_3/Programm/jeu_de_la_vie/JDV0-bis.h create mode 100644 sem_3/Programm/jeu_de_la_vie/arch/population.cpp create mode 100644 sem_3/Programm/jeu_de_la_vie/arch/population.h create mode 100644 sem_3/Programm/jeu_de_la_vie/arch/population_vivante.cpp create mode 100644 sem_3/Programm/jeu_de_la_vie/arch/population_vivante.h create mode 100644 sem_3/Programm/jeu_de_la_vie/cellule.cpp create mode 100644 sem_3/Programm/jeu_de_la_vie/cellule.h create mode 100644 sem_3/Programm/jeu_de_la_vie/option.cpp create mode 100644 sem_3/Programm/jeu_de_la_vie/option.h create mode 100644 sem_3/Programm/jeu_de_la_vie/population-vivante-v2.cpp create mode 100644 sem_3/Programm/jeu_de_la_vie/population-vivante-v2.h create mode 100644 sem_3/Programm/jeu_de_la_vie/tabOptions.cpp create mode 100644 sem_3/Programm/jeu_de_la_vie/tabOptions.h (limited to 'sem_3/Programm/jeu_de_la_vie') diff --git a/sem_3/Programm/jeu_de_la_vie/JDV0-bis-main.cpp b/sem_3/Programm/jeu_de_la_vie/JDV0-bis-main.cpp new file mode 100644 index 0000000..e1ae237 --- /dev/null +++ b/sem_3/Programm/jeu_de_la_vie/JDV0-bis-main.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; + +#include "JDV0-bis.h" + +int main(int argc, char** argv){ + + JeuDeLaVie JDV; + JDV.parseOptions(argc,argv); + JDV.run(4); + + return 0; +} + diff --git a/sem_3/Programm/jeu_de_la_vie/JDV0-bis.cpp b/sem_3/Programm/jeu_de_la_vie/JDV0-bis.cpp new file mode 100644 index 0000000..587c05a --- /dev/null +++ b/sem_3/Programm/jeu_de_la_vie/JDV0-bis.cpp @@ -0,0 +1,149 @@ +#include "JDV0-bis.h" +#include +#include +#include +#include +using namespace std; + + +void JeuDeLaVie::nettoie(std::string &s){ + size_t pos=s.find_first_of("#"); + s=s.substr(0,pos); + int beg=0,end=s.size()-1; + while(begbeg-1 && (s[end]==' ' || s[end]=='\t' )) end--; + s=s.substr(beg,end-beg+1); +} + + +bool JeuDeLaVie::findCleVal(std::string &s, std::string &s1,std::string &s2){ + nettoie(s); + if (s==string("")) return false; + size_t pos=s.find_first_of(":"); + if (pos==string::npos) { + cerr << "Le fichier est mal formé" << endl; + terminate(); + } + s1=s.substr(0,pos); + s2=s.substr(pos+1); + nettoie(s1); + nettoie(s2); + //cerr<<"Found cle/val -> "<< s1<< " and "< "< " << (++i < argc ? argv[i] : "Erreur"); + } else { + cout << "Cette option n'a pas été reconnue."; + opt_error = true; + } + cout << endl; + } + i++; + } + if (opt_error) { + cout << "Usage : " << argv[0] << " [Options]" << endl; + opts.print(); + terminate(); + } +} diff --git a/sem_3/Programm/jeu_de_la_vie/JDV0-bis.h b/sem_3/Programm/jeu_de_la_vie/JDV0-bis.h new file mode 100644 index 0000000..763111d --- /dev/null +++ b/sem_3/Programm/jeu_de_la_vie/JDV0-bis.h @@ -0,0 +1,38 @@ +#ifndef __JDV_H +#define __JDV_H + +#include "population-vivante-v2.h" +#include "tabOptions.h" + +#define HELP_ID 1 +#define VERSION_ID 2 +#define DIMENSION_ID 10 +#define PROBABILITY_ID 11 +#define CONFIG_ID 20 + + +class JeuDeLaVie { + private: + PopulationVivante POP; + TabOptions opts; + + void nettoie(std::string &s); + bool findCleVal(std::string &s, std::string &s1,std::string &s2); + void TraiteOption(const std::string &cle, const std::string &valeur, size_t num_ligne); + + public: + + JeuDeLaVie(); + + void loadConfig(std::string file); + void run(size_t); + void parseOptions(int argc, char** argv); + +}; + + + + + + +#endif diff --git a/sem_3/Programm/jeu_de_la_vie/arch/population.cpp b/sem_3/Programm/jeu_de_la_vie/arch/population.cpp new file mode 100644 index 0000000..5655e2f --- /dev/null +++ b/sem_3/Programm/jeu_de_la_vie/arch/population.cpp @@ -0,0 +1,145 @@ +#include +#include +#include +#include "population.h" +using namespace std; + +#define CHECK_BOUND(i,j) \ + if (i>=N || j>=N){ \ + std::cout<<"Accessing a Cell at ("< +#include +#include +#include "population_vivante.h" + +population_vivante::population_vivante(size_t x){ + if ( x > N ) { + std::cout<<" Erreur, taille de population trop grande"< +#include +using namespace std; + +Cellule::Cellule(): age(0), x(0), y(0), couleur(NOIR) { +} + +Cellule::Cellule(bool etat, unsigned int x, unsigned int y): + age(etat ? 1 : 0), x(x), y(y), couleur(etat ? BLEU : NOIR) { +} + +bool Cellule::getVivante() const { + return age; +} + +unsigned int Cellule::getX() const { + return x; +} + +unsigned int Cellule::getY() const { + return y; +} + +Cellule::Couleur Cellule::getCouleur() const { + return couleur; +} + +// Accesseurs en écriture +void Cellule::setX(unsigned int x) { + this->x = x; +} + +void Cellule::setY(unsigned int y) { + this->y = y; +} + +bool Cellule::estVoisine(const Cellule &c) const { + return age && + ((x - c.x) * (x - c.x) + (y - c.y) * (y - c.y) <= 2); +} + +void Cellule::print() const { + std::cout<<"("< "< + +class Cellule { + public: + + enum Couleur { + NOIR, + BLEU, + VERT, + ROUGE, + JAUNE, + NB_COULEURS + }; + + private: + size_t age; + unsigned int x, y; + Couleur couleur; + + public: + + // Constructeurs + Cellule(); // morte par défaut + Cellule(bool etat, unsigned int x, unsigned int y); + + // Accesseurs en lecture + bool getVivante() const; + unsigned int getX() const; + unsigned int getY() const; + Couleur getCouleur() const; + + // Accesseurs en écriture + void setX(unsigned int x); + void setY(unsigned int y); + void setVivante(bool etat); + + // renvoie vrai si la cellule courante est vivante et est voisine de c + bool estVoisine(const Cellule &c) const; + // affiche la cellule + void print() const; + + // spécifie qu'une cellule doit mourir au prochain tour du jeu (-> changement de couleur) + void doitMourir(); +}; + +// Renvoie vrai si la cellule est de la couleur passée en paramètre, faux sinon. +bool CelluleEstDeLaCouleur(const Cellule &cellule, Cellule::Couleur couleur); + +// Retourne la chaîne correspondant à la couleur passée en paramètre +std::string Couleur2String(Cellule::Couleur c); + +#endif diff --git a/sem_3/Programm/jeu_de_la_vie/option.cpp b/sem_3/Programm/jeu_de_la_vie/option.cpp new file mode 100644 index 0000000..65d2241 --- /dev/null +++ b/sem_3/Programm/jeu_de_la_vie/option.cpp @@ -0,0 +1,45 @@ +#include +#include "option.h" +#include + +using namespace std; + +/////////////////// +// Classe Option // +/////////////////// + +Option::Option(): id(-1), nom(), raccourci(), description(), type(AUCUN) {} + +Option::Option(int _id, const string &_nom, const string &_raccourci, + const string &_desc, Option::Type _type): + id(_id), nom(_nom), raccourci(_raccourci), description(_desc), type(_type) {} + +int Option::getId() const { return id; } +string Option::getName() const { return nom; } +string Option::getShortcut() const { return raccourci; } +string Option::getDescription() const { return description; } +Option::Type Option::getType() const { return type; } + +void Option::setId(int _id) { this->id = _id; } +void Option::setName(const string &name) { nom = name; } +void Option::setShortcut(const string &shortcut) { raccourci = shortcut; } +void Option::setDescription(const string &desc) { description = desc; } +void Option::setType(Option::Type t) { type = t; } + +void Option::print() const { + cout << nom << " (" << raccourci << ") " << Type2String(type) + << "\t" << description << endl; +} + +string Type2String(Option::Type t) { + string tmp; + switch (t) { + case Option::AUCUN: tmp = ""; break; + case Option::BOOLEEN: tmp = ""; break; + case Option::ENTIER: tmp = ""; break; + case Option::REEL: tmp = ""; break; + case Option::CHAR: tmp = ""; break; + case Option::CHAINE: tmp = ""; break; + } + return tmp; +} diff --git a/sem_3/Programm/jeu_de_la_vie/option.h b/sem_3/Programm/jeu_de_la_vie/option.h new file mode 100644 index 0000000..e420c4a --- /dev/null +++ b/sem_3/Programm/jeu_de_la_vie/option.h @@ -0,0 +1,47 @@ +#ifndef __OPTION_H +#define __OPTION_H + +#include + +class Option { + public: + enum Type { + AUCUN, + BOOLEEN, + ENTIER, + REEL, + CHAR, + CHAINE + }; + + private: + int id; + std::string nom, raccourci, description; + Type type; + + public: + Option(); + Option(int id, const std::string &nom, const std::string &raccourci, + const std::string &desc, Type type); + + // Accesseurs en lecture + int getId() const; + std::string getName() const; + std::string getShortcut() const; + std::string getDescription() const; + Type getType() const; + + // Accesseurs en écriture + void setId(int id); + void setName(const std::string &name); + void setShortcut(const std::string &shortcut); + void setDescription(const std::string &desc); + void setType(Type t); + + // Affichage + void print() const; +}; + +std::string Type2String(Option::Type t); + +#endif diff --git a/sem_3/Programm/jeu_de_la_vie/population-vivante-v2.cpp b/sem_3/Programm/jeu_de_la_vie/population-vivante-v2.cpp new file mode 100644 index 0000000..f59cb97 --- /dev/null +++ b/sem_3/Programm/jeu_de_la_vie/population-vivante-v2.cpp @@ -0,0 +1,184 @@ +#include +#include +#include +#include "population-vivante-v2.h" +using namespace std; + +#define CHECK_BOUND(i,j) \ + if (i>=N || j>=N){ \ + std::cout<<"Accessing a Cell at ("<setVivante(true); + } + } + else { + std::cerr<<"PopulationVivante: Erreur -> trop de cellule vivante pour NMAX="< +#include +#include "tabOptions.h" + +using namespace std; + +TabOptions::TabOptions(): opts(), nb_opts(0) {} + +void TabOptions::addOption(const Option &o) { + if (nb_opts == NMAX_OPTS) { + cerr << "Erreur: Impossible d'ajouter une nouvelle option." << endl + << " Nombre maximum d'option atteint"<< " (" << NMAX_OPTS << ")." + << endl; + terminate(); + } + + bool found = (getOptionIndex(o.getName()) != -1) || (getOptionIndex(o.getShortcut()) != -1); + + if (found) { + cerr << "Avertissement: L'identifiant " << o.getId() << " est déjà utilisé." + << endl; + } else { + opts[nb_opts] = o; + nb_opts++; + } +} + +void TabOptions::print() const { + cout << "Options :" << endl; + for (size_t i = 0; i < nb_opts; i++) { + opts[i].print(); + } +} + +int TabOptions::getOptionIndex(const string &opt) const { + bool found = false; + size_t i = 0; + while (!found && (i < nb_opts)) { + found = ((opts[i].getName() == opt) || (opts[i].getShortcut() == opt)); + i++; + } + return found ? i - 1 : -1; +} +// opt doit etre une option valide +int TabOptions::getOptionId(const std::string &opt) const { + int i = getOptionIndex(opt); + return (i>=0 ? opts[i].getId():-1); +} +// opt doit etre une option valide +bool TabOptions::optionHasArgument(const std::string &opt) const { + size_t i = getOptionIndex(opt); + return (opts[i].getType() != Option::AUCUN); +} +// opt doit etre une option valide +Option::Type TabOptions::optionArgumentType(const std::string &opt) const { + size_t i = getOptionIndex(opt); + return opts[i].getType(); +} diff --git a/sem_3/Programm/jeu_de_la_vie/tabOptions.h b/sem_3/Programm/jeu_de_la_vie/tabOptions.h new file mode 100644 index 0000000..cef8aaa --- /dev/null +++ b/sem_3/Programm/jeu_de_la_vie/tabOptions.h @@ -0,0 +1,22 @@ +#ifndef __TABOPTIONS_H__ +#define __TABOPTIONS_H__ + +#include +#include "option.h" + +#define NMAX_OPTS 100 +class TabOptions { + private: + Option opts[NMAX_OPTS]; + size_t nb_opts; + int getOptionIndex(const std::string &opt) const; + public: + TabOptions(); + void addOption(const Option &o); + void print() const; + int getOptionId(const std::string &opt) const; + bool optionHasArgument(const std::string &opt) const; + Option::Type optionArgumentType(const std::string &opt) const; +}; + +#endif -- cgit v1.2.3