/* * Copyright (c) Camden Dixie O'Brien * SPDX-License-Identifier: AGPL-3.0-only */ #include "puzz.h" #include #include #include #include #include #include #include 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]; } } } static void getadj(puzz_t field, int *x, int *y, uint8_t val) { 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; if (field[xp][yp] == val) { *x = xp; *y = yp; return; } } } assert(false); } int main(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)); bool solved = false; do { int x = rand() % WIDTH; int y = rand() % HEIGHT; probe: if (probe(x, y, field) == DEAD) { field[x][y] = KILLER; break; } update: 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 (unknowns == 0) continue; if (mines + unknowns == field[x][y]) { setadj(field, x, y, UNKNOWN, MINE); goto update; } if (mines == field[x][y]) { getadj(field, &x, &y, UNKNOWN); goto probe; } } } solved = true; for (y = 0; y < HEIGHT; ++y) { for (x = 0; x < WIDTH; ++x) solved &= field[x][y] != UNKNOWN; } } while (!solved); puts(solved ? "Solved" : "Dead"); 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 0; }