#include #define MaxSIZE 1000 double drand48(); int rules[8]; int width; int curr[MaxSIZE+2]; int next[MaxSIZE+2]; /***********************************/ void fill_in_rule(int rule) { int i; unsigned int foo = rule; for( i=0; i<8; i++ ) { rules[i] = foo & 1; foo = foo >> 1; } } /***********************************/ void make_periodic() { curr[0] = curr[width]; curr[width+1] = curr[1]; } /***********************************/ void initialize(int type) { int i; if( type == 0 ) { for( i=1; i<=width; i++ ) curr[i] = (drand48() > .5); } make_periodic(); } /***********************************/ void update() { int i; unsigned int state; for( i=1; i<=width; i++ ) { state=0; /* this is arranged so we read the state from left to right, i.e. 110 is 3 */ if( curr[i-1] == 1 ) state = 4; if( curr[i] == 1 ) state += 2; if( curr[i+1] == 1 ) state += 1; next[i] = rules[state]; } for( i=1; i<=width; i++ ) curr[i] = next[i]; make_periodic(); } /****************************************/ void display_state() { int i; for( i=1; i<=width; i++ ) { if( curr[i] == 1 ) printf("@"); else printf(" "); } printf("\n"); } /****************************************/ int main( int argc, char **argv) { int i,j,k; int nsteps = 20; char c='a'; int rule = 34; int randomstart=1; int seed=934; int sleeptime=0; width = 80; if( argc < 2 ) { fprintf(stderr,"Syntax: $s rule [nsteps [seed [width] ]] \n",argv[0]); fprintf(stderr,"rule: one of the CA rules\n"); fprintf(stderr,"nsteps: how many steps to run\n"); fprintf(stderr,"seed: random seed.\n"); fprintf(stderr,"width: number of elements (less than MaxSIZE).\n"); exit(0); } rule = atoi(argv[1]); fill_in_rule(rule); for( i=7;i>=0;i-- ) printf("%d",rules[i]); printf("\n"); if( argc > 2 ) nsteps = atoi(argv[2]); if( nsteps == 0 ) nsteps = 20; if( argc > 3 ) seed = atoi(argv[3]); if( seed == 0 ) seed = 983; if( argc > 4 ) width = atoi(argv[4]); if( width == 0 ) width = 80; if( width > MaxSIZE ) width = MaxSIZE; srand48(seed); /* seed the random number generator */ initialize(0); for( i=0; i