Run 1000 times and measure success rate
This commit is contained in:
parent
027fea745d
commit
7f2a9b79ae
58
main.c
58
main.c
@ -13,6 +13,8 @@
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user