-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGOL.C
146 lines (124 loc) · 2.33 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
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 30
#define COLS 40
#define DENSITY 0.35
char cells[ROWS][COLS];
char newstate[ROWS][COLS];
int width, height;
void initboard();
void drawcell(int x, int y, char alive);
char isalive(int x, int y);
void tick();
int main()
{
initboard();
/* todo handle keyboard interrupts */
while (1)
{
tick();
}
closegraph();
return 0;
}
void initboard()
{
int gd = DETECT, gm, errorcode;
int i, j;
initgraph(&gd, &gm, "C:\\TC");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
getch();
free(cells);
exit(1);
}
width = (getmaxx() + 1) / COLS;
height = (getmaxy() + 1) / ROWS;
srand(time(NULL));
setfillstyle(SOLID_FILL, 0xF);
bar(0, 0, getmaxx(), getmaxy());
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
cells[i][j] = (double)rand() / RAND_MAX < DENSITY;
if (cells[i][j] > 0)
{
drawcell(j, i, cells[i][j]);
}
}
}
}
void tick()
{
int i, j, alive = 0;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
alive = isalive(j, i);
newstate[i][j] = alive;
}
}
/* todo use dynamic allocation to avoid iterating again*/
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
if (cells[i][j] != newstate[i][j])
{
drawcell(j, i, newstate[i][j]);
cells[i][j] = newstate[i][j];
}
}
}
}
char isalive(int x, int y)
{
int neighbors = 0;
int i, j;
for (i = y - 1; i <= y + 1; i++)
{
if (i < 0 || i >= ROWS)
continue;
for (j = x - 1; j <= x + 1; j++)
{
if (j < 0 || j >= COLS || (i == y && j == x))
continue;
if (cells[i][j] > 0)
{
neighbors++;
if (neighbors > 3)
return 0;
}
}
}
if (neighbors < 2 || neighbors > 3)
return 0;
if (cells[y][x] > 0)
return 1;
if (neighbors == 3)
return 1;
return 0;
}
void drawcell(int x, int y, char alive)
{
int left = x * width;
int top = y * height;
int right = left + width;
int bottom = top + height;
if (alive > 0)
{
setfillstyle(SOLID_FILL, 1);
}
else
{
setfillstyle(SOLID_FILL, 0xF);
}
bar3d(left, top, right, bottom, 0, 0);
}