-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconway_auto.c
114 lines (99 loc) · 3.2 KB
/
conway_auto.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define gridSize 32
#define cellCount 200
#define simulationSize 100
#define stepDelay 100 // in milliseconds
#define PRINT_GRID 1
void printGrid(int grid[gridSize][gridSize]);
void determineState(int grid[gridSize][gridSize]);
void clearScreen(void);
void randomizeGrid(int grid[gridSize][gridSize], int numCells);
int main() {
int grid[gridSize][gridSize] = {};
double duration = 0, averageduration = 0;
srand(time(NULL));
clearScreen();
randomizeGrid(grid, cellCount);
printGrid(grid);
clock_t startTime = clock();
for (int i; i < simulationSize; i++) {
clock_t startTime = clock();
determineState(grid);
clock_t endTime = clock();
duration = (double)(endTime - startTime) / CLOCKS_PER_SEC;
averageduration += duration;
printGrid(grid);
printf("Iteration #%d/%d\n", i, simulationSize);
printf("Execution time of determineState: %f seconds\n", duration);
usleep(stepDelay * 1000); // 700 ms pause between generations
clearScreen();
}
clock_t endTime = clock();
duration = (double)(endTime - startTime) / CLOCKS_PER_SEC;
printGrid(grid);
printf("Simulation done after %d generations.\n", simulationSize);
printf("Execution time of life: %f seconds\n", duration);
printf("Average execution time of each generation: %f seconds\n",
averageduration / simulationSize);
return 0;
}
void clearScreen(void) {
printf("\033[2J\033[1;1H");
}
void randomizeGrid(int grid[gridSize][gridSize], int nc) {
for (int i = 0; i < nc; i++) {
int x = rand() % gridSize;
int y = rand() % gridSize;
grid[x][y] = 1;
}
}
void printGrid(int grid[gridSize][gridSize]) {
if (!PRINT_GRID) return;
for (int a = 0; a < gridSize; a++) {
for (int b = 0; b < gridSize; b++) {
if (grid[a][b] == 1) {
printf("O ");
} else {
printf(". ");
}
if (b == gridSize - 1) {
printf("\n");
}
}
}
}
void determineState(int grid[gridSize][gridSize]) {
// First pass to calculate new state
for (int a = 0; a < gridSize; a++) {
for (int b = 0; b < gridSize; b++) {
int alive = 0;
for (int c = -1; c <= 1; ++c) {
for (int d = -1; d <= 1; ++d) {
if (c == 0 && d == 0)
continue;
int x = a + c;
int y = b + d;
if (x >= 0 && x < gridSize && y >= 0 && y < gridSize &&
(grid[x][y] & 1)) {
++alive;
}
}
}
if (alive < 2 || alive > 3) {
grid[a][b] &= 1;
} else if (alive == 3 || grid[a][b] & 1) {
grid[a][b] |= 2;
}
}
}
// Second pass to normalize state back to 0 and 1
for (int a = 0; a < gridSize; a++) {
for (int b = 0; b < gridSize; b++) {
grid[a][b] >>=
1; // Shift to get the new state into the least significant bit
}
}
}