-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
69 lines (59 loc) · 1.61 KB
/
test.c
File metadata and controls
69 lines (59 loc) · 1.61 KB
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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "cgl.h"
extern bool **cells;
extern unsigned int size;
#define ALIVE_CHAR 'X'
#define DEAD_CHAR '-'
#define H_BORDER_CHAR '='
#define V_BORDER_CHAR '|'
// Stop after a set number of generations (or -1 to run forever)
#define MAX_GENERATIONS -1
// Enable/disable a delay between generations
#define DELAY 1
useconds_t delay = 100000; // 0.1 sec
void print_cells(bool **c, unsigned int s);
void print_horizontal_border(unsigned int s);
int main(int argc, char **argv) {
srand(time(NULL));
cells = init_cells();
randomize_cells(cells);
unsigned int generation_count = 0;
while (generation_count <= MAX_GENERATIONS) {
loop();
print_cells(cells, size);
if (DELAY) usleep(delay);
++generation_count;
}
free_cells(cells);
return 0;
}
void print_cells(bool **c, unsigned int s) {
print_horizontal_border(s);
for (unsigned int row = 0; row < s; ++row) {
printf("%c", V_BORDER_CHAR);
for (unsigned int col = 0; col < s; ++col) {
bool alive = c[row][col];
if (alive) {
printf("%c", ALIVE_CHAR);
} else {
printf("%c", DEAD_CHAR);
}
if (col < s - 1) {
printf(" ");
}
}
printf("%c\n", V_BORDER_CHAR);
}
print_horizontal_border(s);
}
void print_horizontal_border(unsigned int s) {
printf(" ");
for (unsigned int i = 0; i < (s * 2) - 1; ++i) {
printf("%c", H_BORDER_CHAR);
}
printf("\n");
}