chess-puzzles

chess puzzle book generator
git clone git://git.codemadness.org/chess-puzzles
Log | Files | Refs | README | LICENSE

commit 6af435b4416d90cb27e447e2f0bf505721374e12
parent b7a52ecde26555a060898faaa5643851368c7467
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Mon, 25 Dec 2023 18:49:58 +0100

add util functions for (x,y) to (file, rank) and vice versa

Diffstat:
Mfen.c | 46+++++++++++++++++++++++++++++++++++-----------
1 file changed, 35 insertions(+), 11 deletions(-)

diff --git a/fen.c b/fen.c @@ -97,12 +97,36 @@ getpiece(int x, int y) } int +xtofile(int c) +{ + return 'a' + c; +} + +int +ytorank(int c) +{ + return '8' - c; +} + +int +filetox(int c) +{ + return c - 'a'; +} + +int +ranktoy(int c) +{ + return '8' - c; +} + +int squaretoxy(const char *s, int *x, int *y) { if (*s >= 'a' && *s <= 'h' && *(s + 1) >= '1' && *(s + 1) <= '8') { - *x = *s - 'a'; - *y = '8' - *(s + 1); + *x = filetox(*s); + *y = ranktoy(*(s + 1)); return 1; } return 0; @@ -153,8 +177,8 @@ showboardfen(void) putchar(' '); if (enpassantsquare[0] != -1 && enpassantsquare[1] != -1) { - putchar('a' + enpassantsquare[0]); - putchar('8' - enpassantsquare[1]); + putchar(xtofile(enpassantsquare[0])); + putchar(ytorank(enpassantsquare[1])); } else { putchar('-'); } @@ -277,7 +301,7 @@ output_svg(void) color = highlight[y][x] ? lightsquarehi : lightsquare; printf("<text x=\"%d\" y=\"%d\" fill=\"#%02x%02x%02x\" text-anchor=\"end\" style=\"font-family: sans-serif; font-size: 10px\">%c</text>\n", - (ix + 1) * 45 - 2, (iy * 45) + 10, color[0], color[1], color[2], '8' - y); + (ix + 1) * 45 - 2, (iy * 45) + 10, color[0], color[1], color[2], ytorank(y)); } iy = 7; y = flipboard ? 0 : 7; @@ -298,7 +322,7 @@ output_svg(void) } printf("<text x=\"%d\" y=\"%d\" fill=\"#%02x%02x%02x\" text-anchor=\"start\" style=\"font-family: sans-serif; font-size: 10px\">%c</text>\n", - (ix * 45) + 2, (iy + 1) * 45 - 3, color[0], color[1], color[2], x + 'a'); + (ix * 45) + 2, (iy + 1) * 45 - 3, color[0], color[1], color[2], xtofile(x)); } } @@ -384,7 +408,7 @@ output_tty(void) SETBGCOLOR(color[0], color[1], color[2]); if (showcoords) { fputs("\x1b[97m", stdout); /* bright white */ - putchar('8' - y); + putchar(ytorank(y)); putchar(' '); } else { fputs(" ", stdout); @@ -455,7 +479,7 @@ output_ascii(void) } if (showcoords) { putchar(' '); - putchar('8' - y); + putchar(ytorank(y)); } putchar('\n'); } @@ -857,15 +881,15 @@ parsemoves(const char *moves) if (piece != 'p' && piece != 'P') { pgn("%c", toupper(piece)); /* TODO: check ambiguity for certain pieces and make the notation shorter */ - pgn("%c%c", 'a' + x, '8' - y); + pgn("%c%c", xtofile(x), ytorank(y)); } if (tookpiece) { /* pawn captures are prefixed by the file letter */ if (piece == 'p' || piece == 'P') - pgn("%c", 'a' + x); + pgn("%c", xtofile(x)); pgn("x"); } - pgn("%c%c", 'a' + x2, '8' - y2); + pgn("%c%c", xtofile(x2), ytorank(y2)); /* possible promotion: queen, knight, bishop */ if (*s == 'q' || *s == 'n' || *s == 'b') {