summaryrefslogtreecommitdiff
path: root/sem_3/Programm/jeu_de_la_vie/cellule.cpp
diff options
context:
space:
mode:
authorGaspard Coulet <gaspard.coulet@mines-ales.org>2021-04-28 23:05:53 +0200
committerGaspard Coulet <gaspard.coulet@mines-ales.org>2021-04-28 23:05:53 +0200
commit9fe033ea88c2f705ec18c232873d056e0c229d72 (patch)
tree0647dc8c51610c7336c88c04de2068ea14b21e17 /sem_3/Programm/jeu_de_la_vie/cellule.cpp
Initial commit
Diffstat (limited to 'sem_3/Programm/jeu_de_la_vie/cellule.cpp')
-rw-r--r--sem_3/Programm/jeu_de_la_vie/cellule.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/sem_3/Programm/jeu_de_la_vie/cellule.cpp b/sem_3/Programm/jeu_de_la_vie/cellule.cpp
new file mode 100644
index 0000000..7611e7a
--- /dev/null
+++ b/sem_3/Programm/jeu_de_la_vie/cellule.cpp
@@ -0,0 +1,95 @@
+#include "cellule.h"
+#include <iostream>
+#include <string>
+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<<"("<<x<<","<<y<<") - > "<<Couleur2String(couleur)<<std::endl;
+}
+
+void Cellule::setVivante(bool etat) {
+ if (etat) {
+ couleur = age++ ? VERT : BLEU;
+ } else {
+ age = 0;
+ couleur = NOIR;
+ }
+}
+
+void Cellule::doitMourir() {
+ if (age) { // La cellule est vivante et va mourir
+ couleur = (couleur == BLEU ? JAUNE : ROUGE);
+ }
+}
+
+bool CelluleEstDeLaCouleur(const Cellule &cellule, Cellule::Couleur couleur) {
+ return (cellule.getCouleur() == couleur);
+}
+
+string Couleur2String(Cellule::Couleur c) {
+ string res;
+ switch (c) {
+ case Cellule::NOIR:
+ res = "\033[1;30mX\033[0m"; //"noire";
+ break;
+ case Cellule::BLEU:
+ res = "\033[1;34mX\033[0m";//"bleue";
+ break;
+ case Cellule::VERT:
+ res = "\033[1;32mX\033[0m";//"verte";
+ break;
+ case Cellule::ROUGE:
+ res = "\033[1;31mX\033[0m";//"rouge";
+ break;
+ case Cellule::JAUNE:
+ res = "\033[1;33mX\033[0m";//"jaune";
+ break;
+ default:
+ res = "non définie.";
+ cerr << "Erreur:" << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__
+ << ":Couleur non définie. Les couleurs possibles sont:" << endl;
+ for (Cellule::Couleur i = Cellule::NOIR;
+ i != Cellule::NB_COULEURS;
+ i = (Cellule::Couleur) (((int) i)+1)) {
+ cerr << "- " << Couleur2String(i) << endl;
+ }
+ }
+ return res;
+}