diff options
author | Gregor Kleen <kleen@cip.ifi.lmu.de> | 2016-11-03 21:42:59 +0100 |
---|---|---|
committer | Gregor Kleen <kleen@cip.ifi.lmu.de> | 2016-11-03 21:42:59 +0100 |
commit | 1d8b83da2a84a464e581a50c097f29e98b4622e1 (patch) | |
tree | fafd4cda37dbfba5c01d768a5cc52feec3116709 | |
parent | f0727faab76249aa956012cf060a03ed93e01f26 (diff) | |
download | sysprak.exc-1d8b83da2a84a464e581a50c097f29e98b4622e1.tar sysprak.exc-1d8b83da2a84a464e581a50c097f29e98b4622e1.tar.gz sysprak.exc-1d8b83da2a84a464e581a50c097f29e98b4622e1.tar.bz2 sysprak.exc-1d8b83da2a84a464e581a50c097f29e98b4622e1.tar.xz sysprak.exc-1d8b83da2a84a464e581a50c097f29e98b4622e1.zip |
-rw-r--r-- | Makefile | 7 | ||||
-rw-r--r-- | gameOfLife.c | 173 |
2 files changed, 178 insertions, 2 deletions
@@ -5,11 +5,11 @@ PREFIX = /usr/local | |||
5 | 5 | ||
6 | .PHONY: all install | 6 | .PHONY: all install |
7 | 7 | ||
8 | all: temperaturUmrechner matrixMult findMaxOfFloats | 8 | all: temperaturUmrechner matrixMult findMaxOfFloats gameOfLife |
9 | 9 | ||
10 | install: | 10 | install: |
11 | mkdir -p $(PREFIX)/bin | 11 | mkdir -p $(PREFIX)/bin |
12 | install -m 0755 -t $(PREFIX)/bin temperaturUmrechner matrixMult findMaxOfFloats | 12 | install -m 0755 -t $(PREFIX)/bin temperaturUmrechner matrixMult findMaxOfFloats gameOfLife |
13 | mkdir -p $(PREFIX)/share | 13 | mkdir -p $(PREFIX)/share |
14 | install -m 0644 -t $(PREFIX)/share floats.txt | 14 | install -m 0644 -t $(PREFIX)/share floats.txt |
15 | 15 | ||
@@ -21,3 +21,6 @@ matrixMult: | |||
21 | 21 | ||
22 | findMaxOfFloats: | 22 | findMaxOfFloats: |
23 | $(CC) $(INCLUDES) $(CCFLAGS) -o findMaxOfFloats findMaxOfFloats.c | 23 | $(CC) $(INCLUDES) $(CCFLAGS) -o findMaxOfFloats findMaxOfFloats.c |
24 | |||
25 | gameOfLife: | ||
26 | $(CC) $(INCLUDES) $(CCFLAGS) -o gameOfLife gameOfLife.c | ||
diff --git a/gameOfLife.c b/gameOfLife.c new file mode 100644 index 0000000..e016993 --- /dev/null +++ b/gameOfLife.c | |||
@@ -0,0 +1,173 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include <stdio.h> | ||
3 | #include <getopt.h> | ||
4 | #include <unistd.h> | ||
5 | #include <string.h> | ||
6 | |||
7 | #define RANDOM_SEED time(NULL) | ||
8 | |||
9 | typedef enum CellState { DEAD, ALIVE } CellState; | ||
10 | |||
11 | void printUsage() | ||
12 | { | ||
13 | printf("usage: gameOfLife [-x <SIZE>] [-y <SIZE>] [-d <FLOAT>] [-s <SECONDS>]\n" | ||
14 | " -d <FLOAT> sets density of life cells at game start\n" | ||
15 | " -s <SECONDS> time to sleep between rounds\n" | ||
16 | ); | ||
17 | } | ||
18 | |||
19 | void initBoard(CellState* board, unsigned int x, unsigned int y, float density) | ||
20 | { | ||
21 | float roll; | ||
22 | |||
23 | srand(RANDOM_SEED); | ||
24 | for (int i = 0; i < x * y; i++) | ||
25 | { | ||
26 | roll = (float)rand()/(float)RAND_MAX; | ||
27 | if (roll <= density) | ||
28 | board[i] = ALIVE; | ||
29 | else | ||
30 | board[i] = DEAD; | ||
31 | } | ||
32 | } | ||
33 | |||
34 | int boardEq(CellState* a, CellState* b, unsigned int x, unsigned int y) | ||
35 | { | ||
36 | for (int i = 0; i < x * y; i++) | ||
37 | { | ||
38 | if (a[i] != b[i]) | ||
39 | return 0; | ||
40 | } | ||
41 | |||
42 | return 1; | ||
43 | } | ||
44 | |||
45 | void evolve(CellState* board, CellState* newBoard, unsigned int width, unsigned int height) | ||
46 | { | ||
47 | int liveNeighbours; | ||
48 | CellState* focus; | ||
49 | CellState* newFocus; | ||
50 | |||
51 | for (int y = 0; y < height; y++) | ||
52 | for (int x = 0; x < width; x++) | ||
53 | { | ||
54 | focus = &(board[y * width + x]); | ||
55 | newFocus = &(newBoard[y * width + x]); | ||
56 | liveNeighbours = 0; | ||
57 | |||
58 | for (int n = (y <= 0) ? 0 : -1; n <= ((y + 1) >= height ? 0 : 1); n++) | ||
59 | for (int m = (x <= 0) ? 0 : -1; m <= ((x + 1) >= width ? 0 : 1); m++) | ||
60 | { | ||
61 | // fprintf(stderr, "(%d, %d) + (%d, %d)\n", x, y, m, n); // DEBUG | ||
62 | if (board[(y + n) * width + (x + m)] == ALIVE && !(n == 0 && m == 0)) | ||
63 | liveNeighbours++; | ||
64 | } | ||
65 | |||
66 | if (liveNeighbours == 3) | ||
67 | *newFocus = ALIVE; | ||
68 | else if (*focus == ALIVE && liveNeighbours == 2) | ||
69 | *newFocus = ALIVE; | ||
70 | else | ||
71 | *newFocus = DEAD; | ||
72 | } | ||
73 | } | ||
74 | |||
75 | void printBoard(CellState* board, unsigned int width, unsigned int height) | ||
76 | { | ||
77 | void drawHoriz() { | ||
78 | printf("+"); | ||
79 | for (int x = 0; x < width; x++) | ||
80 | printf("-"); | ||
81 | printf("+\n"); | ||
82 | } | ||
83 | |||
84 | drawHoriz(); | ||
85 | |||
86 | for (int y = 0; y < height; y++) | ||
87 | { | ||
88 | printf("|"); | ||
89 | for (int x = 0; x < width; x++) | ||
90 | if (board[y * width + x] == ALIVE) | ||
91 | { | ||
92 | printf("X"); | ||
93 | } | ||
94 | else | ||
95 | { | ||
96 | printf(" "); | ||
97 | } | ||
98 | printf("|"); | ||
99 | printf("\n"); | ||
100 | } | ||
101 | |||
102 | drawHoriz(); | ||
103 | } | ||
104 | |||
105 | void main(int argc, char* argv[]) | ||
106 | { | ||
107 | unsigned int x = 16; | ||
108 | unsigned int y = 16; | ||
109 | float d = 0; | ||
110 | float s = 1; | ||
111 | |||
112 | CellState* board; | ||
113 | CellState* newBoard; | ||
114 | |||
115 | int c; | ||
116 | |||
117 | while ((c = getopt(argc, argv, "x:y:d:s:h?")) != -1) | ||
118 | { | ||
119 | switch (c) | ||
120 | { | ||
121 | case 'x': | ||
122 | sscanf(optarg, "%d", &x); | ||
123 | break; | ||
124 | case 'y': | ||
125 | sscanf(optarg, "%d", &y); | ||
126 | break; | ||
127 | case 'd': | ||
128 | sscanf(optarg, "%f", &d); | ||
129 | break; | ||
130 | case 's': | ||
131 | sscanf(optarg, "%f", &s); | ||
132 | break; | ||
133 | case 'h': | ||
134 | case '?': | ||
135 | printUsage(); | ||
136 | exit(EXIT_SUCCESS); | ||
137 | default: | ||
138 | printUsage(); | ||
139 | exit(2); | ||
140 | } | ||
141 | } | ||
142 | |||
143 | if (d < 0 || d > 1) | ||
144 | { | ||
145 | fprintf(stderr, "Densitiy needs to be in [0;1]\n"); | ||
146 | exit(2); | ||
147 | } | ||
148 | |||
149 | if (s <= 0) | ||
150 | { | ||
151 | fprintf(stderr, "Sleep interval needs to be greater 0\n"); | ||
152 | exit(2); | ||
153 | } | ||
154 | |||
155 | board = malloc(sizeof(CellState) * x * y); | ||
156 | newBoard = malloc(sizeof(CellState) * x * y); | ||
157 | |||
158 | memset(board, DEAD, sizeof(CellState) * x * y); | ||
159 | initBoard(newBoard, x, y, d); | ||
160 | |||
161 | while (! boardEq(board, newBoard, x, y)) | ||
162 | { | ||
163 | printBoard(newBoard, x, y); | ||
164 | |||
165 | usleep((int)(s * 1e6)); | ||
166 | |||
167 | memcpy(board, newBoard, sizeof(CellState) * x * y); | ||
168 | evolve(board, newBoard, x, y); | ||
169 | } | ||
170 | |||
171 | free(board); | ||
172 | exit(EXIT_SUCCESS); | ||
173 | } | ||