-
Notifications
You must be signed in to change notification settings - Fork 1
/
life.c
342 lines (310 loc) · 7.32 KB
/
life.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* Copyright (C) 1990-2018 Sean MacLennan <[email protected]> */
#include "z.h"
/** @addtogroup zedit
* @{
*/
#define SROWS (Rowmax - 2) /* Screen rows */
#define SCOLS (Colmax - 1) /* Screen columns */
#define MATRIX (SROWS * SCOLS) /* Matrix buffer size */
#define NW (-1 - SCOLS) /* Directional constants, within */
#define N (-SCOLS) /* matrix 2. For example, NW */
#define NE (1-SCOLS) /* refers to the upper, left- */
#define E (1) /* hand neighbour */
#define SE (1+SCOLS)
#define S (SCOLS)
#define SW (-1+SCOLS)
#define W (-1)
#define LIFECH '*'
#define EMPTY ' '
#define LIVING '+'
#define DEAD 'X'
#define invalidate_row(row) invalidate_scrnmarks(row, row + 1)
/* Do one generation of life. First matrix 2 is cleared, then
* matrix 1 is scanned. Wherever a living cell is found, the CORRESPONDING
* NEIGHBOR CELLS IN MATRIX 2 are incremented by 1, and the corresponding
* cell itself is incremented by 100. If the cell is not living, do nothing.
* This provides a fast method of determining neighbor count, which is
* kept track of in matrix 2.
*
* The "zone" of each cell is checked, and used as a guide for determining
* neighbors. Nothern neighbors of northernmost row are found in the
* southernmost row, so that the game has a "boundless" effect...formations
* that move off one side automatically circle around to the other side.
*
* Pass 2 is called to determine what actually lives or dies, based on
* the neighbor-count of each cell.
*/
static void generation(char *matrix1, char *matrix2)
{
char *p1, *p2, *end;
int msize, row = 0, col;
/* Initialization */
msize = MATRIX;
end = matrix1 + msize;
memset(matrix2, 0, msize);
for (p1 = matrix1, p2 = matrix2; p1 < end; ++p1, ++p2) {
/* If matrix 1 cell is alive . . . */
if (*p1 > 100) {
/* Update matrix 2 cell. */
*p2 += 100;
/* Get the zone and update the neighbors accordingly. */
switch (*p1 - 100) {
case 1:
++*(p2 + NW);
++*(p2 + N);
++*(p2 + NE);
++*(p2 + E);
++*(p2 + SE);
++*(p2 + S);
++*(p2 + SW);
++*(p2 + W);
break;
case 2:
++*(p2 + NW + msize);
++*(p2 + N + msize);
++*(p2 + NE + msize);
++*(p2 + E);
++*(p2 + SE);
++*(p2 + S);
++*(p2 + SW);
++*(p2 + W);
break;
case 3:
++*(p2 + NW);
++*(p2 + N);
++*(p2 + NE);
++*(p2 + E);
++*(p2 + SE - msize);
++*(p2 + S - msize);
++*(p2 + SW - msize);
++*(p2 + W);
break;
case 4:
++*(p2 + NW + SCOLS);
++*(p2 + N);
++*(p2 + NE);
++*(p2 + E);
++*(p2 + SE);
++*(p2 + S);
++*(p2 + SW + SCOLS);
++*(p2 + W + SCOLS);
break;
case 5:
++*(p2 + NW);
++*(p2 + N);
++*(p2 + NE - SCOLS);
++*(p2 + E - SCOLS);
++*(p2 + SE - SCOLS);
++*(p2 + S);
++*(p2 + SW);
++*(p2 + W);
break;
case 6:
++*(p2 + NW + msize + SCOLS);
++*(p2 + N + msize);
++*(p2 + NE + msize);
++*(p2 + E);
++*(p2 + SE);
++*(p2 + S);
++*(p2 + SW + SCOLS);
++*(p2 + W + SCOLS);
break;
case 7:
++*(p2 + NW + msize);
++*(p2 + N + msize);
++*(p2 + NE + msize - SCOLS);
++*(p2 + E - SCOLS);
++*(p2 + SE - SCOLS);
++*(p2 + S);
++*(p2 + SW);
++*(p2 + W);
break;
case 8:
++*(p2 + NW + SCOLS);
++*(p2 + N);
++*(p2 + NE);
++*(p2 + E);
++*(p2 + SE - msize);
++*(p2 + S - msize);
++*(p2 + SW + SCOLS - msize);
++*(p2 + W + SCOLS);
break;
case 9:
++*(p2 + NW);
++*(p2 + N);
++*(p2 + NE - SCOLS);
++*(p2 + E - SCOLS);
++*(p2 + SE - msize - SCOLS);
++*(p2 + S - msize);
++*(p2 + SW - msize);
++*(p2 + W);
break;
default:
break;
}
}
}
/* pass2 - Scan matrix 2 and update matrix 1 according to the following:
*
* Matrix 2 value Matrix 1 result
* -------------- ----------------------
* 3 Birth
* other < 100 No change
* 102, 103 No change
* other >= 100 Live cell becomes dead
*/
col = 0;
btostart(Bbuff);
p1 = matrix1;
p2 = matrix2;
while (p1 < end) {
if (*p2 == 3) { /* Birth */
*p1 += 100;
Buff() = LIFECH;
invalidate_row(row);
} else if (*p2 >= 100 && *p2 != 102 && *p2 != 103) { /* Death */
*p1 -= 100;
Buff() = EMPTY;
invalidate_row(row);
}
bmove1(Bbuff);
if (Buff() == NL) {
++row;
col = 0;
bmove1(Bbuff);
} else
++col;
++p1;
++p2;
}
}
/* Initializes the zones (1-9) of matrix 1.
*
* The "zones" are used by the LIFE algorithm to determine the method
* of calculating neighbors. Zones are pertinent to edges and corners:
*
* +-+--------------+-+
* |6| 2 |7|
* +-+--------------+-+
* | | | |
* |4| 1 |5|
* | | | |
* +-+--------------+-+
* |8| 3 |9|
* +-+--------------+-+
*
* Zones are recorded in matrix 1 for ease of computation. If a cell
* lives, then add 100 to flag cell's existence.
*/
static void init_matrix1(char *p1)
{
int row, col;
/* Initilialize row 1 to zones 6, 2, and 7. */
*p1++ = 6;
for (col = 2; col < SCOLS; col++)
*p1++ = 2;
*p1++ = 7;
/* Initialize center rows to zones 4, 1, and 5. */
for (row = 2; row < SROWS; row++) {
*p1++ = 4;
for (col = 2; col < SCOLS; ++col)
*p1++ = 1;
*p1++ = 5;
}
/* Initialize bottom row to zones 8, 3, and 9. */
*p1++ = 8;
for (col = 2; col < SCOLS; col++)
*p1++ = 3;
*p1++ = 9;
}
/* Go throught the current buffer and find any 'lives'. If a life is
* found, convert the buffer character to a '*' and fill in matrix1.
* While going though the buffer it is padded with spaces to fill
* SROWS * SCOLS
*/
static void fill_matrix1(char *p1)
{
bool eol;
int row = 0, col = 0;
btostart(Bbuff);
for (row = 0; row < SROWS; ++row) {
for (eol = false, col = 0; col < SCOLS; ++col, ++p1)
if (eol || bisend(Bbuff) || Buff() == NL) {
/* at end of buffer line */
binsert(Bbuff, EMPTY);
eol = true;
} else {
if (isspace(Buff()))
Buff() = EMPTY;
else {
*p1 += 100;
Buff() = LIFECH;
}
bmove1(Bbuff);
}
if (!bisend(Bbuff) && Buff() == NL)
bmove1(Bbuff);
else
binsert(Bbuff, NL);
}
}
void Zlife(void)
{
char *matrix1, *matrix2;
bool go = true, step = true;
unsigned int cmd;
struct zbuff *buff;
buff = zcreatebuff(LIFEBUFF, NULL);
if (buff == NULL) {
error("Unable to create life buffer.");
return;
}
if (buff != Curbuff) {
/* Copy the first SROWS lines to life buffer */
struct mark save;
int i;
bempty(buff->buff);
bmrktopnt(Bbuff, &save);
bpnttomrk(Bbuff, Sstart); /* screen start */
for (i = 0; i < SROWS + 1; ++i)
if (!bcsearch(Bbuff, NL))
break; /* EOB */
bcopyrgn(Sstart, buff->buff);
bpnttomrk(Bbuff, &save);
invalidate_scrnmarks(0, SROWS);
}
cswitchto(buff);
matrix1 = malloc(MATRIX);
matrix2 = malloc(MATRIX);
if (!matrix1 || !matrix2) {
free(matrix1);
free(matrix2);
error("Not enough memory.");
return;
}
putpaw("Setting up...");
init_matrix1(matrix1);
fill_matrix1(matrix1);
putpaw("The Game of Life");
while (go) {
btostart(Bbuff);
zrefresh();
if (step || tkbrdy()) {
cmd = tgetkb();
if (cmd == CR)
step = false;
else if (Keys[cmd] == ZABORT)
go = false;
else
step = true;
} else
tdelay(100);
if (go)
generation(matrix1, matrix2);
}
clrpaw();
free(matrix1);
free(matrix2);
Bbuff->bmodf = 0;
}
/* @} */