-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
87 lines (79 loc) · 2.17 KB
/
main.c
1
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdio_ext.h>#define SIZE_X 20 #define SIZE_Y 20int cells[SIZE_X][SIZE_Y];void loadPattern(){ FILE * file = fopen("pattern.txt","r"); for (int i = 0; i< SIZE_X; i++) for (int j = 0; j< SIZE_Y; j++) fscanf(file, "%d", &cells[i][j]); fclose(file);}int countAlive(int x, int y){ int i = 0; if (cells[(x + SIZE_Y - 1) % SIZE_Y][ y]) { i++;} if (cells[(x + SIZE_Y - 1) % SIZE_Y][ (y + SIZE_X - 1) % SIZE_X]) {i++;} if (cells[(x + SIZE_Y - 1) % SIZE_Y][ (y + 1) % SIZE_X]) {i++;} if (cells[x][ (y + SIZE_X - 1) % SIZE_X]) { i++;} if (cells[x][ (y + 1) % SIZE_X]) {i++;} if (cells[(x + 1) % SIZE_Y][ (y + SIZE_X - 1) % SIZE_X]) { i++;} if (cells[(x + 1) % SIZE_Y][y]) {i++;} if (cells[(x + 1) % SIZE_Y][ (y + 1) % SIZE_X]) {i++;} return i;}void generate(){ int c; int tmp[SIZE_Y][ SIZE_X]; for (int i = 0; i < SIZE_Y; ++i) for (int j = 0; j < SIZE_X; ++j) {tmp[i][j] = cells[i][j];} for (int i = 0; i < SIZE_Y; ++i) for (int j = 0; j < SIZE_X; ++j) { c = countAlive(i, j); if (cells[i][j]) if (c < 2 || c > 3) {tmp[i][j] = 0;} if (cells[i][j] == 0) if (c == 3) {tmp[i][j] = 1;} } for (int i = 0; i < SIZE_Y; ++i) for (int j = 0; j < SIZE_X; ++j) cells[i][j] = tmp[i][j];}void redraw(){ for (int i = 0; i< SIZE_X; i++){ for(int j = 0; j < SIZE_Y; j++){ if (cells[i][j]) {printf("%2c", '*');} else {printf("%2c", ' ');} } puts(""); }}void clrscr(void){ printf("\033[2J"); /* Clear the entire screen. */ printf("\033[0;0f"); /* Move cursor to the top left hand corner */} int main(int argc, char * argv[]){ puts("Type amount of cycles"); loadPattern(); int n ; scanf("%d",&n); __fpurge(stdin); for (int i = 0; i< n; i++){ system("sleep 0.1"); clrscr(); printf("Life game - %d cycle\n", i); redraw(); generate(); } return EXIT_SUCCESS;}