Identify mines on edges
This commit is contained in:
parent
c701669fc9
commit
7cdf4dcae6
27
main.c
27
main.c
@ -13,6 +13,18 @@
|
|||||||
|
|
||||||
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)
|
||||||
|
{
|
||||||
|
for (int yp = y - 1; yp < y + 2; ++yp) {
|
||||||
|
for (int xp = x - 1; xp < x + 2; ++xp) {
|
||||||
|
if (xp < 0 || xp >= WIDTH || yp < 0 || yp >= HEIGHT
|
||||||
|
|| (xp == x && yp == y))
|
||||||
|
continue;
|
||||||
|
field[xp][yp] = field[xp][yp] == from ? to : field[xp][yp];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
@ -28,11 +40,22 @@ int main(void)
|
|||||||
puzz_t field;
|
puzz_t field;
|
||||||
memset(field, UNKNOWN, sizeof(field));
|
memset(field, UNKNOWN, sizeof(field));
|
||||||
|
|
||||||
const int x = rand() % WIDTH;
|
int x = rand() % WIDTH;
|
||||||
const int y = rand() % HEIGHT;
|
int y = rand() % HEIGHT;
|
||||||
if (probe(x, y, field) == DEAD) {
|
if (probe(x, y, field) == DEAD) {
|
||||||
field[x][y] = KILLER;
|
field[x][y] = KILLER;
|
||||||
fprintf(stderr, "Dead\n");
|
fprintf(stderr, "Dead\n");
|
||||||
|
} else {
|
||||||
|
for (y = 0; y < HEIGHT; ++y) {
|
||||||
|
for (x = 0; x < WIDTH; ++x) {
|
||||||
|
if (field[x][y] == UNKNOWN || field[x][y] == MINE)
|
||||||
|
continue;
|
||||||
|
const int mines = countadj(field, x, y, MINE);
|
||||||
|
const int unknowns = countadj(field, x, y, UNKNOWN);
|
||||||
|
if (mines + unknowns == field[x][y])
|
||||||
|
setadj(field, x, y, UNKNOWN, MINE);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user