-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife-printf.c
91 lines (73 loc) · 1.63 KB
/
life-printf.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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#define DIM 25
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
bool board_0[DIM][DIM], board_1[DIM][DIM];
bool b = 0;
// get the value at (x, y) on the live board.
bool board(int i, int j)
{
return (b ? board_1[i][j] : board_0[i][j]);
}
// set (x, y) on the buffer board (i.e. the _not_ being shown) to n.
int set_board(int i, int j, bool n)
{
b ? (board_0[i][j] = n) : (board_1[i][j] = n);
}
// loop through a 3x3 square around (x, y) and count the live neighbours (that
// is, ignoring the point (x, y) itself).
int get_live_nbs(int x, int y)
{
int i, j, count = 0;
for (i = max(x-1, 0); i <= min(x+1, DIM-1); i++)
{
for (j = max(y-1, 0); j <= min(y+1, DIM-1); j++)
{
if ((i == x) && (j == y))
continue;
count += board(i, j);
}
}
return count;
}
int main(int argc, char **argv)
{
int i, j, live_nbs;
for (i = 0; i < DIM; i++)
for (j = 0; j < DIM; j++)
board_0[i][j] = board_1[i][j] = 0; // zero the boards
if (argc % 2 == 0)
{
printf("Need an even number of arguments, for (x, y) pairs.\n");
return 1;
}
b = !b;
for (i = 1; i < argc; i += 2)
set_board(atoi(argv[i]), atoi(argv[i + 1]), 1);
b = !b;
while (1)
{
printf("\n");
for (i = 0; i < DIM; i++)
{
printf("\n|");
for (j = 0; j < DIM; j++)
{
live_nbs = get_live_nbs(i, j);
if (live_nbs == 3)
set_board(i, j, 1);
else if (live_nbs != 2)
set_board(i, j, 0);
else
set_board(i, j, board(i, j));
printf("%c", ((board(i, j) == 1) ? '@' : ' '));
}
printf("| %d", i);
fflush(stdout);
}
b = !b;
usleep(100000);
}
}