summaryrefslogtreecommitdiff
path: root/sem_3/Programm/jeu_de_la_vie/arch
diff options
context:
space:
mode:
Diffstat (limited to 'sem_3/Programm/jeu_de_la_vie/arch')
-rw-r--r--sem_3/Programm/jeu_de_la_vie/arch/population.cpp145
-rw-r--r--sem_3/Programm/jeu_de_la_vie/arch/population.h53
-rw-r--r--sem_3/Programm/jeu_de_la_vie/arch/population_vivante.cpp42
-rw-r--r--sem_3/Programm/jeu_de_la_vie/arch/population_vivante.h18
4 files changed, 258 insertions, 0 deletions
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 <iostream>
+#include <cstdlib>
+#include <exception>
+#include "population.h"
+using namespace std;
+
+#define CHECK_BOUND(i,j) \
+ if (i>=N || j>=N){ \
+ std::cout<<"Accessing a Cell at ("<<i<<","<<j<<") out of range..." \
+ << " Aborting"<<std::endl; \
+ std::terminate(); \
+ }
+
+
+size_t Population::nb_voisins_vivants(size_t ci, size_t cj) const {
+ size_t cpt=0;
+ size_t imin,imax,jmin,jmax;
+ imin = ci==0?ci:ci-1;
+ imax = ci==(N-1)?ci:ci+1;
+ jmin = cj==0?cj:cj-1;
+ jmax = cj==(N-1)?cj:cj+1;
+
+ for (size_t i = imin ; i <= imax ; i++) {
+ for (size_t j = jmin ; j <= jmax ; j++) {
+ if (T[i][j].getVivante()) {
+ cpt++;
+ }
+ }
+ }
+ return cpt - (T[ci][cj].getVivante() ? 1 : 0);
+}
+
+void Population::updateColors() {
+ //calcule les cellules vivantes qui vont mourir
+ for (size_t i = 0 ; i < N ; i++) {
+ for (size_t j = 0 ; j < N ; j++) {
+ size_t voisin=nb_voisins_vivants(i, j);
+ if (voisin !=3 && voisin!=2 && T[i][j].getVivante())
+ T[i][j].doitMourir();
+ }
+ }
+}
+
+
+
+Population::Population() {
+ for (size_t i = 0 ; i < N ; i++) {
+ for (size_t j = 0 ; j < N ; j++) {
+ T[i][j].setX(i);
+ T[i][j].setY(j);
+ T[i][j].setVivante(false);
+ }
+ }
+}
+
+void Population::init(size_t n) {
+ srand(time(NULL));
+ if (nb_vivants()==0){
+ size_t i,j;
+ for (size_t k = 0 ; k < n ; k++) {
+ do {
+ i=rand()% N;
+ j=rand()% N;
+ } while ((T[i][j]).getVivante());
+ T[i][j].setVivante(true);
+ }
+ updateColors();
+ }
+}
+
+
+size_t Population::nb_cellules(Cellule::Couleur c) const {
+ size_t cpt=0;
+ for (size_t i = 0 ; i < N ; i++) {
+ for (size_t j = 0 ; j < N ; j++) {
+ if (CelluleEstDeLaCouleur(T[i][j],c)) {
+ cpt++;
+ }
+ }
+ }
+ return cpt;
+}
+
+size_t Population::nb_vivants() const { return N*N-nb_morts();}
+size_t Population::nb_deces() const { return nb_cellules(Cellule::ROUGE)+nb_cellules(Cellule::JAUNE);}
+size_t Population::nb_morts() const { return nb_cellules(Cellule::NOIR);}
+size_t Population::nb_naissances() const { return nb_cellules(Cellule::BLEU);}
+
+
+Cellule Population::getCelluleCopie(size_t i, size_t j) const {
+ CHECK_BOUND(i,j);
+ return T[i][j];
+}
+
+const Cellule& Population::getCellule(size_t i, size_t j) const{
+ CHECK_BOUND(i,j);
+ return T[i][j];
+}
+
+void Population::printCell(size_t i, size_t j) const {
+ CHECK_BOUND(i,j);
+ T[i][j].print();
+}
+
+void Population::kill(size_t i, size_t j) {
+ CHECK_BOUND(i,j);
+ T[i][j].setVivante(false);
+}
+
+void Population::birth(size_t i, size_t j) {
+ CHECK_BOUND(i,j);
+ T[i][j].setVivante(true);
+}
+
+void Population::print() const {
+ for (size_t i = 0 ; i < N + 2 ; i++) {
+ cout<<"X";
+ }
+ cout<<endl;
+ for (size_t i = 0 ; i < N ; i++) {
+ cout<<"X";
+ for (size_t j = 0 ; j < N ; j++) {
+ //T[i][j].print();
+ cout<<Couleur2String(T[i][j].getCouleur());
+ }
+ cout<<"X"<<endl;
+ }
+ for (size_t i = 0 ; i < N + 2 ; i++) {
+ cout<<"X";
+ }
+ cout<<endl;
+}
+
+Population Population::next() const {
+ Population POP(*this);
+ for (size_t i = 0 ; i < N ; i++) {
+ for (size_t j = 0 ; j < N ; j++) {
+ size_t voisin=nb_voisins_vivants(i,j);
+ (POP.T[i][j]).setVivante(voisin ==3 || (voisin==2 && T[i][j].getVivante()));
+ }
+ }
+ POP.updateColors();
+ return POP;
+}
+
diff --git a/sem_3/Programm/jeu_de_la_vie/arch/population.h b/sem_3/Programm/jeu_de_la_vie/arch/population.h
new file mode 100644
index 0000000..9f5ad98
--- /dev/null
+++ b/sem_3/Programm/jeu_de_la_vie/arch/population.h
@@ -0,0 +1,53 @@
+#ifndef __POPULATION_H
+#define __POPULATION_H
+#include "cellule.h"
+
+#define N 5
+
+class Population {
+
+ private:
+ Cellule T[N][N];
+
+ // retourne le nbr de voisins vivants de la cellule à (i,j)
+ size_t nb_voisins_vivants(size_t, size_t) const;
+
+ //retourne le nbr de cellule d'une couleur donnée
+ size_t nb_cellules(Cellule::Couleur) const;
+
+ // Mise à jour des couleurs des cellules mourantes
+ void updateColors();
+
+ public:
+
+ // constructeur d'une population vide (sur une grille NxN)
+ Population();
+
+ // création de n cellules vivantes aléatoires (uniquement si population vide)
+ void init(size_t);
+
+ // accesseurs en interrogation
+ size_t nb_vivants() const;
+ size_t nb_deces() const;
+ size_t nb_morts() const;
+ size_t nb_naissances() const;
+
+ // accesseurs en lecture d'une cellule
+ Cellule getCelluleCopie(size_t i, size_t j) const;
+ const Cellule& getCellule(size_t i, size_t j) const;
+
+ // accesseurs en modification
+ void kill(size_t i, size_t j);
+ void birth(size_t i, size_t j);
+
+ // affichage d'une cellule
+ void printCell(size_t i, size_t j) const;
+ // affichage de la population
+ void print() const;
+
+ // calcul de la population suivante
+ Population next() const;
+
+};
+
+#endif
diff --git a/sem_3/Programm/jeu_de_la_vie/arch/population_vivante.cpp b/sem_3/Programm/jeu_de_la_vie/arch/population_vivante.cpp
new file mode 100644
index 0000000..93b328a
--- /dev/null
+++ b/sem_3/Programm/jeu_de_la_vie/arch/population_vivante.cpp
@@ -0,0 +1,42 @@
+#include <iostream>
+#include <cstdlib>
+#include <exception>
+#include "population_vivante.h"
+
+population_vivante::population_vivante(size_t x){
+ if ( x > N ) {
+ std::cout<<" Erreur, taille de population trop grande"<<std::endl;
+ }
+ nmax = x;
+ vivantes=0;
+}
+
+void population_vivante::birth(unsigned int x, unsigned int y) {
+ if (vivantes < nmax){
+ Cellule tmp(true, x,y);
+ pop[vivantes]= tmp;
+ vivantes++;
+}
+else{
+ std::cout<<"Erreur, toutes les cellules sont deja vivantes!"
+}
+}
+void population_vivante::death(unsigned int x, unsigned int y){
+ for (int i =0; i < vivantes; i ++){
+ if ((pop[i].getX == x) && (pop[i].getY==y)) {
+ pop[i].setVivante(false);
+ for ( int j = i; j < vivantes-1; j ++ ){
+ pop[j]= pop[j+1];
+ }
+ i = vivantes;
+ vivantes--;
+ }
+ }
+}
+const Cellule& copiecell(unsigned int x, unsigned int y){
+ for(int i=0; i< vivantes; i ++){
+ if ((pop[i].getX == x) && (pop[i].getY==y)) {
+ return pop[i];
+ }
+ }
+ }
diff --git a/sem_3/Programm/jeu_de_la_vie/arch/population_vivante.h b/sem_3/Programm/jeu_de_la_vie/arch/population_vivante.h
new file mode 100644
index 0000000..5b960b6
--- /dev/null
+++ b/sem_3/Programm/jeu_de_la_vie/arch/population_vivante.h
@@ -0,0 +1,18 @@
+#ifndef __POPULATION_VIVANTE_H
+#define __POPULATION_VIVANTE_H
+
+#include "cellule.h"
+
+#define N 5
+class population_vivante {
+private:
+ size_t nmax;
+ cellule pop[N*N];
+ size_t vivantes;
+public:
+ population_vivante(size_t);
+ void death(unsigned int x, unsigned int y);
+ void birth(unsigned int x, unsigned int y);
+ cellule& copiecell(unsigned int x, unsigned int y);
+};
+#endif