#include #define XSIZE 20 /* vertical direction */ #define YSIZE 60 /* horizontal */ double drand48(); WINDOW *winm; int curr[XSIZE+2][YSIZE+2]; int next[XSIZE+2][YSIZE+2]; int refra = 5; /* refractory period */ int excited = 99; /* excited */ /********************************/ void make_periodic() { int i,j; /* make the boundary conditions periodic */ for( i=0; i .8 ) curr[i][j] = 1; } } else if(type==1) { for( i=XSIZE/2; i<=1+XSIZE/2; i++ ) for( j=YSIZE/2; j<=1+YSIZE/2; j++ ) { curr[i][j] = 1; } } else { /* do it yourself pattern */ starti = XSIZE/2; startj = YSIZE/2; i = starti; j = startj; while( *spattern != '\0' ) { if( *spattern == '0' ) curr[i][j++] = 0; else if( *spattern == '1' ) curr[i][j++] = 1; else if( *spattern == '-' ) { j = startj; i++; } spattern++; } } /* make the boundary conditions periodic */ make_periodic(); } /********************************/ void initialize_heart( int type ) { int i,j; double r; /* initialize the array */ for( i=0; i .95 ) curr[i][j] = excited; else if( r > .35 ) curr[i][j] = refra; } } /* make the boundary conditions periodic */ make_periodic(); } /********************************/ void update_heart() { int i,j,k; int state; for( i=1; i 0 ) next[i][j] = state - 1; else { /* check if any of the neighbors are excited */ if( curr[i][j+1] == excited ) next[i][j] = excited; if( curr[i][j-1] == excited ) next[i][j] = excited; if( curr[i+1][j] == excited ) next[i][j] = excited; if( curr[i-1][j] == excited ) next[i][j] = excited; } } for( i=1; i<=XSIZE; i++ ) for( j=1; j<= YSIZE; j++ ) curr[i][j] = next[i][j]; make_periodic(); } /********************************/ void draw_array_life() { int i,j; wclear(winm); for( i=1; i<=XSIZE; i++ ) for( j=1; j<=YSIZE; j++ ) { if( curr[i][j] ==1 ) waddch(winm,'@'); else waddch(winm,' '); } wrefresh(winm); /* printf("\007"); */ } /********************************/ void draw_array_heart() { int i,j; wclear(winm); for( i=1; i<=XSIZE; i++ ) for( j=1; j<=YSIZE; j++ ) { if( curr[i][j] == excited ) waddch(winm,'@'); else if( curr[i][j] > 0 ) { if( curr[i][j] < 3 ) waddch(winm,'.'); else waddch(winm,'+'); } else waddch( winm, ' ' ); } wrefresh(winm); /* printf("\007"); */ } /********************************/ void update_life() { int i,j,count,m,n; for( i=1; i<=XSIZE; i++ ) for( j=1; j<=YSIZE; j++ ) { count=0; if( curr[i+1][j] == 1 ) count++; if( curr[i-1][j] == 1 ) count++; if( curr[i][j+1] == 1 ) count++; if( curr[i][j-1] == 1 ) count++; if( curr[i+1][j+1] == 1 ) count++; if( curr[i-1][j-1] == 1 ) count++; if( curr[i-1][j+1] == 1 ) count++; if( curr[i+1][j-1] == 1 ) count++; if( curr[i][j] == 1 ) { if (count < 2 || count >= 4) next[i][j] = 0; /* dies if too crowded or lonely */ else next[i][j] = 1; /* stays alive */ } else { if( count == 3) next[i][j] = 1; /* born! */ } } for( i=1; i<=XSIZE; i++ ) for( j=1; j<= YSIZE; j++ ) curr[i][j] = next[i][j]; make_periodic(); } /********************************/ int main( int argc, char **argv) { int i,j,k; char c='a'; int dolife=1; int starttype=1; int seed=934; int sleeptime=0; char startpattern[1000]; char *goo; if( argc < 3 ) { fprintf(stderr,"Syntax: $s gametype starttype [seed [sleep]]\n",argv[0]); fprintf(stderr,"gametype: L=life, H=heart\n"); fprintf(stderr,"starttype: 0=random, 1=organized\n"); fprintf(stderr,"sleep will wait seconds between updates."); fprintf(stderr,"For LIFE, starttype can be a pattern like 100/101/111"); fprintf(stderr,"which will specify the initial condition near the center."); exit(0); } if( argv[1][0] == 'L' ) dolife=1; else dolife=0; /* do the heart */ if( argv[2][0] == '0' && argv[2][1] =='\0' ) starttype=0; else if( argv[2][0] == '1' && argv[2][1] == '\0' ) starttype=1; else { /* do it yourself pattern */ starttype=2; } if( argc > 3 ) seed = atoi(argv[3]); if( argc > 4 ) sleeptime = atoi(argv[4]); if( seed == 0 ) seed = 983; srand48(seed); /* seed the random number generator */ winm = initscr(); winm = newwin(XSIZE,YSIZE,0,0); if( dolife ) initialize_life(starttype,argv[2]); else initialize_heart(starttype); if( dolife ) draw_array_life(); else draw_array_heart(); sleep(sleeptime); for( i=0; i<1000; i++ ) { if( dolife) { update_life(); draw_array_life(); } else { update_heart(); draw_array_heart(); } sleep(sleeptime); } }