#include #include #define M 10 /* Nbr of birds */ #define CSTAB 100 /* Criterium of stability */ #define DOVE 'D' /* Dove */ #define HAWK 'H' /* Hawk */ #define VULT 'V' /* Vulture */ main() { char birds[M+1]; /* Bird string e.g. "DDHHVHVHVD" */ int ibird; /* Attacking bird index */ int jbird; /* Defending bird index */ int t; /* Time */ int c; /* Stability criterium counter */ char buf[32]; /* Input buffer */ FILE *fp; /* Output file */ srand(time(0)); /* Set random seed */ fp = fopen("A.DAT","w"); /* Open output file */ while ( printf("Enter %d-char bird-string (use DHV): ",M), gets(buf) ) { /* Input loop, end with CTRL-Z */ sscanf(buf,"%s",birds); /* Initial bird string */ printf("%s",birds); fprintf(fp,"%s",birds); for (c = 0, t = 0; c < CSTAB; t++) { ibird = (int)((double)rand()/(double)INT_MAX*M); if (birds[ibird] != VULT) { c++; continue; /* Only vultures attack */ } jbird = (int)((double)rand()/(double)INT_MAX*M); switch (birds[jbird]) { case DOVE: /* Vulture wins */ birds[jbird] = VULT; c = 0; break; case HAWK: /* 50% vulture, 50% hawk */ if (rand() < INT_MAX/2) birds[jbird] = VULT; else birds[ibird] = HAWK; c = 0; break; case VULT: /* Nothing changes */ c++; break; } } printf(" ==> %s in %3d\n",birds,t-CSTAB); fprintf(fp," ==> %s in %3d\n",birds,t-CSTAB); } fclose(fp); }