Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <limits.h>
  4. #include <time.h>
  5. #include <omp.h>
  6. #define T 100
  7. #define N 10
  8.  
  9. //Fonction renvoyant "aleatoirement" 0 ou 1 (mort ou vivant)
  10. int randInRange(void)
  11. {
  12.     return rand()/(double)RAND_MAX >= 0.5 ? 0 : 1;
  13. }
  14.  
  15. //Fonction qui initialise une génération de façon "aléatoire" et renvoie son tableau
  16. int** initGrid(void)
  17. {
  18.     int **tab=NULL,i,j;
  19.  
  20.     tab = (int**)calloc(N,sizeof(int*));
  21.  
  22.     for (i=0;i<N;i++)
  23.     {
  24.         tab[i] = (int*)calloc(N,sizeof(int));
  25.         for (j=0;j<N;j++)
  26.         {
  27.             tab[i][j]=randInRange();
  28.         }
  29.     }
  30.  
  31.     return tab;
  32. }
  33.  
  34. //Simple fonction d'affichage de grille
  35. void printGrid(int **tab)
  36. {
  37.     int i,j;
  38.  
  39.     for (i=0;i<N;i++)
  40.     {
  41.         for (j=0;j<N;j++)
  42.         {
  43.             if(tab[i][j]==0)printf("|*|");
  44.             else printf("|%d|",tab[i][j]);
  45.         }
  46.         printf("\n");
  47.     }
  48. }
  49.  
  50.  
  51. //Fonction qui retourne le nombres de voisins vivant de la case (i,j)
  52. int nbVoisinVivant(int i,int j,int **tab)
  53. {
  54.     int compt=0,m,n;
  55.  
  56.     for(m=-1;m<=1;m++)
  57.     {
  58.         for(n=-1;n<=1;n++)
  59.         {
  60.             compt += ((m!=0 || n!=0) && (i+m < N) && (i+m>=0) && (j+n<N) && (j+n>=0) && (tab[i+m][j+n]==1));
  61.         }
  62.     }
  63.     return compt;
  64. }
  65.  
  66. //Fonction qui va modifier la génération en fonction du nombre de voisins vivants de chaque personne
  67. void cellule(int **courant,int **suivant)
  68. {
  69.     int i,j,nv;
  70.  
  71.     for (i=0;i<N;i++)
  72.     {
  73.         for (j=0;j<N;j++)
  74.         {
  75.             nv=nbVoisinVivant(i,j,courant);
  76.             if(courant[i][j]==0 && nv==3) suivant[i][j]=1;
  77.             else if(courant[i][j]==1 && (nv>3||nv<2)) suivant[i][j]=0;
  78.         }
  79.     }
  80. }
  81.  
  82. //Fonction qui verifie si deux grilles sont identiques (=1) ou pas (=0)
  83. int isEqual(int **courant,int **suivant)
  84. {
  85.     int i,j;
  86.  
  87.     for (i=0;i<N;i++)
  88.     {
  89.         for (j=0;j<N;j++)
  90.         {
  91.             if (courant[i][j] != suivant[i][j]) return 0;
  92.         }
  93.     }
  94.     return 1;
  95. }
  96.  
  97. //Fonction qui recopie la grille courant dans une nouvelle grille suivant et qui la retourne(retourne son pointeur)
  98. int** copyGrid(int **courant)
  99. {
  100.     int i,j;
  101.     int **suivant = initGrid();
  102.  
  103.     for (i=0;i<N;i++)
  104.     {
  105.         for(j=0;j<N;j++)
  106.         {
  107.             suivant[i][j]=courant[i][j];
  108.         }
  109.     }
  110.  
  111.     return suivant;
  112. }
  113.  
  114.  
  115. int main(void)
  116. {
  117.  
  118.     int **first=NULL;
  119.     int **next=NULL;
  120.     int nbGen = 1;
  121.  
  122.     srand(time(NULL)); //on initialise le générateur de nombre pseudo aléatoire en fonction du seed du temps
  123.  
  124.     first = initGrid();
  125.     printf("Generation no%d\n",nbGen);
  126.     printGrid(first);
  127.     next = copyGrid(first);
  128.     cellule(first, next);
  129.    
  130.     while (nbGen < T && isEqual(first,next) != 1)
  131.     {
  132.         nbGen+=1;
  133.         printf("Generation no%d\n",nbGen);
  134.         printGrid(next);
  135.         first = copyGrid(next);
  136.         cellule(first, next);
  137.         printf("\n");
  138.        
  139.     }
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement