2 * life - john conways's game of life (and variations),
3 * sci. am. 223, october 1970, pp. 120—123, or
4 * http://en.wikipedia.org/wiki/Conway's_Game_of_Life
13 NLIFE = 256, /* life array size */
14 PX = 4, /* cell spacing */
15 BX = PX - 1, /* box size */
16 NADJUST = NLIFE * NLIFE,
20 * life[i][j] stores L+2*N, where L is 0 if the cell is dead and 1
21 * if alive, and N is the number of the cell's 8-connected neighbours
23 * row[i] indicates how many cells are alive in life[i][*].
24 * col[j] indicates how many cells are alive in life[*][j].
25 * Adjust contains pointers to cells that need to have their neighbour
26 * counts adjusted in the second pass of the generation procedure.
28 char life[NLIFE][NLIFE];
31 char action[18]; /* index by cell contents to find action */
32 char *adjust[NADJUST];
40 void centerlife(void);
43 int interest(int [NLIFE], int);
44 void main(int, char *[]);
46 void readlife(char *);
48 void setrules(char *);
51 static void reshape(void);
58 loc = Pt(cen.x + j*PX, cen.y + i*PX);
59 draw(screen, Rpt(loc, addpt(loc, Pt(BX, BX))), box, nil, ZP);
67 loc = Pt(cen.x + j*PX, cen.y + i*PX);
68 draw(screen, Rpt(loc, addpt(loc, Pt(BX, BX))), display->white, nil, ZP);
76 for (a = action; a != &action[nelem(action)]; *a++ = *r++)
81 g9err(Display *, char *err)
83 static int entered = 0;
85 fprint(2, "%s: %s (%r)\n", argv0, err);
92 fprint(2, "Usage: %s [-3o] [-r rules] file\n", argv0);
102 emouse(); /* throw away mouse events */
104 if ((c = ekbd()) == 'q' || c == 0177) /* watch keyboard ones */
111 main(int argc, char *argv[])
115 setrules(".d.d..b..d.d.d.d.d"); /* regular rules */
118 setrules(".d.d.db.b..d.d.d.d");
121 setrules(".d.d.db.b.b..d.d.d");
122 break; /* lineosc? */
123 case 'r': /* rules from cmdline */
124 setrules(EARGF(usage()));
132 initdraw(g9err, 0, argv0);
133 einit(Emouse|Ekeyboard); /* implies rawon() */
135 cen = divpt(subpt(addpt(screen->r.min, screen->r.max),
136 Pt(NLIFE * PX, NLIFE * PX)), 2);
137 box = allocimage(display, Rect(0, 0, BX, BX), RGB24, 1, DBlack);
143 flushimage(display, 1);
147 } while (generate());
152 * We can only have interest in a given row (or column) if there
153 * is something alive in it or in the neighbouring rows (or columns.)
156 interest(int rc[NLIFE], int i)
158 return(rc[i-1] != 0 || rc[i] != 0 || rc[i+1] != 0);
162 * A life generation proceeds in two passes. The first pass identifies
163 * cells that have births or deaths. The `alive bit' is updated, as are
164 * the screen and the row/col count deltas. Also, a record is made
165 * of the cell's address. In the second pass, the neighbours of all changed
166 * cells get their neighbour counts updated, and the row/col deltas get
167 * merged into the row/col count arrays.
169 * The border cells (i==0 || i==NLIFE-1 || j==0 || j==NLIFE-1) are not
170 * processed, purely for speed reasons. With a little effort, a wrap-around
171 * universe could be implemented.
173 * Generate returns 0 if there was no change from the last generation,
174 * and 1 if there were changes.
176 #define neighbour(di, dj, op) lp[(di)*NLIFE+(dj)] op= 2
177 #define neighbours(op)\
178 neighbour(-1, -1, op);\
179 neighbour(-1, 0, op);\
180 neighbour(-1, 1, op);\
181 neighbour( 0, -1, op);\
182 neighbour( 0, 1, op);\
183 neighbour( 1, -1, op);\
184 neighbour( 1, 0, op);\
191 char **p, **addp, **delp;
192 int i, j, j0 = NLIFE, j1 = -1;
193 int drow[NLIFE], dcol[NLIFE];
195 for (j = 1; j != NLIFE - 1; j++) {
196 drow[j] = dcol[j] = 0;
197 if (interest(col, j)) {
205 delp = &adjust[NADJUST];
206 for (i = 1; i != NLIFE - 1; i++)
207 if (interest(row, i)) {
208 for (j = j0, lp = &life[i][j0]; j <= j1; j++, lp++)
209 switch (action[*lp]) {
226 if (addp == adjust && delp == &adjust[NADJUST])
229 sysfatal("Out of space (delp < addp)");
236 while (p != &adjust[NADJUST]) {
240 for (i = 1; i != NLIFE - 1; i++) {
244 if (row[1] || row[NLIFE-2] || col[1] || col[NLIFE-2])
250 * Record a birth at (i,j).
257 if (i < 1 || NLIFE - 1 <= i || j < 1 || NLIFE - 1 <= j ||
269 * Record a death at (i,j)
276 if (i < 1 || NLIFE - 1 <= i || j < 1 || NLIFE - 1 <= j ||
288 readlife(char *filename)
294 if ((bp = Bopen(filename, OREAD)) == nil) {
295 snprint(name, sizeof name, "/sys/games/lib/life/%s", filename);
296 if ((bp = Bopen(name, OREAD)) == nil)
297 sysfatal("can't read %s: %r", name);
299 draw(screen, screen->r, display->white, nil, ZP);
300 for (i = 0; i != NLIFE; i++) {
302 for (j = 0; j != NLIFE; j++)
306 for (i = 1; i != NLIFE - 1 && c >= 0; i++) {
308 while ((c = Bgetc(bp)) >= 0 && c != '\n')
327 return(a < b ? a : b);
333 int i, j, di, dj, iinc, jinc, t;
336 di = NLIFE / 2 - (i0 + i1) / 2;
339 else if (i1 + di >= NLIFE - 1)
341 dj = NLIFE / 2 - (j0 + j1) / 2;
344 else if (j1 + dj >= NLIFE - 1)
346 if (di != 0 || dj != 0) {
361 for (i = i0; i * iinc <= i1 * iinc; i += iinc)
362 for (j = j0; j * jinc <= j1 * jinc; j += jinc)
363 if (life[i][j] & 1) {
364 birth(i + di, j + dj);
376 draw(screen, screen->r, display->white, nil, ZP);
377 for (i = i0; i <= i1; i++)
378 for (j = j0; j <= j1; j++)
386 for (i0 = 1; i0 != NLIFE - 2 && row[i0] == 0; i0++)
388 for (i1 = NLIFE - 2; i1 != i0 && row[i1] == 0; --i1)
390 for (j0 = 1; j0 != NLIFE - 2 && col[j0] == 0; j0++)
392 for (j1 = NLIFE - 2; j1 != j0 && col[j1] == 0; --j1)
402 // sqwid = Dx(screen->r) / (1 + bdp->cols + 1);
403 // dy12 = Dy(screen->r) / (1 + bdp->rows + 1 + 2);
406 // recompute(bdp, sqwid);
410 cen = divpt(subpt(addpt(screen->r.min, screen->r.max),
411 Pt(NLIFE * PX, NLIFE * PX)), 2);
413 flushimage(display, 1);
416 /* called from graphics library */
418 eresized(int callgetwin)
420 needresize = callgetwin + 1;
423 /* was using Refmesg */
424 if (needresize > 1 && getwindow(display, Refnone) < 0)
425 sysfatal("can't reattach to window: %r");
427 /* destroyed window? */
428 if (Dx(screen->r) == 0 || Dy(screen->r) == 0)
429 exits("window gone");