Run 1000 times and measure success rate

This commit is contained in:
Camden Dixie O'Brien 2025-03-06 22:39:42 +00:00
parent 027fea745d
commit 7f2a9b79ae
2 changed files with 24 additions and 36 deletions

58
main.c
View File

@ -13,6 +13,8 @@
#include <string.h> #include <string.h>
#include <sys/time.h> #include <sys/time.h>
#define NRUNS 1000
enum { UNKNOWN = 0xfe, KILLER = 0xfd }; enum { UNKNOWN = 0xfe, KILLER = 0xfd };
static void setadj(puzz_t field, int x, int y, uint8_t from, uint8_t to) 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); 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; puzz_t field;
memset(field, UNKNOWN, sizeof(field)); memset(field, UNKNOWN, sizeof(field));
status_t status;
int total_mines = 0, total_unknowns = WIDTH * HEIGHT; int total_mines = 0, total_unknowns = WIDTH * HEIGHT;
do { do {
int x = rand() % WIDTH; int x = rand() % WIDTH;
int y = rand() % HEIGHT; int y = rand() % HEIGHT;
probe: 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; field[x][y] = KILLER;
break; break;
} }
@ -100,30 +93,27 @@ int main(void)
} }
} }
} while (total_mines < NMINES); } while (total_mines < NMINES);
puts(total_mines == NMINES ? "Solved" : "Died");
putchar('\n'); return status;
for (int y = 0; y < HEIGHT; ++y) { }
for (int x = 0; x < WIDTH; ++x) {
char c; int main(void)
switch (field[x][y]) { {
case UNKNOWN: struct timeval tv;
c = '?'; if (gettimeofday(&tv, NULL) != 0) {
break; perror("Failed to get time");
case KILLER: exit(1);
c = '!';
break;
case MINE:
c = 'x';
break;
default:
c = '0' + field[x][y];
break;
}
putchar(c);
}
putchar('\n');
} }
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; return 0;
} }

2
puzz.c
View File

@ -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) status_t probe(int x, int y, puzz_t out)
{ {
printf("Probing: %d,%d\n", x, y);
assert(x >= 0 && x < WIDTH); assert(x >= 0 && x < WIDTH);
assert(y >= 0 && y < HEIGHT); assert(y >= 0 && y < HEIGHT);