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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <curses.h>
#include <string.h>
#define x 15
#define y 40
/* FONCTION MENU */
void Menu(){
printf("\n\n CONWAY'S GAME OF LIFE");
printf("\n\n\n\n\t Choisissez votre mode :");
printf("\n\n\n\n 1 : Lancer le jeu de la vie.\n\n");
printf(" 2 : Engendrer une population aléatoire.\n\n");
printf(" 3 : Population par défaut.\n\n");
printf(" 4 : Charger une population.\n\n");
printf(" 5 : Sauver une population courante.\n\n");
printf(" 6 : Aide.\n\n");
printf(" 7 : Quitter.");
switch(getch()){
case '1':
clear();
void EvolutionPop(char t[x][y]);
break;
case '2':
clear();
int GenererAleatoire();
break;
case '3':
clear();
void GenererDefaut();
break;
case '4':
clear();
void ChargerPop();
break;
case '5':
clear();
int SauverPop(char t[x][y]);
break;
case '6':
clear();
int Aide();
break;
case '7':
clear();
void Quitter();
break;
}
}
/* FONCTION QUITTER */
void Quitter(){
exit(0);
}
/* FONCTION DE CHARGEMENT DE POPULATION */
void ChargerPop(){
FILE* fichier;
char s[10000];
fichier=fopen("config.txt","rt");
if (fichier==NULL){
printf ("Erreur, fichier inexistant.");
}
else{
fgets (s,10000,fichier);
while (!feof(fichier)){
printf ("%s",s);
fgets (s,10000,fichier);
}
usleep(4000000);
}
fclose(fichier);
}
/* FONCTION DE SAUVEGARDE DE POPULATION */
int SauverPop(char t[][y]){
FILE* fichier;
char nomFichier[40];
int i, j, colsMax, linesMax;
colsMax=15;
linesMax=40;
int n=20;
printf ("\nDonnez le nom du fichier:");
char* fgets(char *ptr, int n, FILE *fichier);
fichier=fopen(nomFichier,"wt");
if (fichier==NULL){
printf ("Erreur, fichier inexistant.");
}
else{
for (i=0;i<linesMax;i++){
for (j=0;j<colsMax ;j++){
fprintf(fichier,"%d", t[i][j]);
fprintf(fichier,"\n");
}
}
fclose (fichier);
}
}
/* FONCTION DE GENERATION DE LA POPULATION PAR DEFAUT */
void GenererDefaut (char t[][y]){
int i, j, colsMax, linesMax;
char croix[]="+";
colsMax=15;
linesMax=40;
j=linesMax/2;
i=(colsMax - strlen(croix))/2;
/* Placement des "+" pour la configuration par défaut */
move(j,i);
printw(croix);
move(j,i-1);
printw(croix);
move(j,i+1);
printw(croix);
move(j+1,i-1);
printw(croix);
move(j+1,i+1);
printw(croix);
move(j+2,i-1);
printw(croix);
move(j+2,i+1);
printw(croix);
}
/* FONCTION AIDE */
int Aide(){
printw(" Bienvenue dans le menu Aide du jeu de la vie de Conway.\n\n");
printw(" Le jeu de la vie est un automate cellulaire imagine par John Horton Conway en 1970.\n\n");
printw(" Le principe est simple. Sur une grille théoriquement infinie, mais de dimension 15 par 40 dans ce programme, les cases qui representent des cellules, sont soit vivantes soit mortes. A chaque etape, l evolution d une cellule est calculee en fonction de l etat de ses huit voisines de la facon suivante: \n\n");
printw(" - Une cellule morte avec exactement 3 voisins vivants devient vivante (naissance liee a un environnement optimal.\n");
printw(" - Une celle vivante avec 2 ou 3 voisins vivants reste vivante (elle n est ni isolee, ni etoufee), sinon elle meurt (mort par desertification ou surpopulation). \n\n");
printw(" C est l analogie entre ces regles et certains criteres d evolution de populations de bacteries qui a conduit a donner cet automate le nom de jeu de la vie.");
}
/* FONCTION DU CALCUL DE VOISIN */
int calculVoisins(int sommeVoisins, char t[][y]){
int i,j;
/* Si la somme des voisins est égale à 3 et que la cellule est morte. */
if (sommeVoisins == 3 && t[i][j] == ' ' ){
Affichage(t[i][j]);
}
/* Si la somme des voisins est égale à 2 ou à 3, et que la cellule est vivante. */
if (sommeVoisins == 2 || sommeVoisins == 3 && t[i][j] == '+'){
Affichage(t[i][j]);
}
/* Si la somme des voisins est diiférente de 2 et de 3, et que la cellule est vivante. */
if (sommeVoisins != 2 || sommeVoisins != 3 && t[i][j] == '+'){
Affichage(t[i][j]);
}
}
/* FONCTION DE L EVOLUTION DE LA POPULATION */
void EvolutionPop(char t[][y]){
int i, j, sommeVoisins;
for(i=0; i<15; i++){
for(j=0; j<40; j++){
if(i==0 && j==0){
sommeVoisins=t[i][j+1] + t[i+1][j] + t[i+1][j+1];
int calculVoisins(int sommeVoisins, char t[i][j]);
}
if(i==15 && j==40){
sommeVoisins=t[i][j-1] +t[i-1][j] + t[i-1][j-1];
int calculVoisins(int sommeVoisins, char t[i][j]);
}
if(i==15 && j==0){
sommeVoisins=t[i-1][j] + t[i][j+1] + t[i-1][j+1];
int calculVoisins(int sommeVoisins, char t[i][j]);
}
if(i==0 && j==40){
sommeVoisins=t[i][j-1] + t[i+1][j] + t[i+1][j-1];
int calculVoisins(int sommeVoisins, char t[i][j]);
}
if(i==15 && j!=0 && j!=40){
sommeVoisins=t[i][j-1] + t[i][j+1] + t[i-1][j] + t[i-1][j-1] + t[i-1][j+1];
int calculVoisins(int sommeVoisins, char t[i][j]);
}
if(i!=0 && i!=15 && j==40){
sommeVoisins=t[i][j-1] + t[i-1][j] + t[i-1][j-1] + t[i+1][j] + t[i+1][j-1];
int calculVoisins(int sommeVoisins, char t[i][j]);
}
if(i==0 && j!=0 && j!=40){
sommeVoisins=t[i][j-1] + t[i+1][j-1] + t[i+1][j] + t[i+1][j+1] + t[i][j+1];
int calculVoisins(int sommeVoisins, char t[i][j]);
}
if(i!=0 && i!=15 && j==0){
sommeVoisins=t[i-1][j] + t[i-1][j+1] + t[i][j+1] + t[i+1][j+1] + t[i+1][j];
int calculVoisins(int sommeVoisins, char t[i][j]);
}
if(i!=0 && i!= 15 && j!=0 && j==40){
sommeVoisins=t[i-1][j] + t[i-1][j+1] + t[i][j+1] + t[i+1][j+1] + t[i+1][j] + t[i+1][j-1] + t[i][j-1] + t[i-1][j-1];
int calculVoisins(int sommeVoisins, char t[i][j]);
}
}
}
}
/* FONCTION DE GENERATION ALEATOIRE DE LA POPULATION */
int PopAleatoire(char t[][y]){
int i, j;
char croix[]="+";
int pop_aleatoire = 0;
srand(time(NULL));
for(i=0;i<15;i++){
for(j=0;j<40;j++){
pop_aleatoire = rand();
t[i][j] = pop_aleatoire;
if(t[i][j]%2==0){ // si les points de coordonnées correspondent à un chiffre pair
printw(croix);
}
else{ // si les points de coordonnées correspondent à un chiffre impair
printw(" ");
} // le fait de mettre un random avec laffichage en chiffre pair ou impair permet de donner une probabilité de 50% (1/2) etant donné que lintervalle est infini
}
}
}
/* FONCTION AFFICHAGE DE CARACTERES */
int Affichage(char t[][y]){
int i,j;
char croix[]="+";
for(i=0;i<15;i++){
for(j=0;j<40;j++){
if (t[i][j]==' '){
printw(croix);
}
else{
printw(" ");
}
}
}
return;
}
/* FONCTION MAIN */
int main (void){
char t[x][y];
initscr();
void Menu();
refresh();
endwin();
return(0);
} |