53 lines
967 B
C
53 lines
967 B
C
/*
|
|
* Copyright (c) Camden Dixie O'Brien
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
#include "puzz.h"
|
|
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static puzz_t soln;
|
|
|
|
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;
|
|
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)
|
|
continue;
|
|
soln[x][y] += soln[xp][yp] == MINE ? 1 : 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void print(void)
|
|
{
|
|
puts("Solution:");
|
|
for (int y = 0; y < HEIGHT; ++y) {
|
|
for (int x = 0; x < WIDTH; ++x)
|
|
putchar(soln[x][y] == MINE ? 'x' : '0' + soln[x][y]);
|
|
putchar('\n');
|
|
}
|
|
putchar('\n');
|
|
}
|