134 lines
2.0 KiB
C
134 lines
2.0 KiB
C
/*
|
|
* Copyright (c) Camden Dixie O'Brien
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
#include "puzz.h"
|
|
|
|
#include <assert.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
enum { NO = 0, YES = 1 };
|
|
|
|
static puzz_t soln;
|
|
static puzz_t scanned;
|
|
|
|
void gen(void)
|
|
{
|
|
memset(soln, 0, sizeof(soln));
|
|
|
|
for (int i = 0; i < NMINES; ++i) {
|
|
int x, y;
|
|
do {
|
|
x = rand() % WIDTH;
|
|
y = rand() % HEIGHT;
|
|
} while (soln[x][y] == MINE);
|
|
soln[x][y] = MINE;
|
|
}
|
|
|
|
for (int y = 0; y < HEIGHT; ++y) {
|
|
for (int x = 0; x < WIDTH; ++x) {
|
|
if (soln[x][y] == MINE)
|
|
continue;
|
|
soln[x][y] = countadj(soln, x, y, MINE);
|
|
}
|
|
}
|
|
}
|
|
|
|
void print(puzz_t puzz)
|
|
{
|
|
for (int y = 0; y < HEIGHT; ++y) {
|
|
for (int x = 0; x < WIDTH; ++x) {
|
|
char c;
|
|
switch (puzz[x][y]) {
|
|
case MINE:
|
|
c = 'x';
|
|
break;
|
|
case UNKNOWN:
|
|
c = '.';
|
|
break;
|
|
case SAFE:
|
|
c = 's';
|
|
break;
|
|
default:
|
|
c = '0' + puzz[x][y];
|
|
break;
|
|
}
|
|
putchar(c);
|
|
}
|
|
putchar('\n');
|
|
}
|
|
putchar('\n');
|
|
}
|
|
|
|
void printsoln(void)
|
|
{
|
|
puts("Solution:");
|
|
print(soln);
|
|
}
|
|
|
|
static void scan_copy(int x, int y, puzz_t out)
|
|
{
|
|
assert(x >= 0 && x < WIDTH);
|
|
assert(y >= 0 && y < HEIGHT);
|
|
|
|
out[x][y] = soln[x][y];
|
|
scanned[x][y] = YES;
|
|
|
|
if (soln[x][y] != 0)
|
|
return;
|
|
|
|
FORADJ(x, y, xi, yi)
|
|
{
|
|
if (scanned[xi][yi] == YES)
|
|
continue;
|
|
scan_copy(xi, yi, out);
|
|
}
|
|
}
|
|
|
|
status_t probe(int x, int y, puzz_t out)
|
|
{
|
|
assert(x >= 0 && x < WIDTH);
|
|
assert(y >= 0 && y < HEIGHT);
|
|
|
|
if (soln[x][y] == MINE)
|
|
return DEAD;
|
|
|
|
memset(scanned, NO, sizeof(scanned));
|
|
scan_copy(x, y, out);
|
|
|
|
return OK;
|
|
}
|
|
|
|
status_t check(puzz_t puzz)
|
|
{
|
|
for (int y = 0; y < HEIGHT; ++y) {
|
|
for (int x = 0; x < WIDTH; ++x) {
|
|
switch (puzz[x][y]) {
|
|
case UNKNOWN:
|
|
continue;
|
|
case SAFE:
|
|
if (soln[x][y] == MINE)
|
|
return INCORRECT;
|
|
break;
|
|
default:
|
|
if (puzz[x][y] != soln[x][y])
|
|
return INCORRECT;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return OK;
|
|
}
|
|
|
|
int countadj(puzz_t field, int x, int y, uint8_t val)
|
|
{
|
|
int n = 0;
|
|
FORADJ(x, y, xi, yi)
|
|
n += field[xi][yi] == val ? 1 : 0;
|
|
return n;
|
|
}
|