Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <unistd.h>
- #include <string.h>
- int x;
- int y;
- int i; //aby som vždy vo foroch nemusel inicializovať
- int j;
- int **hra = NULL; //pole ktoré drží bunky hry
- 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
- int main(void) {
- srand (time(NULL)); //refreshne seed na random
- //fillRandom(5, 10);
- nacitaj("test.txt");
- int game = 10;
- while(game > 1){
- vypis();
- nextGen();
- game--;
- sleep(1);
- }
- uloz("testUloz.txt");
- return 0;
- }
- void createArrays(void){
- hra = malloc(x*sizeof(int*)); //vytváranie dynamicky deklarovaných polí
- susedia = malloc(x*sizeof(int*));
- for(i=0;i<x;i++){
- hra[i] = malloc(y*sizeof(int));
- susedia[i] = malloc(y*sizeof(int));
- }
- for(i = 0; i < x; i++){ //naplnenie hra[][] nulami
- for(j = 0; j < y; j++){
- hra[i][j] = 0;
- }
- }
- for(i = 0; i < x; i++){ //naplnenie susedia[][] nulami
- for(j = 0; j < y; j++){
- susedia[i][j] = 0;
- }
- }
- }
- void fillRandom(int py, int px){
- y = py;
- x = px;
- createArrays(); for(i = 1; i < x-1; i++){ //naplnenie všetkého v hra[][] okrem okrajov randomom 0/1
- for(j = 1; j < y-1; j++){
- hra[i][j] = rand() % 2;
- }
- }
- }
- void nextGen(void) { //vypočíta ďalšiu generáciu
- for(i = 1; i <= (x-2); i++){
- for(j = 1; j <= (y-2); j++){
- susedia[i][j] = hra[i-1][j-1] +
- hra[i-1][j] +
- hra[i-1][j+1] +
- hra[i][j-1] +
- hra[i][j+1] +
- hra[i+1][j-1] +
- hra[i+1][j] +
- hra[i+1][j+1];
- }
- }
- for(i = 1; i <= (x-2); i++){
- for(j = 1; j <= (y-2); j++){
- if(susedia[i][j] > 3 || susedia[i][j] < 2){
- hra[i][j] = 0;
- }
- if(susedia[i][j] == 2 && hra[i][j] == 1){
- hra[i][j] = 1;
- }
- if(susedia[i][j] == 3){
- hra[i][j] = 1;
- }
- }
- }
- }
- void vypis(void) {
- printf("\f");
- for(i = 0; i <= (x-2)+1; i++){
- for(j = 0; j <= (y-2)+1; j++){
- if(hra[i][j] == 1) {
- printf("X");
- } else {
- printf(".");
- }
- }
- printf("\n");
- }
- printf("\n");
- }
- /*
- uloží dáta do súboru vo formáte rovnakom ako nacitaj()
- */
- void uloz(char filename[50]){
- FILE * fPointer = fopen(filename, "w");
- fprintf(fPointer, "%d %d\n", y, x);
- for(i = 0; i <= (x-2)+1; i++){
- for(j = 0; j <= (y-2)+1; j++){
- fprintf(fPointer,"%d ", hra[i][j]);
- }
- fprintf(fPointer,"\n");
- }
- fclose(fPointer);
- }
- /*
- načíta dáta zo súboru formátu
- x y
- 1 0 0 1 1
- 1 0 0 0 0
- 0 1 1 1 1
- ...
- */
- void nacitaj(char filename[50]){
- FILE * fPointer = fopen(filename, "r");
- if(fPointer != NULL) {
- fscanf(fPointer, "%d", &y);
- //printf("Nacitane y:%d ", y); //využívané pri debuggingu
- fscanf(fPointer, "%d", &x);
- //printf("Nacitane x:%d\n", x);
- createArrays();
- for(i = 0; i <= (x-2)+1; i++){
- for(j = 0; j <= (y-2)+1; j++){
- fscanf(fPointer,"%d", &hra[i][j]);
- //printf("%d", hra[i][j]);
- }
- //printf("\n");
- }
- fclose(fPointer);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement