-
Notifications
You must be signed in to change notification settings - Fork 0
/
gol.c
159 lines (135 loc) · 3.32 KB
/
gol.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
147
148
149
150
151
152
153
154
155
#include <stdlib.h>
#include <stdio.h>
#define BOARDMAX 400
#define ALIVE 'X'
#define DEAD ' '
char grid[22][22];
/* struct to hold info for each cell.
* alive variable can be either be 1 to signify life or 0 to signify death. */
struct cell {
int x;
int y;
int alive;
} *cells[1000], *cp;
int cellp = 0;
/* generate pseudo-random num in range (min, max) */
int rand_range(unsigned int min, unsigned int max)
{
int base_random = rand();
if (RAND_MAX == base_random)
return rand_range(min, max);
int range = max - min,
remainder = RAND_MAX % range,
bucket = RAND_MAX / range;
if (base_random < RAND_MAX - remainder)
return min + base_random/bucket;
else
return rand_range(min, max);
}
void initcell(int x, int y, int alive)
{
cp = (struct cell *)malloc(sizeof(struct cell));
cp->x = x;
cp->y = y;
cp->alive = alive;
cells[cellp++] = cp;
}
void randomize_grid(void)
{
int x, y, lim, c;
for (x = 1; x < 21; ++x) {
lim = rand_range(1, 7);
for (c = 0; c < lim; ++c) {
y = rand_range(1, 20);
if (grid[x][y] != ALIVE) {
grid[x][y] = ALIVE;
initcell(x, y, 1);
}
}
}
for (x = 1; x < 21; ++x) {
for (y = 1; y < 21; ++y)
if (grid[x][y] != ALIVE) {
grid[x][y] = DEAD;
initcell(x, y, 0);
}
}
/* pad board with whitespace */
y = 0;
for (x = 0; x < 22; ++x)
grid[x][y] = DEAD;
y = 21;
for (x = 0; x < 22; ++x)
grid[x][y] = DEAD;
x = 0;
for (y = 0; y < 22; ++y)
grid[x][y] = DEAD;
x = 21;
for (y = 0; y < 22; ++y)
grid[x][y] = DEAD;
}
/* check the 8 adjacent cells for life and increment count if life exists.
* the coordinates of the 8 adjacent cells in relation to the passed
* x,y coordinates can be represented by the following list:
* [ [x+1, y+1], [x+1, y], [x+1, y-1], [x, y-1], [x, y+1], [x-1, y-1],
* [x-1, y+1], [x-1, y] ]
*/
int check_alive(struct cell *p)
{
int count, tempx, tempy;
count = 0;
int y = p->y;
tempx = p->x+1;
if (grid[tempx][y+1] == ALIVE)
count++;
if (grid[tempx][y] == ALIVE)
count++;
if (grid[tempx][y-1] == ALIVE)
count++;
tempx--;
if (grid[tempx][y-1] == ALIVE)
count++;
if (grid[tempx][y+1] == ALIVE)
count++;
tempx--;
if (grid[tempx][y-1] == ALIVE)
count++;
if (grid[tempx][y] == ALIVE)
count++;
if (grid[tempx][y+1] == ALIVE)
count++;
if (p->alive == 0 && count == 3)
return 1;
else if (p->alive == 1 && count == 3 || count == 2)
return 1;
else
return 0;
}
void scan_board(void)
{
int i;
for (i = 0; i < BOARDMAX; ++i)
cells[i]->alive = (check_alive(cells[i])) ? 1 : 0;
}
void print_board(void)
{
int i;
for (i = 0; i < BOARDMAX; ++i)
grid[cells[i]->x][cells[i]->y] = (cells[i]->alive) ? ALIVE : DEAD;
int x, y;
for (x = 0; x < 22; ++x) {
printf("\n");
for (y = 0; y < 22; ++y)
putchar(grid[x][y]);
}
}
int main(void)
{
int x, y;
randomize_grid();
while (1) {
scan_board();
print_board();
}
return 0;
}