Advertisement
Finti

life.c

Nov 21st, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6.  
  7. int x;
  8. int y;
  9.  
  10. int i; //aby som vždy vo foroch nemusel inicializovať
  11. int j;
  12.  
  13. int **hra = NULL; //pole ktoré drží bunky hry
  14. int **susedia = NULL; //pole ktoré drží počet živých susedov bunky i j z hra[][] využívame o 2 z každej strany menšie, lebo hra[][] obsahuje aj vždy nulové okraje
  15.  
  16.  
  17. int main(void) {
  18.  
  19.     srand (time(NULL)); //refreshne seed na random
  20.  
  21.  
  22.     //fillRandom(5, 10);
  23.     nacitaj("test.txt");
  24.  
  25.  
  26.     int game = 10;
  27.     while(game > 1){
  28.     vypis();
  29.     nextGen();
  30.     game--;
  31.     sleep(1);
  32.     }
  33.     uloz("testUloz.txt");
  34.     return 0;
  35. }
  36.  
  37. void createArrays(void){
  38.     hra = malloc(x*sizeof(int*)); //vytváranie dynamicky deklarovaných polí
  39.     susedia = malloc(x*sizeof(int*));
  40.     for(i=0;i<x;i++){
  41.         hra[i] = malloc(y*sizeof(int));
  42.         susedia[i] = malloc(y*sizeof(int));
  43.     }
  44.  
  45.     for(i = 0; i < x; i++){ //naplnenie hra[][] nulami
  46.         for(j = 0; j < y; j++){
  47.             hra[i][j] = 0;
  48.         }
  49.     }
  50.  
  51.     for(i = 0; i < x; i++){ //naplnenie susedia[][] nulami
  52.         for(j = 0; j < y; j++){
  53.             susedia[i][j] = 0;
  54.         }
  55.     }
  56. }
  57.  
  58. void fillRandom(int py, int px){
  59.     y = py;
  60.     x = px;
  61.     createArrays();    for(i = 1; i < x-1; i++){ //naplnenie všetkého v hra[][] okrem okrajov randomom 0/1
  62.         for(j = 1; j < y-1; j++){
  63.             hra[i][j] = rand() % 2;
  64.         }
  65.     }
  66. }
  67.  
  68. void nextGen(void) { //vypočíta ďalšiu generáciu
  69.  
  70.     for(i = 1; i <= (x-2); i++){
  71.         for(j = 1; j <= (y-2); j++){
  72.             susedia[i][j] = hra[i-1][j-1] +
  73.             hra[i-1][j] +
  74.             hra[i-1][j+1] +
  75.             hra[i][j-1] +
  76.             hra[i][j+1] +
  77.             hra[i+1][j-1] +
  78.             hra[i+1][j] +
  79.             hra[i+1][j+1];
  80.         }
  81.     }
  82.  
  83.  
  84.     for(i = 1; i <= (x-2); i++){
  85.         for(j = 1; j <= (y-2); j++){
  86.             if(susedia[i][j] > 3 || susedia[i][j] < 2){
  87.                 hra[i][j] = 0;
  88.             }
  89.             if(susedia[i][j] == 2 && hra[i][j] == 1){
  90.                 hra[i][j] = 1;
  91.             }
  92.             if(susedia[i][j] == 3){
  93.                 hra[i][j] = 1;
  94.             }
  95.         }
  96.     }
  97. }
  98. void vypis(void) {
  99.     printf("\f");
  100.     for(i = 0; i <= (x-2)+1; i++){
  101.         for(j = 0; j <= (y-2)+1; j++){
  102.             if(hra[i][j] == 1) {
  103.                 printf("X");
  104.             } else {
  105.                 printf(".");
  106.             }
  107.         }
  108.         printf("\n");
  109.     }
  110.     printf("\n");
  111. }
  112.  
  113. /*
  114. uloží dáta do súboru vo formáte rovnakom ako nacitaj()
  115. */
  116. void uloz(char filename[50]){
  117.     FILE * fPointer = fopen(filename, "w");
  118.  
  119.     fprintf(fPointer, "%d %d\n", y, x);
  120.  
  121.     for(i = 0; i <= (x-2)+1; i++){
  122.         for(j = 0; j <= (y-2)+1; j++){
  123.             fprintf(fPointer,"%d ", hra[i][j]);
  124.         }
  125.         fprintf(fPointer,"\n");
  126.     }
  127.     fclose(fPointer);
  128. }
  129.  
  130.  
  131. /*
  132. načíta dáta zo súboru formátu
  133. x y
  134. 1 0 0 1 1
  135. 1 0 0 0 0
  136. 0 1 1 1 1
  137. ...
  138. */
  139. void nacitaj(char filename[50]){
  140.     FILE * fPointer = fopen(filename, "r");
  141.     if(fPointer != NULL) {
  142.  
  143.         fscanf(fPointer, "%d", &y);
  144.         //printf("Nacitane y:%d ", y); //využívané pri debuggingu
  145.         fscanf(fPointer, "%d", &x);
  146.         //printf("Nacitane x:%d\n", x);
  147.         createArrays();
  148.  
  149.         for(i = 0; i <= (x-2)+1; i++){
  150.             for(j = 0; j <= (y-2)+1; j++){
  151.                 fscanf(fPointer,"%d", &hra[i][j]);
  152.                 //printf("%d", hra[i][j]);
  153.             }
  154.             //printf("\n");
  155.         }
  156.         fclose(fPointer);
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement