From 7f2a9b79ae64d562625a9be3c966bb0be3a21423 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Thu, 6 Mar 2025 22:39:42 +0000 Subject: [PATCH] Run 1000 times and measure success rate --- main.c | 58 ++++++++++++++++++++++++---------------------------------- puzz.c | 2 -- 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/main.c b/main.c index 6f1f734..b6a63a4 100644 --- a/main.c +++ b/main.c @@ -13,6 +13,8 @@ #include #include +#define NRUNS 1000 + enum { UNKNOWN = 0xfe, KILLER = 0xfd }; static void setadj(puzz_t field, int x, int y, uint8_t from, uint8_t to) @@ -44,28 +46,19 @@ static void getadj(puzz_t field, int *x, int *y, uint8_t val) assert(false); } -int main(void) +static status_t solve(void) { - struct timeval tv; - if (gettimeofday(&tv, NULL) != 0) { - perror("Failed to get time"); - exit(1); - } - srand(tv.tv_usec); - - gen(); - print(); - puzz_t field; memset(field, UNKNOWN, sizeof(field)); + status_t status; int total_mines = 0, total_unknowns = WIDTH * HEIGHT; do { int x = rand() % WIDTH; int y = rand() % HEIGHT; probe: - if (field[x][y] != MINE && probe(x, y, field) == DEAD) { + if (field[x][y] != MINE && (status = probe(x, y, field)) == DEAD) { field[x][y] = KILLER; break; } @@ -100,30 +93,27 @@ int main(void) } } } while (total_mines < NMINES); - puts(total_mines == NMINES ? "Solved" : "Died"); - putchar('\n'); - for (int y = 0; y < HEIGHT; ++y) { - for (int x = 0; x < WIDTH; ++x) { - char c; - switch (field[x][y]) { - case UNKNOWN: - c = '?'; - break; - case KILLER: - c = '!'; - break; - case MINE: - c = 'x'; - break; - default: - c = '0' + field[x][y]; - break; - } - putchar(c); - } - putchar('\n'); + return status; +} + +int main(void) +{ + struct timeval tv; + if (gettimeofday(&tv, NULL) != 0) { + perror("Failed to get time"); + exit(1); } + srand(tv.tv_usec); + + int nsolved = 0; + for (int i = 0; i < NRUNS; ++i) { + gen(); + nsolved += solve() == OK ? 1 : 0; + } + + const double prop = (double)nsolved / NRUNS; + printf("Solved %d/%d (%0.1f%%)\n", nsolved, NRUNS, 100 * prop); return 0; } diff --git a/puzz.c b/puzz.c index 1778202..2f39c03 100644 --- a/puzz.c +++ b/puzz.c @@ -72,8 +72,6 @@ static void scan_copy(int x, int y, puzz_t out) status_t probe(int x, int y, puzz_t out) { - printf("Probing: %d,%d\n", x, y); - assert(x >= 0 && x < WIDTH); assert(y >= 0 && y < HEIGHT);