-
Notifications
You must be signed in to change notification settings - Fork 0
/
automata.c
99 lines (81 loc) · 2.08 KB
/
automata.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int* game_of_life(int board[5][5]) {
int m = 5;
int n = 5;
int moves[8][2] = {{-1, 1},
{-1, 0},
{-1, -1},
{0, -1},
{0, 1},
{1, 0},
{1, -1},
{1, 1}};
int dm = 8;
int grid[m][n];
memcpy(grid, board, sizeof(grid));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int total = 0;
for (int k = 0; k < dm; k++) {
int cr = i + moves[k][0];
int cc = j + moves[k][1];
if (cr >= 0 && cr < m && cc >= 0 && cc < n) {
total += grid[cr][cc];
}
}
if (total < 2 || total > 3) {
board[i][j] = 0;
} else if (total == 3) {
board[i][j] = 1;
} else {
board[i][j] = grid[i][j];
}
}
}
int *ptr;
ptr = &board[0][0];
return ptr;
}
int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
char *mapping = malloc(strlen(argv[2]) + 1);
strcpy(mapping,argv[2]);
FILE *file = fopen(mapping, "r");
char c;
int matrix[5][5];
for(int i =0; i < 5; i++)
{
for(int j=0; j<5; j++)
{
fscanf(file, " %c", &c);
matrix[i][j] = c - '0';
}
}
fclose(file);
while (n > 0){
int* ptr = game_of_life(matrix);
for(int i =0; i < 5; i++)
{
for(int j=0; j<5; j++)
{
matrix[i][j] = *(ptr + (i * 5) + j);
}
}
n--;
}
FILE*pfile=NULL;
pfile = fopen(strcat(mapping, ".out"), "w");
for(int k =0; k < 5; k++)
{
for(int l=0; l<5; l++)
{
printf("%d ", matrix[k][l]);
fprintf(pfile, "%d ", matrix[k][l]);
}
printf("\n");
fprintf(pfile, "\n");
}
fclose(pfile);
}