add LEN() macro and use it - chess-puzzles - chess puzzle book generator | |
git clone git://git.codemadness.org/chess-puzzles | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit f1daa80c1d6cf199c57d820e117255f38bd3d55e | |
parent 6af435b4416d90cb27e447e2f0bf505721374e12 | |
Author: Hiltjo Posthuma <[email protected]> | |
Date: Mon, 25 Dec 2023 18:54:16 +0100 | |
add LEN() macro and use it | |
Diffstat: | |
M fen.c | 17 +++++++++-------- | |
1 file changed, 9 insertions(+), 8 deletions(-) | |
--- | |
diff --git a/fen.c b/fen.c | |
@@ -9,6 +9,8 @@ | |
#include <unistd.h> | |
#endif | |
+#define LEN(s) (sizeof(s)/sizeof(*s)) | |
+ | |
/* macro for truecolor RGB output to tty */ | |
#define SETFGCOLOR(r,g,b) printf("\x1b[38;2;%d;%d;%dm", r, g, b) | |
#define SETBGCOLOR(r,g,b) printf("\x1b[48;2;%d;%d;%dm", r, g, b) | |
@@ -216,7 +218,7 @@ output_svg(void) | |
{ | |
const char *s; | |
char pieces[] = "pPKQRBNkqrbn"; /* pieces, check if they are used for … | |
- unsigned char pieceused[sizeof("pPKQRBNkqrbn")]; | |
+ unsigned char pieceused[LEN("pPKQRBNkqrbn")] = { 0 }; | |
const int *color; | |
int i, ix, iy, x, y, piece; | |
@@ -225,8 +227,7 @@ output_svg(void) | |
"<svg width=\"360\" height=\"360\" viewBox=\"0 0 360 360\" xml… | |
"<rect fill=\"#fff\" stroke=\"#000\" x=\"0\" y=\"0\" width=\"3… | |
- memset(pieceused, 0, sizeof(pieceused)); | |
- for (i = 0; i < sizeof(pieces) / sizeof(pieces[0]); i++) { | |
+ for (i = 0; i < LEN(pieces); i++) { | |
for (y = 0; y < 8 && !pieceused[i]; y++) { | |
for (x = 0; x < 8; x++) { | |
if (getpiece(x, y) == pieces[i]) { | |
@@ -238,7 +239,7 @@ output_svg(void) | |
} | |
fputs("<defs>\n", stdout); | |
- for (i = 0; i < sizeof(pieces) / sizeof(pieces[0]); i++) { | |
+ for (i = 0; i < LEN(pieces); i++) { | |
if (!pieceused[i]) | |
continue; | |
s = NULL; | |
@@ -533,7 +534,7 @@ ischecked(int side) | |
return 0; /* should not happen */ | |
/* check files and ranks (for queen and rook) */ | |
- for (j = 0; j < 8; j += 2) { | |
+ for (j = 0; j < LEN(line); j += 2) { | |
for (i = 1; i < 8; i ++) { | |
x = kingx + (i * line[j]); | |
y = kingy + (i * line[j + 1]); | |
@@ -551,8 +552,8 @@ ischecked(int side) | |
} | |
/* check diagonals (queen and bishop) */ | |
- for (j = 0; j < 8; j += 2) { | |
- for (i = 1; i < 8; i ++) { | |
+ for (j = 0; j < LEN(diag); j += 2) { | |
+ for (i = 1; i < 8; i++) { | |
x = kingx + (i * diag[j]); | |
y = kingy + (i * diag[j + 1]); | |
if (!(piece = getpiece(x, y))) | |
@@ -570,7 +571,7 @@ ischecked(int side) | |
/* check knights */ | |
piece = side == 'w' ? 'n' : 'N'; | |
- for (j = 0; j < 16; j += 2) { | |
+ for (j = 0; j < LEN(knight); j += 2) { | |
x = kingx + knight[j]; | |
y = kingy + knight[j + 1]; | |
// highlightmove(x, y); /* DEBUG */ |