-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
147 lines (126 loc) · 3.05 KB
/
main.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
#include <stdio.h>
#include <sys/time.h>
#include "sleep.h"
/*
* Build and run with:
* gcc main.c -o a.out && ./a.out
*
* This has been tested on Ubuntu 20.04
*/
#define WIDTH 80
#define HEIGHT 10
#define CELL_TO_STR(X) X == 1 ? "X" : " "
typedef struct Board {
int width;
int height;
int cells[HEIGHT][WIDTH];
int nextCells[HEIGHT][WIDTH];
} board;
void printHeader(board* b) {
printf("+");
for (int i = 0; i < b->width; i++)
printf("-");
printf("+\n");
}
void show(board* b) {
printHeader(b);
for (int y = 0; y < b->height; y++) {
// print a line:
printf("|");
for (int x = 0; x < b->width; x++)
printf("%s", CELL_TO_STR(b->cells[y][x]));
printf("|\n");
}
printHeader(b);
}
int get(int x, int y, board* b) {
// wrap board like a torus
if (y < 0)
y = b->height + y;
if (x < 0)
x = b->width + x;
if (y >= b->height)
y = b->height - y;
if (x >= b->width)
x = b->width - x;
return b->cells[y][x];
}
int countNeighbors(int x, int y, board* b) {
int n = 0;
// dx and dy are deltas from the given (x, y) point,
// Here is a graph:
/*
(x-1, y-1) | (x, y-1) | (x+1, y-1)
(x-1, y ) | (x, y ) | (x+1, y )
(x-1, y+1) | (x, y+1) | (x+1, y+1)
*/
for (int dx = -1; dx <= 1; dx++)
for (int dy = -1; dy <= 1; dy++)
// make sure we don't count the square we're checking around
if (x+dx != x || y+dy != y)
// count alive squares
if (get(x+dx, y+dy, b) == 1)
n++;
return n;
}
void setNext(int x, int y, int state, board* b) {
b->nextCells[y][x] = state;
}
void applyRules(int x, int y, board* b) {
int n = countNeighbors(x, y, b);
// Any live cell
if (get(x, y, b) == 1) {
if (n < 2) // with fewer than two live neighbours
// dies, as if by underpopulation.
setNext(x, y, 0, b);
else if (n > 3) // with more than three live neighbours
// dies, as if by overpopulation.
setNext(x, y, 0, b);
else // with two or three live neighbours
// lives on to the next generation.
setNext(x, y, 1, b);
} else {
// Any dead cell with exactly three live neighbours
// becomes a live cell, as if by reproduction.
if (n == 3)
setNext(x, y, 1, b);
else
setNext(x, y, 0, b);
}
}
void tick(board* b) {
// generate next frame
for (int y = 0; y < b->height; y++)
for (int x = 0; x < b->width; x++)
applyRules(x, y, b);
// copy next frame to current frame
for (int y = 0; y < b->height; y++)
for (int x = 0; x < b->width; x++)
b->cells[y][x] = b->nextCells[y][x];
}
int main() {
/* Initialize board */
board b = {WIDTH, HEIGHT};
for (int y = 0; y < b.height; y++)
for (int x = 0; x < b.width; x++)
b.cells[y][x] = 0;
for (int y = 0; y < b.height; y++)
for (int x = 0; x < b.width; x++)
b.nextCells[y][x] = 0;
// a glider:
b.cells[5][5] = 1;
b.cells[6][6] = 1;
b.cells[7][6] = 1;
b.cells[7][5] = 1;
b.cells[7][4] = 1;
show(&b);
struct timeval stop, start;
for (;;) {
gettimeofday(&start, NULL);
tick(&b);
gettimeofday(&stop, NULL);
show(&b);
printf("frame generated in %lu us\n", (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec);
sleep_ms(100);
}
}