Make macro for looping through adjacent tiles
This commit is contained in:
parent
7156efc99c
commit
5b20a3e97b
24
main.c
24
main.c
@ -29,30 +29,20 @@ typedef struct {
|
|||||||
|
|
||||||
static void setadj(puzz_t field, int x, int y, uint8_t from, uint8_t to)
|
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) {
|
FORADJ(x, y, xi, yi)
|
||||||
for (int xp = x - 1; xp < x + 2; ++xp) {
|
field[xi][yi] = field[xi][yi] == from ? to : field[xi][yi];
|
||||||
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)
|
static void getadj(puzz_t field, int *x, int *y, uint8_t val)
|
||||||
{
|
{
|
||||||
for (int yp = *y - 1; yp < *y + 2; ++yp) {
|
FORADJ(*x, *y, xi, yi)
|
||||||
for (int xp = *x - 1; xp < *x + 2; ++xp) {
|
{
|
||||||
if (xp < 0 || xp >= WIDTH || yp < 0 || yp >= HEIGHT
|
if (field[xi][yi] == val) {
|
||||||
|| (xp == *x && yp == *y))
|
*x = xi;
|
||||||
continue;
|
*y = yi;
|
||||||
if (field[xp][yp] == val) {
|
|
||||||
*x = xp;
|
|
||||||
*y = yp;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
20
puzz.c
20
puzz.c
@ -78,13 +78,11 @@ static void scan_copy(int x, int y, puzz_t out)
|
|||||||
if (soln[x][y] != 0)
|
if (soln[x][y] != 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (int yp = y - 1; yp < y + 2; ++yp) {
|
FORADJ(x, y, xi, yi)
|
||||||
for (int xp = x - 1; xp < x + 2; ++xp) {
|
{
|
||||||
if (xp < 0 || xp >= WIDTH || yp < 0 || yp >= HEIGHT
|
if (scanned[xi][yi] == YES)
|
||||||
|| scanned[xp][yp] == YES)
|
|
||||||
continue;
|
continue;
|
||||||
scan_copy(xp, yp, out);
|
scan_copy(xi, yi, out);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,13 +124,7 @@ status_t check(puzz_t puzz)
|
|||||||
int countadj(puzz_t field, int x, int y, uint8_t val)
|
int countadj(puzz_t field, int x, int y, uint8_t val)
|
||||||
{
|
{
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (int yp = y - 1; yp < y + 2; ++yp) {
|
FORADJ(x, y, xi, yi)
|
||||||
for (int xp = x - 1; xp < x + 2; ++xp) {
|
n += field[xi][yi] == val ? 1 : 0;
|
||||||
if (xp < 0 || xp >= WIDTH || yp < 0 || yp >= HEIGHT
|
|
||||||
|| (xp == x && yp == y))
|
|
||||||
continue;
|
|
||||||
n += field[xp][yp] == val ? 1 : 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
8
puzz.h
8
puzz.h
@ -6,11 +6,17 @@
|
|||||||
#ifndef PUZZ_H
|
#ifndef PUZZ_H
|
||||||
#define PUZZ_H
|
#define PUZZ_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
#define WIDTH 9
|
#define WIDTH 9
|
||||||
#define HEIGHT 9
|
#define HEIGHT 9
|
||||||
#define NMINES 10
|
#define NMINES 10
|
||||||
|
|
||||||
#include <stdint.h>
|
#define FORADJ(X, Y, XI, YI) \
|
||||||
|
for (int YI = Y - 1; YI < Y + 2; ++YI) \
|
||||||
|
for (int XI = X - 1; XI < X + 2; ++XI) \
|
||||||
|
if (XI >= 0 && XI < WIDTH && YI >= 0 && YI < HEIGHT \
|
||||||
|
&& (XI != X || YI != Y))
|
||||||
|
|
||||||
enum { MINE = 0xff, UNKNOWN = 0xfe, SAFE = 0xfd };
|
enum { MINE = 0xff, UNKNOWN = 0xfe, SAFE = 0xfd };
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user