/* "LIFE" The game invented by John Conway This version written by Leor Zolman to exemplify PROPER use of"goto" statements in C programs! Note that the Universe is a toroid; The left extreme is adjacent to the right extreme, the top is adjacent to the bottom, and each corner is adjacent to each other corner. In other words, there ARE NO EXTREMES !! Or, in a more physical illustration: If you could look straight ahead through an infinitely powerful telescope, you'd eventually see the back of your head.... */ #define CLEARS 0x1a /* character to clear your screen */ /* If no single character does this on your terminal, make this a newline ('\n') */ #define TWIDTH 64 /* # of columns on your terminal */ #define XSIZE 60 /* length of cell array */ #define YSIZE 65 /* width of cell array */ /* To see how the toroid works, try reducing XSIZE and YSIZE to around 10 or 20. */ #define BOREDOM_THRESHOLD 5 /* This is how many generations are allowed to pass without a population change */ char *gets(); /* routine to accept a line of input */ char cell[XSIZE][YSIZE]; /* the universe matrix */ int minx, maxx, miny, maxy, pop, gen; /* misc. variables */ char doneflag; /* This goes true when we want to stop */ int boring; /* keeps count of how many generations go by without a population change */ char center; /* 1 = center display; 0 = left justify */ main() { char c; printf("\n\t *** BDS Life ***\n"); printf("\nDo you wish to have the display centered\n"); printf(" (and thereby slowed down a bit) (y/n) ?"); center = toupper(getchar()) == 'Y'; for (;;) { clear(); setup(); if (!pop) break; adjust(); display(); while (pop) { adjust(); dogen(); display(); if (boring == BOREDOM_THRESHOLD) { boring++; printf("\nNo change in population for"); printf(" %d ",BOREDOM_THRESHOLD); printf("generations. Abort (y/n) "); c = toupper(getchar()); putchar('\n'); if (c == 'Y') break; } if (kbhit()) {getchar(); break;} if (doneflag) break; } } } /* initialize the cell matrix to all dead */ clear() { setmem(cell,(XSIZE*YSIZE),0); } /* get initial set-up from user */ setup() { char c,y; char string[YSIZE], *ptr; y = pop = gen = minx = maxx = miny= maxy = 0; boring = 0; printf("\nEnter initial configuration (null line to end):\n"); while (*gets(string)) { ptr = string; while (*ptr) { if ( *ptr++ != ' ') { cell[maxx][y] = 10; ++pop; } ++y; if (y==YSIZE) { printf("Truncated to %d chars\n", YSIZE); break; } } --y; ++maxx; if (y>maxy) maxy = y; if (maxx==XSIZE) break; y = 0; } --maxx; } /* display the current generation */ display() { int i,j,k,l,j9; char c; if (!pop) { printf("\nLife ends at %d\n",gen); return; } if(minx && prow(minx-1)) minx--; if(miny && pcol(miny-1)) miny--; if ((maxx < (XSIZE-1)) && prow(maxx+1)) maxx++; if((maxy<(YSIZE-1)) && pcol(maxy+1))maxy++; while (!prow(minx)) minx++; while (!prow(maxx)) maxx--; while (!pcol(miny)) miny++; while (!pcol(maxy)) maxy--; putchar(CLEARS); if (center) { i = (TWIDTH-33)/2; for (j = 0; j(XSIZE-3) || miny<2 || maxy>(YSIZE-3)) ; i2 = (maxx==(XSIZE-1)) ? maxx : maxx+1; j2 = (maxy==(YSIZE-1)) ? maxy : maxy+1; for (pass = 0; pass < 2; pass++) for (i=minx ? minx-1 : minx; i<=i2; ++i) for (j=miny ? miny-1 : miny; j<=j2; ++j) { c = cell[i][j]; if (!pass) { if (c >= 10) if (bigflag) for (k = -1; k <= 1; k++) for (l = -1; l <= 1; l++) cell[mod(i+k,XSIZE)][mod(j+l,YSIZE)]++; else for (k = -1; k<=1; k++) for (l = -1; l <= 1; l++) cell[i+k][j+l]++; } else if (c > 10) if (c < 13 || c > 14) { cell[i][j] = 0; pop--; doneflag = 0; } else cell[i][j] = 10; else if (c == 3) { cell[i][j] = 10; pop++; doneflag = 0; } else cell[i][j] = 0; } if (pop == oldpop) boring++; else boring = 0; } int mod(a,b) { if (a<0) return b+a; if (a XSIZE-2) return; if (minx==0) { delta = (XSIZE-maxx)/2+maxx; savdelta = delta; for (i=maxx; i >= 0; --i) { for (j=miny; j<=maxy; ++j) { cell[delta][j] = cell[i][j]; cell[i][j] = 0; } --delta; } minx = delta+1; maxx = savdelta; } if (maxx == (XSIZE-1)) { delta = minx/2; savdelta = delta; for (i=minx; i YSIZE -2) return; if (miny == 0) { delta = (YSIZE-maxy)/2+maxy; savdelta = delta; for (i=maxy; i>=0; --i) { for (j=minx; j<=maxx; ++j) { cell[j][delta] = cell[j][i]; cell[j][i] = 0; } --delta; } miny = delta+1; maxy = savdelta; } if (maxy == (YSIZE-1)) { delta = miny/2; savdelta = delta; for (i=miny; i