-
Notifications
You must be signed in to change notification settings - Fork 0
/
computeNextGeneration.c
29 lines (27 loc) · 1.76 KB
/
computeNextGeneration.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
#include "includes.h"
//function to calculate the next generation
void computeNextGeneration(struct GameValues *value) {
int i, j, alive;
for (i = 0; i < ((value->size_y) + 1); i++) {
for (j = 0; j < ((value->size_x) + 1); j++) {
alive = 0;
alive += value->gameMapBuffer[(i - 1 + (value->size_y)) % (value->size_y)][(j - 1 + (value->size_x)) %
(value->size_x)];
alive += value->gameMapBuffer[(i - 1 + (value->size_y)) % (value->size_y)][j];
alive += value->gameMapBuffer[(i - 1 + (value->size_y)) % (value->size_y)][(j + 1 + (value->size_x)) %
(value->size_x)];
alive += value->gameMapBuffer[i][(j - 1 + (value->size_x)) % (value->size_x)];
alive += value->gameMapBuffer[i][(j + 1 + (value->size_x)) % (value->size_x)];
alive += value->gameMapBuffer[(i + 1 + (value->size_y)) % (value->size_y)][(j - 1 + (value->size_x)) %
(value->size_x)];
alive += value->gameMapBuffer[(i + 1 + (value->size_y)) % (value->size_y)][j];
alive += value->gameMapBuffer[(i + 1 + (value->size_y)) % (value->size_y)][(j + 1 + (value->size_x)) %
(value->size_x)];
//check rules and write to gameMap
if (value->gameMapBuffer[i][j] == 1 && (alive > 3 || alive < 2)) value->gameMap[i][j] = 0;
if (value->gameMapBuffer[i][j] == 0 && alive == 3) value->gameMap[i][j] = 1;
}
}
value->gen++;
return;
}