chess-puzzles

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

commit 3d921c8e0e2011ae994e508e89fc71086fffd598
parent 3c9f4a2be304b3f0d197d69bea715ee0c9e29570
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Wed, 20 Dec 2023 21:54:11 +0100

option to flip the board

Diffstat:
Mfen_to_svg.c | 115+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
1 file changed, 80 insertions(+), 35 deletions(-)

diff --git a/fen_to_svg.c b/fen_to_svg.c @@ -1,4 +1,4 @@ -/* TODO: option to flip board? */ +/* TODO: output for PGN notation for moves */ #include <ctype.h> #include <stdio.h> @@ -26,7 +26,8 @@ static const int lightsquare[] = { 0xf0, 0xd9, 0xb5 }; static const int darksquarehi[] = { 0xaa, 0xa2, 0x3a }; static const int lightsquarehi[] = { 0xcd, 0xd2, 0x6a }; -static const int showcoords = 1; /* config: show board coordinates? */ +static int showcoords = 1; /* config: show board coordinates? */ +static int flipboard = 0; /* config: flip board ? */ int isvalidsquare(int x, int y) @@ -159,7 +160,7 @@ void svg_showboard(void) { const int *color; - int x, y, piece; + int ix, iy, x, y, piece; fputs("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n" @@ -170,8 +171,12 @@ svg_showboard(void) showboardfen(); fputs(" -->\n", stdout); - for (y = 0; y < 8; y++) { - for (x = 0; x < 8; x++) { + for (iy = 0; iy < 8; iy++) { + y = flipboard ? 7 - iy : iy; + + for (ix = 0; ix < 8; ix++) { + x = flipboard ? 7 - ix : ix; + if (x % 2 == 0) { if (y % 2 == 0) color = highlight[y][x] ? lightsquarehi : lightsquare; @@ -185,11 +190,11 @@ svg_showboard(void) } printf("<g><rect x=\"%d\" y=\"%d\" width=\"45\" height=\"45\" fill=\"#%02x%02x%02x\"/></g>\n", - x * 45, y * 45, color[0], color[1], color[2]); + ix * 45, iy * 45, color[0], color[1], color[2]); piece = getpiece(x, y); if (piece) { - printf("<g transform=\"translate(%d %d)\">", x * 45, y * 45); + printf("<g transform=\"translate(%d %d)\">", ix * 45, iy * 45); svg_showpiece(piece); fputs("</g>\n", stdout); } @@ -197,23 +202,47 @@ svg_showboard(void) } if (showcoords) { - x = 7; - for (y = 0; y < 8; y++) { - if (y % 2 == 0) - color = highlight[y][x] ? lightsquarehi : lightsquare; - else - color = highlight[y][x] ? darksquarehi : darksquare; + ix = 7; + x = flipboard ? 0 : 7; + for (iy = 0; iy < 8; iy++) { + y = flipboard ? 7 - iy : iy; + + /* inverse square color for text */ + if (x % 2 == 0) { + if (y % 2 == 0) + color = highlight[y][x] ? darksquarehi : darksquare; + else + color = highlight[y][x] ? lightsquarehi : lightsquare; + } else { + if (y % 2 == 0) + color = highlight[y][x] ? lightsquarehi : lightsquare; + else + color = highlight[y][x] ? darksquarehi : darksquare; + } + printf("<text x=\"%d\" y=\"%d\" fill=\"#%02x%02x%02x\" text-anchor=\"end\" style=\"font-family: sans-serif; font-size: 10px\">%c</text>\n", - (x + 1) * 45 - 2, (y * 45) + 10, color[0], color[1], color[2], '8' - y); + (ix + 1) * 45 - 2, (iy * 45) + 10, color[0], color[1], color[2], '8' - y); } - y = 7; - for (x = 0; x < 8; x++) { - if (x % 2 == 0) - color = highlight[y][x] ? lightsquarehi : lightsquare; - else - color = highlight[y][x] ? darksquarehi : darksquare; + iy = 7; + y = flipboard ? 0 : 7; + for (ix = 0; ix < 8; ix++) { + x = flipboard ? 7 - ix : ix; + + /* inverse square color for text */ + if (x % 2 == 0) { + if (y % 2 == 0) + color = highlight[y][x] ? darksquarehi : darksquare; + else + color = highlight[y][x] ? lightsquarehi : lightsquare; + } else { + if (y % 2 == 0) + color = highlight[y][x] ? lightsquarehi : lightsquare; + else + color = highlight[y][x] ? darksquarehi : darksquare; + } + printf("<text x=\"%d\" y=\"%d\" fill=\"#%02x%02x%02x\" text-anchor=\"start\" style=\"font-family: sans-serif; font-size: 10px\">%c</text>\n", - (x * 45) + 2, (y + 1) * 45 - 3, color[0], color[1], color[2], x + 'a'); + (ix * 45) + 2, (iy + 1) * 45 - 3, color[0], color[1], color[2], x + 'a'); } } @@ -255,7 +284,7 @@ void tty_showboard(void) { const int *color; - int x, y, piece; + int ix, iy, x, y, piece; printf("Board FEN:\n"); showboardfen(); @@ -271,13 +300,17 @@ tty_showboard(void) SETFGCOLOR(0x00, 0x00, 0x00); putchar('\n'); - for (y = 0; y < 8; y++) { + for (iy = 0; iy < 8; iy++) { + y = flipboard ? 7 - iy : iy; + color = border; SETBGCOLOR(color[0], color[1], color[2]); SETFGCOLOR(0xff, 0xff, 0xff); fputs(" ", stdout); - for (x = 0; x < 8; x++) { + for (ix = 0; ix < 8; ix++) { + x = flipboard ? 7 - ix : ix; + if (x % 2 == 0) { if (y % 2 == 0) color = highlight[y][x] ? lightsquarehi : lightsquare; @@ -326,10 +359,14 @@ tty_showboard(void) color = border; SETBGCOLOR(color[0], color[1], color[2]); SETFGCOLOR(0xff, 0xff, 0xff); - if (showcoords) - fputs(" a b c d e f g h ", stdout); - else + if (showcoords) { + if (flipboard) + fputs(" h g f e d c b a ", stdout); + else + fputs(" a b c d e f g h ", stdout); + } else { fputs(" ", stdout); + } printf("\x1b[0m"); /* reset */ printf("\n"); } @@ -355,15 +392,19 @@ ascii_showboard(void) int hi[3] = { '>', ' ', '<' }; int dark[3] = { '.', '.', '.' }; int light[3] = { ' ', ' ', ' ' }; - int *color, x, y, piece; + int *color, ix, iy, x, y, piece; printf("Board FEN:\n"); showboardfen(); printf("\n\n"); - for (y = 0; y < 8; y++) { + for (iy = 0; iy < 8; iy++) { + y = flipboard ? 7 - iy : iy; + fputs("+---+---+---+---+---+---+---+---+\n", stdout); - for (x = 0; x < 8; x++) { + for (ix = 0; ix < 8; ix++) { + x = flipboard ? 7 - ix : ix; + if (x % 2 == 0) { if (y % 2 == 0) color = highlight[y][x] ? hi : light; @@ -375,8 +416,8 @@ ascii_showboard(void) else color = highlight[y][x] ? hi : light; } - - if (x == 0) + + if (ix == 0) putchar('|'); putchar(color[0]); piece = getpiece(x, y); @@ -394,8 +435,12 @@ ascii_showboard(void) putchar('\n'); } fputs("+---+---+---+---+---+---+---+---+\n", stdout); - if (showcoords) - printf(" a | b | c | d | e | f | g | h |\n"); + if (showcoords) { + if (flipboard) + printf(" h | g | f | e | d | c | b | a |\n"); + else + printf(" a | b | c | d | e | f | g | h |\n"); + } fputs("\n", stdout); } @@ -467,7 +512,7 @@ main(int argc, char *argv[]) black_can_castle[1] = 1; } break; - case 3: /* TODO: en-passant square, rest of the fields */ + case 3: /* en passant square */ if (*s >= 'a' && *s <= 'h' && *(s + 1) >= '1' && *(s + 1) >= '6') {