-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.c
345 lines (256 loc) · 8.5 KB
/
methods.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "./methods.h"
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include "../patterns/main.h"
#include "../utilities.h"
#include "./macros.h"
#include "./structs.h"
int countAliveNeighbors(TGame* pGame, const int cellRow, const int cellCol, const int radius) {
int i;
int j;
const int startRow = cellRow - radius;
const int startCol = cellCol - radius;
const int endRow = cellRow + radius + 1;
const int endCol = cellCol + radius + 1;
size_t aliveNeighbors = 0;
for (i = startRow; i < endRow; i++) {
if (i > pGame->rows - 1) break;
if (i < 0) continue;
for (j = startCol; j < endCol; j++) {
if (j > pGame->cols - 1) break;
if (j < 0) continue;
if (i == cellRow && j == cellCol) continue;
if (pGame->dashboard[i][j] == ALIVE_CELL || pGame->dashboard[i][j] == DEAD_CELL_NG)
aliveNeighbors++;
}
}
return aliveNeighbors;
}
void drawPattern(TGame* pGame, const char* pattern) {
TPattern SPattern;
fillDashboard(pGame, DEAD_CELL);
if (strcmpi(pattern, "glider") == 0) {
newGliderPattern(&SPattern);
pGame->cellsAlive = 5;
} else if (strcmpi(pattern, "glider cannon") == 0) {
newGliderCannonPattern(&SPattern);
pGame->cellsAlive = 36;
} else if (strcmpi(pattern, "press") == 0) {
newPressPattern(&SPattern);
pGame->cellsAlive = 48;
} else if (strcmpi(pattern, "toad") == 0) {
newToadPattern(&SPattern);
pGame->cellsAlive = 6;
} else {
return;
}
pGame->cellsDead = (pGame->cols * pGame->rows) - pGame->cellsAlive;
pGame->generation = 0;
drawPatternInDashboard(pGame, &SPattern);
destroy2DArray(SPattern.arr, SPattern.rows, SPattern.cols);
}
void drawPatternInDashboard(TGame* pGame, TPattern* pPattern) {
size_t i;
size_t j;
size_t pI = 0;
size_t pJ = 0;
int startRow;
int startCol;
if (pPattern->rows > pGame->rows || pPattern->cols > pGame->cols) {
destroy2DArray(pGame->dashboard, pGame->rows, pGame->cols);
pGame->dashboard = new2DArray(pPattern->rows, pPattern->cols);
pGame->rows = pPattern->rows;
pGame->cols = pPattern->cols;
pGame->cellsDead = (pGame->rows * pGame->cols) - pGame->cellsAlive;
setDashboardCenter(pGame);
fillDashboard(pGame, DEAD_CELL);
}
startRow = pGame->center[0] - pPattern->center[0];
startCol = pGame->center[1] - pPattern->center[1];
for (i = startRow; pI < pPattern->rows; i++) {
if (i < 0) continue;
if (i > pGame->rows - 1) break;
for (j = startCol; pJ < pPattern->cols; j++) {
if (j < 0) continue;
if (j > pGame->cols - 1) break;
pGame->dashboard[i][j] = pPattern->arr[pI][pJ];
pJ++;
};
pJ = 0;
pI++;
}
}
void fillDashboard(TGame* pGame, const char with) {
size_t i;
size_t j;
for (i = 0; i < pGame->rows; i++) {
for (j = 0; j < pGame->cols; j++) {
pGame->dashboard[i][j] = with;
}
}
}
void generateNextGeneration(TGame* pGame) {
size_t i;
size_t j;
size_t aliveNeighbors;
for (i = 0; i < pGame->rows; i++) {
for (j = 0; j < pGame->cols; j++) {
aliveNeighbors = countAliveNeighbors(pGame, i, j, NEIGHBORHOOD_RADIUS);
if (pGame->dashboard[i][j] == DEAD_CELL) {
if (aliveNeighbors == 3) {
pGame->cellsDead--;
pGame->cellsAlive++;
pGame->dashboard[i][j] = ALIVE_CELL_NG;
};
continue;
}
if (aliveNeighbors < 2 || aliveNeighbors > 3) {
pGame->cellsAlive--;
pGame->cellsDead++;
pGame->dashboard[i][j] = DEAD_CELL_NG;
};
}
}
for (i = 0; i < pGame->rows; i++) {
for (j = 0; j < pGame->cols; j++) {
if (pGame->dashboard[i][j] == DEAD_CELL_NG) {
pGame->dashboard[i][j] = DEAD_CELL;
continue;
}
if (pGame->dashboard[i][j] == ALIVE_CELL_NG) pGame->dashboard[i][j] = ALIVE_CELL;
}
}
}
void printDashboardByConsole(TGame* pGame) {
size_t i;
size_t j;
for (i = 0; i < pGame->rows; i++) {
printf("\n");
for (j = 0; j < pGame->cols; j++) {
printf("%c", pGame->dashboard[i][j]);
}
}
}
void printGameByConsole(TGame* pGame) {
size_t i;
size_t j;
// Print header
for (i = 0; i < pGame->cols + 2; i++) printf("-");
printf("\n| Cells alive: %*d |", pGame->cols - 17 + 2, pGame->cellsAlive);
printf("\n| Cells dead: %*d |", pGame->cols - 16 + 2, pGame->cellsDead);
printf("\n| Generation: %*d |", pGame->cols - 16 + 2, pGame->generation);
if (pGame->maximumGeneration == INT_MAX) {
printf("\n| Maximum generation: %*s |", pGame->cols - 25 + 3, "infinity");
} else {
printf("\n| Maximum generation: %*d |", pGame->cols - 25 + 3, pGame->maximumGeneration);
}
printf("\n| Delay between generations: %*d ms |\n", pGame->cols - 35 + 3,
pGame->delayBetweenGenerations);
// Print dashboard
for (i = 0; i < pGame->cols + 2; i++) printf("-");
for (i = 0; i < pGame->rows; i++) {
printf("\n|");
for (j = 0; j < pGame->cols; j++) {
printf("%c", pGame->dashboard[i][j]);
}
printf("|");
}
printf("\n");
for (i = 0; i < pGame->cols + 2; i++) printf("-");
}
void setDashboardCenter(TGame* pGame) {
const int row = pGame->rows / 2;
const int col = pGame->cols / 2;
pGame->center[0] = row;
pGame->center[1] = col;
}
int setDashboardFromFile(const char* filePath, TGame* pGame, const int minRows, const int minCols) {
FILE* pf;
TPattern pattern;
char* line;
const size_t lineLength = 100;
char* row;
char* col;
char* sep;
int rowInt;
int colInt;
int rows = minRows;
int cols = minCols;
int patternRows = 0;
int patternCols = 0;
pf = fopen(filePath, "rt");
if (pf == NULL) return 0;
line = malloc(sizeof(char) * (lineLength + 1));
if (line == NULL) {
fclose(pf);
return 0;
};
*(line + lineLength) = '\0';
fgets(line, lineLength, pf);
while (fgets(line, lineLength, pf)) {
row = line;
sep = strrchr(line, ';');
if (sep == NULL) continue;
*sep = '\0';
col = sep + 1;
sscanf(row, "%d", &rowInt);
sscanf(col, "%d", &colInt);
patternRows = MAX(rowInt, patternRows);
patternCols = MAX(colInt, patternCols);
}
rows = MAX(patternRows, rows);
cols = MAX(patternCols, cols);
pGame->dashboard = new2DArray(rows, cols);
pGame->rows = rows;
pGame->cols = cols;
pGame->cellsAlive = 0;
pGame->generation = 0;
setDashboardCenter(pGame);
fillDashboard(pGame, DEAD_CELL);
pattern.arr = new2DArray(patternRows, patternCols);
pattern.rows = patternRows;
pattern.cols = patternCols;
setPatternCenter(&pattern);
fillPattern(&pattern, DEAD_CELL);
rewind(pf);
fgets(line, lineLength, pf);
while (fgets(line, lineLength, pf)) {
row = line;
sep = strrchr(line, ';');
if (sep == NULL) continue;
*sep = '\0';
col = sep + 1;
sscanf(row, "%d", &rowInt);
sscanf(col, "%d", &colInt);
pattern.arr[rowInt - 1][colInt - 1] = ALIVE_CELL;
pGame->cellsAlive++;
}
pGame->cellsDead = (cols * rows) - pGame->cellsAlive;
drawPatternInDashboard(pGame, &pattern);
destroy2DArray(pattern.arr, pattern.rows, pattern.cols);
fclose(pf);
free(line);
return 1;
}
void startGameByConsole(TGame* pGame, const int maxGeneration, const int delayBetweenGenerations) {
size_t generation = 0;
unsigned char isToInfinity = maxGeneration == INT_MAX;
pGame->generation = 0;
pGame->maximumGeneration = maxGeneration;
pGame->delayBetweenGenerations = delayBetweenGenerations;
system("cls");
printGameByConsole(pGame);
if (generation == maxGeneration) return;
sleep(delayBetweenGenerations);
while (isToInfinity || generation < maxGeneration) {
generateNextGeneration(pGame);
if (generation != INT_MAX) {
generation++;
pGame->generation = generation;
};
system("cls");
printGameByConsole(pGame);
if (generation != maxGeneration) sleep(delayBetweenGenerations);
}
}