summaryrefslogtreecommitdiff
path: root/sem_3/Programm/jeu_de_la_vie/cellule.cpp
blob: 7611e7afb0cb8885097a45b108552988c1b98d4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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;
}