Implement puzzle generation

This commit is contained in:
2025-03-22 13:33:38 +00:00
parent bb63cc0a30
commit 0c0ccc2930
3 changed files with 97 additions and 0 deletions

50
puzz.c Normal file
View File

@@ -0,0 +1,50 @@
/*
* 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)
{
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');
}
}