pietrod21

conway's game of life

Dec 21st, 2011
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #define L 50
  3. #define t_max 100
  4.  
  5.  
  6. int somma(int x,int y); //somma caselle adiacenti
  7. int c[L][L];
  8. int c1[L][L];
  9.  
  10. int main(){
  11. int x, y, n, somm, t;
  12. FILE *fp, *fd;
  13. fp=fopen("vita.dat","w");
  14. fd=fopen("vita.gnu","w");
  15. fprintf(fd, "set size square \n set view map \n plot 'vita.dat' u 1:2");
  16. fclose(fd);
  17.  
  18. for(x=0;x<L+1;x++){     //azzero array
  19. for(y=0;y<L+1;y++){
  20.     if(x%%)              
  21.     c[x][y]=0;
  22.     c1[x][y]=0;
  23. }
  24. }
  25. c[L/2+5][L/2+1]=1;  
  26. c[L/2+4][L/2+1]=1;  
  27. c[L/2+5][L/2-1]=1;  
  28. c[L/2+4][L/2-1]=1;  
  29. c[L/2+6][L/2]=1;  
  30. c[L/2+3][L/2]=1;  
  31. c[L/2+1][L/2]=1;  
  32. c[L/2+1][L/2]=1;  
  33. c[L/2+1][L/2+1]=1;
  34. c[L/2][L/2]=1;
  35. c[L/2][L/2+1]=1;
  36.  
  37.  
  38.  
  39.  
  40. for(t=0;t<t_max;t++){
  41.     for(x=1;x<L-1;x++){ //stampo
  42.     for(y=1;y<L-1;y++){
  43.         if(c[x][y]==1){fprintf(fp,"%d %d %d\n",x,y,t);}
  44.  
  45.     }
  46.     }
  47.  
  48.     for(x=1;x<L-1;x++){
  49.     for(y=1;y<L-1;y++){
  50.         somm=somma(x,y);    //vedo quanto vale la somma delle celle adiacenti
  51.         if(somm==3 && c[x][y]==0){ //giusta popolazione--->nascita
  52.             c1[x][y]=1;
  53.         }else if(somm!=3 && c[x][y]==0){c1[x][y]=0;}
  54.         if((somm==2||somm==3) && c[x][y]==1){ //sovra o sottopopolazione--->morte
  55.             c1[x][y]=1;
  56.         }else if((somm!=2&&somm!=3) && c[x][y]==1){c1[x][y]=0;}
  57.     }
  58.     }
  59.     for(x=1;x<L-1;x++){ //sostituzione
  60.     for(y=1;y<L-1;y++){
  61.         c[x][y]=c1[x][y];
  62.     }
  63.     }
  64. }
  65. system("gnuplot vita.gnu");
  66. }
  67.  
  68. int somma(int x,int y){
  69.     int somma;
  70.     somma=c[x][y+1]+c[x][y-1]+c[x-1][y]+c[x-1][y-1]+c[x-1][y+1]+c[x+1][y]+c[x+1][y-1]+c[x+1][y+1];
  71.     return somma;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment