-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.c
76 lines (68 loc) · 1.63 KB
/
worker.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
#include "worker.h"
void
worker_add(Worker worker, int x, int y)
{
chunk_flip(worker.chunk);
chunk_set(worker.chunk, x, y, 1);
chunk_flip(worker.chunk);
}
void
worker_clear(Worker worker)
{
for (int y = 0; y < worker.chunk->size_y; y++) {
for (int x = 0; x < worker.chunk->size_x; x++) {
chunk_set(worker.chunk, x, y, 0);
}
}
chunk_flip(worker.chunk);
}
int
min(int x, int y)
{
return x < y ? x : y;
}
int
max(int x, int y)
{
return x > y ? x : y;
}
int
count_neighbors(Chunk *chunk, int x, int y)
{
int neighbors = 0;
for (int y1 = max(0, y - 1); y1 <= min(chunk->size_y - 1, y + 1); y1++) {
for (int x1 = x - 1; x1 <= x + 1; x1++) {
if ((x1 != x) || (y1 != y)) {
neighbors += chunk_get(chunk, x1, y1);
}
}
}
return neighbors;
}
void
worker_evaluate(Worker worker)
{
for (int y = 0; y < worker.chunk->size_y; y++) {
for (int x = 0; x < worker.chunk->size_x; x++) {
int neighbors = count_neighbors(worker.chunk, x, y);
char current = chunk_get(worker.chunk, x, y);
if ((neighbors < 2) || (neighbors > 3)) {
chunk_set(worker.chunk, x, y, 0);
}
else if (neighbors == 3) {
chunk_set(worker.chunk, x, y, 1);
}
else {
chunk_set(worker.chunk, x, y, current);
}
}
}
chunk_flip(worker.chunk);
}
void
worker_get_string(Worker worker, int y, char *string)
{
for (int x = 0; x < worker.chunk->size_x; x++) {
string[x] = chunk_get(worker.chunk, x, y);
}
}