/* defines CHEAT print values during the game */ #include #include #include #include #include #include #define MAX_DESC_LEN 128 #define RESERVOIR 8 struct Thing { char description[MAX_DESC_LEN+1]; int value; }; unsigned int sample(char* filename, unsigned int nofentries, struct Thing selected[nofentries]) { FILE* fp = fopen(filename, "r"); if (!fp) return 0; const unsigned int buflen = MAX_DESC_LEN + 32; char buf[buflen]; unsigned int filled = 0; unsigned int number_of_records = 0; while (fgets(buf, buflen, fp)) { char* sep = 0; for (char* cp = buf; *cp && cp < buf + buflen; ++cp) { if (*cp == ':') { sep = cp; *sep = 0; break; } } if (!sep) continue; ++number_of_records; unsigned int index = filled; if (filled < nofentries) { ++filled; } else { index = rand() % number_of_records; } if (index < nofentries) { strncpy(selected[index].description, buf, MAX_DESC_LEN); selected[index].description[MAX_DESC_LEN] = 0; selected[index].value = atoi(sep + 1); } } fclose(fp); return filled; } unsigned int game(unsigned int nofentries, struct Thing things[nofentries]) { unsigned int chosen[nofentries]; /* chosen things in sorted order */ unsigned int selected = 0; /* number of chosen things */ unsigned int points = 0; bool taken[nofentries]; for (unsigned int i = 0; i < nofentries; ++i) { taken[i] = false; } while (selected < nofentries) { /* display current state */ printf("Current state:\n"); printf(" %2d:\n", 1); for (unsigned int i = 0; i < selected; ++i) { unsigned int index = chosen[i]; #ifdef CHEAT printf(" %4d %s\n", things[index].value, things[index].description); #else printf(" %s\n", things[index].description); #endif printf(" %2d:\n", i + 2); } printf("Still to be sorted:\n"); for (unsigned int i = 0; i < nofentries; ++i) { if (!taken[i]) { #ifdef CHEAT printf(" %2d: %4d %s\n", i + 1, things[i].value, things[i].description); #else printf(" %2d: %s\n", i + 1, things[i].description); #endif } } unsigned int what; unsigned int where; for(;;) { printf("What is to be inserted where? "); if (scanf("%u %u", &what, &where) != 2) { printf("Bad input."); return 0; } if (what-- == 0 || what >= nofentries || taken[what]) { printf("Bad value for what.\n"); continue; } if (where-- == 0 || where > selected) { printf("Bad value for where.\n"); continue; } break; } int value = things[what].value; if ((where > 0 && value < things[chosen[where-1]].value) || (where < selected && value > things[chosen[where]].value)) { printf("Sorry, then it is no longer sorted:\n"); for (int i = 0; i <= selected; ++i) { unsigned int index = chosen[i]; if (i == where) { printf("--> %4d %s\n", things[what].value, things[what].description); } if (i < selected) { printf(" %4d %s\n", things[index].value, things[index].description); } } printf("Bye!\n"); return points; } for (int i = selected; i > where; --i) { chosen[i] = chosen[i-1]; } chosen[where] = what; ++selected; ++points; taken[what] = true; } return points; } int main() { srand(getpid() ^ time(0)); struct Thing things[RESERVOIR]; unsigned int filled = sample("berge", RESERVOIR, things); unsigned int points = game(filled, things); printf("You got %d points.\n", points); /* for (unsigned int i = 0; i < filled; ++i) { printf("%4d %s\n", things[i].value, things[i].description); } */ }