small code-cleanup and add some comments - chess-puzzles - chess puzzle book generator
 (HTM) git clone git://git.codemadness.org/chess-puzzles
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit df3a95f12df8a701ed0ad5997be6ad5c3f840bda
 (DIR) parent da65c61271ad6e8eb3fde5a8e828ed4d2c882fb2
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Mon,  8 Jan 2024 21:50:26 +0100
       
       small code-cleanup and add some comments
       
       Diffstat:
         M fen.c                               |      53 +++++++++++++++++--------------
       
       1 file changed, 30 insertions(+), 23 deletions(-)
       ---
 (DIR) diff --git a/fen.c b/fen.c
       @@ -15,19 +15,20 @@
        #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)
        
       -enum outputmode { ModeInvalid = 0, ModeASCII, ModeCGI, ModeFEN, ModePGN, ModeTTY, ModeSVG, ModeSpeak };
       -enum outputmode outputmode = ModeSVG;
       +enum outputmode { ModeInvalid = 0, ModeASCII, ModeCGI, ModeFEN, ModePGN,
       +                  ModeTTY, ModeSVG, ModeSpeak };
       +enum outputmode outputmode = ModeSVG; /* default is SVG */
        
        static int onlylastmove = 0, silent = 0, dutchmode = 0;
        
        /* localization of letter for PGN pieces */
        const char *pgn_piecemapping = "";
        
       -typedef unsigned char Color;
       +typedef unsigned char Color; /* for RGB: 0-255 */
        
        struct theme {
                const char *name;
       -        /* RGB */
       +        /* RGB values */
                Color border[3];
                Color darksquare[3];
                Color lightsquare[3];
       @@ -60,7 +61,7 @@ struct theme themes[] = {
                .lightsquarecheck = { 0xff, 0x6a, 0x6a },
                .darksquarecheck = { 0xff, 0x3a, 0x3a  }
        },
       -/* greyscale theme, highlight is still green */
       +/* greyscale theme, highlight is still green though */
        {
                .name = "grey",
                .border = { 0x00, 0x00, 0x00 },
       @@ -88,9 +89,7 @@ struct board {
        
                int flipboard;           /* flip board ? default: 0 */
                int showcoords;          /* board coordinates? default: 1 */
       -
       -        /* board theme */
       -        struct theme *theme;
       +        struct theme *theme;     /* board theme */
        };
        
        /* set theme by name */
       @@ -108,11 +107,12 @@ board_set_theme(struct board *b, const char *name)
                return NULL;
        }
        
       +/* initialize board and set sane defaults */
        void
        board_init(struct board *b)
        {
                memset(b, 0, sizeof(*b)); /* zero fields by default */
       -        b->side_to_move = 'w';
       +        b->side_to_move = 'w'; /* white */
                b->enpassantsquare[0] = -1; /* no en passant */
                b->enpassantsquare[1] = -1;
                b->movenumber = 1;
       @@ -121,6 +121,7 @@ board_init(struct board *b)
                b->theme = &themes[0]; /* use first theme as default */
        }
        
       +/* copy entire board and its state */
        void
        board_copy(struct board *bd, struct board *bs)
        {
       @@ -191,6 +192,7 @@ squaretoxy(const char *s, int *x, int *y)
                return 0;
        }
        
       +/* write formatted string, only if output mode is ModePGN */
        void
        pgn(const char *fmt, ...)
        {
       @@ -204,6 +206,20 @@ pgn(const char *fmt, ...)
                va_end(ap);
        }
        
       +/* write formatted string, only if output mode is ModeSpeak */
       +void
       +speak(const char *fmt, ...)
       +{
       +        va_list ap;
       +
       +        if (outputmode != ModeSpeak || silent)
       +                return;
       +
       +        va_start(ap, fmt);
       +        vprintf(fmt, ap);
       +        va_end(ap);
       +}
       +
        /* remap letter for PGN pieces, default: "KQRBN"
           Dutch: (K)oning, (D)ame, (T)oren, (L)oper, (P)aard: "KDTLP" */
        int
       @@ -227,19 +243,6 @@ pgnpiece(int piece)
        }
        
        void
       -speak(const char *fmt, ...)
       -{
       -        va_list ap;
       -
       -        if (outputmode != ModeSpeak || silent)
       -                return;
       -
       -        va_start(ap, fmt);
       -        vprintf(fmt, ap);
       -        va_end(ap);
       -}
       -
       -void
        speakpiece(int piece)
        {
                switch (piece) {
       @@ -764,6 +767,8 @@ isincheck(struct board *b, int side)
                return 0;
        }
        
       +/* copy the board state and try the piece move, see if the piece wouldn't put
       +   the ourself in check */
        int
        trypiecemove(struct board *b, int side, int piece,
                     int x1, int y1, int x2, int y2, int px, int py)
       @@ -1406,7 +1411,9 @@ board_playmoves(struct board *b, const char *moves)
        void
        usage(char *argv0)
        {
       -        fprintf(stderr, "usage: %s [-cCfF] [-l] [-m mapping] [-o ascii|fen|pgn|speak|svg|tty] [-t default|green|grey] [FEN] [moves]\n", argv0);
       +        fprintf(stderr, "usage: %s [-cCfF] [-l] [-m mapping] "
       +                "[-o ascii|fen|pgn|speak|svg|tty] [-t default|green|grey] "
       +                "[FEN] [moves]\n", argv0);
                exit(1);
        }