detect en passant, add some tests for it - 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 64c4e3aaecf348c2cb7c61f5569cae1a820d20db
 (DIR) parent d69d3ff1075f952ebdd45af40e7580bb3003721f
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Wed, 20 Dec 2023 20:35:16 +0100
       
       detect en passant, add some tests for it
       
       Diffstat:
         M fen_to_svg.c                        |      21 ++++++++++++++++++---
         M tests.sh                            |      17 +++++++++++++++--
       
       2 files changed, 33 insertions(+), 5 deletions(-)
       ---
 (DIR) diff --git a/fen_to_svg.c b/fen_to_svg.c
       @@ -1,4 +1,3 @@
       -/* TODO: write some test-cases for parsing and output FEN */
        /* TODO: option to flip board? */
        
        #include <ctype.h>
       @@ -120,8 +119,6 @@ showboardfen(void)
                        putchar('-');
                }
                printf(" %d %d", halfmove, movenumber);
       -
       -        /* ? TODO: detect en passant, invalid castling etc? */
        }
        
        void
       @@ -592,6 +589,24 @@ main(int argc, char *argv[])
                                                black_can_castle[0] = 0;
                                }
        
       +                        /* the en passant square resets after a move */
       +                        enpassantsquare[0] = -1;
       +                        enpassantsquare[1] = -1;
       +                        /* moved 2 squares and there is an opponent pawn next to it */
       +                        if (piece == 'P' && y == 6 && y2 == 4) {
       +                                if (getpiece(x - 1, y2) == 'p' ||
       +                                    getpiece(x + 1, y2) == 'p') {
       +                                        enpassantsquare[0] = x;
       +                                        enpassantsquare[1] = 5;
       +                                }
       +                        } else if (piece == 'p' && y == 1 && y2 == 3) {
       +                                if (getpiece(x - 1, y2) == 'P' ||
       +                                    getpiece(x + 1, y2) == 'P') {
       +                                        enpassantsquare[0] = x;
       +                                        enpassantsquare[1] = 2;
       +                                }
       +                        }
       +
                                /* possible promotion: queen, knight, bishop */
                                if (*s == 'q' || *s == 'n' || *s == 'b') {
                                        if (side_to_move == 'w')
 (DIR) diff --git a/tests.sh b/tests.sh
       @@ -63,5 +63,18 @@ testfen 'r3kbr1/pp2qppp/n1p2n2/3p1B2/3P1B2/4QN1b/PPP2PPP/RN2K2R b KQq - 9 12'\
                'r3kbr1/pp2qppp/n1p2n2/3p4/3P1B2/3BQN1b/PPP2PPP/RN2K2R w KQq - 8 12'\
                'd3f5'
        
       -# TODO: test halfmove
       -# TODO: test enpassant
       +# test en passant: move white pawn g2 to g4, enpassant square becomes g3.
       +testfen 'r4k1r/ppppp1pp/8/8/5pP1/4P3/PPPP1P1P/R3K2R b - g3 0 4'\
       +        'r4k1r/ppppp1pp/8/8/5p2/4P3/PPPP1PPP/R3K2R w - - 0 4'\
       +        'g2g4'
       +# moving 2 squares near a pawn, but not en passant.
       +testfen 'r4k1r/ppppp1p1/8/5p2/6P1/4P2p/PPPP1P1P/R3K2R b - - 0 6'\
       +        'r4k1r/ppppp1p1/8/5p2/8/4P2p/PPPP1PPP/R3K2R w - - 0 6'\
       +        'g2g4'
       +# test en passant for black.
       +testfen 'r4k1r/ppp1p1p1/8/2Pp1p2/6P1/4P2p/PP1P1P1P/R3K2R w - d6 0 9'\
       +        'r4k1r/ppppp1p1/8/2P2p2/6P1/4P2p/PP1P1P1P/R3K2R b - - 0 8'\
       +        'd7d5'
       +testfen 'r4k1r/p1ppp1p1/8/1pP2p2/6P1/4P2p/PP1P1P1P/R3K2R w - b6 0 9'\
       +        'r4k1r/ppppp1p1/8/2P2p2/6P1/4P2p/PP1P1P1P/R3K2R b - - 0 8'\
       +        'b7b5'