Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int ** dynamic_array_alloc(int N, int M);
- void dynamic_array_free(int **A, int N);
- void game_of_life(int **A, int **B, int n, int m);
- void current_iteration(int **A, int **B, int n, int m);
- int alive(int **a, int y, int x, int n, int m);
- void print_dynamic_array(int **A, int n, int m);
- int main() {
- int N = 0;
- int xMax = 5;
- int yMax = 5;
- int **A = dynamic_array_alloc(yMax, xMax);
- int **B = dynamic_array_alloc(yMax, xMax);
- for (int i = 0; i < yMax; i++) {
- for (int j = 0; j < xMax; j++) {
- char c;
- if (scanf("%d%c", &A[i][j], &c) == 2 && c == ' ' || c == '\n') {
- continue;
- } else {
- N = -1;
- break;
- }
- }
- }
- //printf("ffffffffff");
- printf("ssssssssssssss");
- game_of_life(A, B, yMax, xMax);
- printf("\n");
- /*for (int i = 0; i < yMax; i++) {
- for (int j = 0; j < xMax; j++) {
- printf("%d ", B[i][j]);
- }
- printf("\n");
- } */
- dynamic_array_free(A, yMax);
- dynamic_array_free(B, yMax);
- }
- int ** dynamic_array_alloc(int N, int M) {
- int **A = malloc(N*sizeof(int *));
- for (int i = 0; i < N; i++) {
- A[i] = malloc(M*sizeof(int));
- }
- return A;
- }
- void dynamic_array_free(int **A, int N) {
- for (int i = 0; i < N; i++) {
- free(A[i]);
- }
- free(A);
- }
- void game_of_life(int **A, int **B, int n, int m) {
- int N = 0; //number of iterations
- while (1) {
- //printf("qqqqqqqqqqqqqq\n");
- current_iteration(A, B, n, m);
- ++N;
- print_dynamic_array(A, n, m);
- char c;
- scanf("%c", &c);
- if (c=='9') {
- continue;
- } else {
- break;
- }
- if (N==9) {
- break;
- }
- current_iteration(B, A, n, m);
- print_dynamic_array(B, n, m);
- //char c;
- scanf("%c", &c);
- if (c=='9') {
- continue;
- } else {
- break;
- }
- ++N;
- }
- }
- void current_iteration(int **a, int **b, int n, int m) {
- for (int y = 0; y < n; y++) {
- for (int x = 0; x < m; x++) {
- //printf("GGGGGGGGGGG");
- int al = alive(a, y , x, n, m);
- if (a[y][x] == 1) {
- if (al == 2 || al == 3) {
- b[y][x] = 1;
- }
- if (al > 3 || al < 2) {
- b[y][x] = 0;
- }
- } else {
- if (al == 3) {
- b[y][x] = 1;
- }
- }
- }
- }
- }
- int alive(int **a, int y, int x, int n, int m) {
- int ret = 0;
- int tmp = 0;
- //printf("aaaaa: %d %d\n", y, x);
- for (int i = -1; i < 2; ++i) {
- for (int j = -1; j < 2; ++j) {
- //printf("cycle %d %d\n", j, i);
- if (j ==0 && i == 0) {
- } else {
- if (y + i < 0) {
- i +=n;
- }
- if (x +j < 0) {
- j+=m;
- }
- if (a[(y + i)%n][(x + j)%m] == 1) {
- //printf("сосед: %d %d\n", y+i, x+j);
- tmp +=1;
- //printf("коорд: %d %d %d\n", y, x, tmp);
- }
- if (i == n - 1) {
- i = -1;
- }
- if (j == m - 1) {
- j = -1;
- }
- }
- }
- }
- //printf("tmp = %d\n", tmp);
- return tmp;
- }
- void print_dynamic_array(int **A, int n, int m) {
- printf("\033[2J\033[1;1H");
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- printf("%d ", A[i][j]);
- }
- printf("\n");
- }
- }
Add Comment
Please, Sign In to add comment