-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_of_life.c
135 lines (115 loc) · 4.32 KB
/
game_of_life.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
// Copyright (c) 2019 Claus Jørgensen
// Conway's Game of Life
// https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int find_neighbors(char neighbors[8],
const int width,
const int height,
const char map[width][height],
const int x,
const int y) {
int count = 0;
if ((x - 1) >= 0) {
neighbors[count++] = map[x - 1][y]; // left
}
if ((x + 1) < width) {
neighbors[count++] = map[x + 1][y]; // right
}
if ((y - 1) >= 0) {
neighbors[count++] = map[x][y - 1]; // top
}
if ((y + 1) < height) {
neighbors[count++] = map[x][y + 1]; // bottom
}
if ((x - 1) >= 0 && (y - 1) >= 0) {
neighbors[count++] = map[x - 1][y - 1]; // top-left
}
if ((x - 1) >= 0 && (y + 1) < height) {
neighbors[count++] = map[x - 1][y + 1]; // bottom-left
}
if ((x + 1) < width && (y + 1) < height) {
neighbors[count++] = map[x + 1][y + 1]; // top-right
}
if ((x + 1) < width && (y - 1) >= 0) {
neighbors[count++] = map[x + 1][y - 1]; // bottom-right
}
return count;
}
void transition(const int width,
const int height,
const char map[width][height],
char newMap[width][height]) {
char tmpMap[width][height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
static char neighbors[8];
int count = find_neighbors(neighbors, width, height, map, x, y);
int numAlive = 0;
for (int n = 0; n < count; n++) {
numAlive = neighbors[n] == 1 ? numAlive + 1 : numAlive;
}
if (numAlive < 2) {
tmpMap[x][y] = 0; // death by under population
} else if (numAlive > 3) {
tmpMap[x][y] = 0; // death by over population
} else if (map[x][y] == 0 && numAlive == 3) {
tmpMap[x][y] = 1; // alive by reproduction
} else {
tmpMap[x][y] = map[x][y]; // continue unchanged
}
}
}
memcpy(newMap, tmpMap, width * height);
}
void print_map(const int width,
const int height,
const char map[width][height]) {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (map[x][y] == 1) {
printf("+ ");
} else {
printf(" ");
}
}
printf("\n");
}
}
#define PENTADECATHLON_WIDTH 20
#define PENTADECATHLON_HEIGHT 20
#define PENTADECATHLON { \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } \
}
int main(int argc, const char * argv[]) {
const int width = PENTADECATHLON_WIDTH;
const int height = PENTADECATHLON_HEIGHT;
char map[width][height] = PENTADECATHLON;
while (1) {
system("clear");
print_map(width, height, map);
transition(width, height, map, map);
sleep(1);
}
return 0;
}