Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <limits.h>
- #include <time.h>
- #include <omp.h>
- #define T 100
- #define N 10
- //Fonction renvoyant "aleatoirement" 0 ou 1 (mort ou vivant)
- int randInRange(void)
- {
- return rand()/(double)RAND_MAX >= 0.5 ? 0 : 1;
- }
- //Fonction qui initialise une génération de façon "aléatoire" et renvoie son tableau
- int** initGrid(void)
- {
- int **tab=NULL,i,j;
- tab = (int**)calloc(N,sizeof(int*));
- for (i=0;i<N;i++)
- {
- tab[i] = (int*)calloc(N,sizeof(int));
- for (j=0;j<N;j++)
- {
- tab[i][j]=randInRange();
- }
- }
- return tab;
- }
- //Simple fonction d'affichage de grille
- void printGrid(int **tab)
- {
- int i,j;
- for (i=0;i<N;i++)
- {
- for (j=0;j<N;j++)
- {
- if(tab[i][j]==0)printf("|*|");
- else printf("|%d|",tab[i][j]);
- }
- printf("\n");
- }
- }
- //Fonction qui retourne le nombres de voisins vivant de la case (i,j)
- int nbVoisinVivant(int i,int j,int **tab)
- {
- int compt=0,m,n;
- for(m=-1;m<=1;m++)
- {
- for(n=-1;n<=1;n++)
- {
- compt += ((m!=0 || n!=0) && (i+m < N) && (i+m>=0) && (j+n<N) && (j+n>=0) && (tab[i+m][j+n]==1));
- }
- }
- return compt;
- }
- //Fonction qui va modifier la génération en fonction du nombre de voisins vivants de chaque personne
- void cellule(int **courant,int **suivant)
- {
- int i,j,nv;
- for (i=0;i<N;i++)
- {
- for (j=0;j<N;j++)
- {
- nv=nbVoisinVivant(i,j,courant);
- if(courant[i][j]==0 && nv==3) suivant[i][j]=1;
- else if(courant[i][j]==1 && (nv>3||nv<2)) suivant[i][j]=0;
- }
- }
- }
- //Fonction qui verifie si deux grilles sont identiques (=1) ou pas (=0)
- int isEqual(int **courant,int **suivant)
- {
- int i,j;
- for (i=0;i<N;i++)
- {
- for (j=0;j<N;j++)
- {
- if (courant[i][j] != suivant[i][j]) return 0;
- }
- }
- return 1;
- }
- //Fonction qui recopie la grille courant dans une nouvelle grille suivant et qui la retourne(retourne son pointeur)
- int** copyGrid(int **courant)
- {
- int i,j;
- int **suivant = initGrid();
- for (i=0;i<N;i++)
- {
- for(j=0;j<N;j++)
- {
- suivant[i][j]=courant[i][j];
- }
- }
- return suivant;
- }
- int main(void)
- {
- int **first=NULL;
- int **next=NULL;
- int nbGen = 1;
- srand(time(NULL)); //on initialise le générateur de nombre pseudo aléatoire en fonction du seed du temps
- first = initGrid();
- printf("Generation no%d\n",nbGen);
- printGrid(first);
- next = copyGrid(first);
- cellule(first, next);
- while (nbGen < T && isEqual(first,next) != 1)
- {
- nbGen+=1;
- printf("Generation no%d\n",nbGen);
- printGrid(next);
- first = copyGrid(next);
- cellule(first, next);
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement