fen.c: fix promotion to rook, fix regression in promotion and PGN output 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 a7b46089306de698b20b0400a8ec9f3ccda3f729
 (DIR) parent c569951f00bbd5517962401c1f090cac6a99a8c5
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Sun, 14 Jan 2024 19:09:38 +0100
       
       fen.c: fix promotion to rook, fix regression in promotion and PGN output for it
       
       Diffstat:
         M fen.c                               |      18 +++++++++---------
         M tests.sh                            |       5 +++++
       
       2 files changed, 14 insertions(+), 9 deletions(-)
       ---
 (DIR) diff --git a/fen.c b/fen.c
       @@ -1145,8 +1145,12 @@ board_playmoves(struct board *b, const char *moves)
                        s += 2;
        
                        promote = 0;
       -                if (*s == 'q' || *s == 'b' || *s == 'n') {
       -                        promote = *s;
       +                /* is a piece? not validated, but should be queen, rook, bishop, knight */
       +                if (isvalidpiece(*s)) {
       +                        if (side == 'w')
       +                                promote = toupper(*s);
       +                        else
       +                                promote = tolower(*s);
                                s++;
                        }
        
       @@ -1349,16 +1353,10 @@ board_playmoves(struct board *b, const char *moves)
        
                                        /* possible promotion: queen, rook, bishop, knight */
                                        if (promote) {
       -                                        if (side == 'w')
       -                                                piece = toupper(promote);
       -                                        else
       -                                                piece = tolower(promote);
       -                                        place(b, piece, x2, y2);
       -
                                                speak(dutchmode ? "en promoot naar " : "and promotes to ");
                                                speakpiece(promote);
        
       -                                        pgn("=%c", pgnpiece(piece));
       +                                        pgn("=%c", pgnpiece(promote));
                                        }
                                }
                        }
       @@ -1367,6 +1365,8 @@ board_playmoves(struct board *b, const char *moves)
                        if (!castled) {
                                place(b, 0, x, y);
                                /* place piece or new promoted piece */
       +                        if (promote)
       +                                piece = promote;
                                place(b, piece, x2, y2);
                        }
        
 (DIR) diff --git a/tests.sh b/tests.sh
       @@ -492,6 +492,11 @@ testpgn 'Black checks white (blunder), white defends with knight and checkmate'\
                '1. ... Rd1+ 2. Ng1#'\
                '8/7k/8/8/3r4/7N/6RR/7K b - - 0 1'\
                'd4d1 h3g1'
       +
       +testpgn 'Test a longer sequence of moves, promotion, castling, etc'\
       +        '1. d4 d5 2. Bf4 c6 3. e3 Bf5 4. Bd3 e6 5. g4 Nf6 6. gxf5 Qb6 7. fxe6 fxe6 8. b3 Nbd7 9. Nd2 O-O-O 10. c4 Be7 11. c5 Qa5 12. a3 Ne4 13. Bxe4 Bxc5 14. b4 Qb5 15. Bc2 g5 16. Ba4 Qa6 17. Bxg5 Bd6 18. Bxd8 Rxd8 19. b5 cxb5 20. Rc1+ Bc5 21. Bxb5 Qxb5 22. dxc5 Nxc5 23. Nb3 b6 24. Nxc5 Qxc5 25. Rxc5+ bxc5 26. Qg4 c4 27. Qxe6+ Kc7 28. Nf3 a5 29. Nd4 Rd7 30. Qc6+ Kd8 31. Ne6+ Ke7 32. f4 Rd6 33. Qxd6+ Kxd6 34. f5 Kc6 35. Rg1 Kd6 36. h4 h6 37. h5 Kd7 38. Rg6 Ke7 39. Rxh6 a4 40. Rg6 Kd6 41. Rg5 c3 42. h6 Ke5 43. h7 Ke4 44. h8=Q Kd3 45. Qd4+ Kc2 46. Rg2+ Kb3 47. Nc5+ Kxa3 48. Qxa4#'\
       +        'startpos'\
       +        'd2d4 d7d5 c1f4 c7c6 e2e3 c8f5 f1d3 e7e6 g2g4 g8f6 g4f5 d8b6 f5e6 f7e6 b2b3 b8d7 b1d2 e8c8 c2c4 f8e7 c4c5 b6a5 a2a3 f6e4 d3e4 e7c5 b3b4 a5b5 e4c2 g7g5 c2a4 b5a6 f4g5 c5d6 g5d8 h8d8 b4b5 c6b5 a1c1 d6c5 a4b5 a6b5 d4c5 d7c5 d2b3 b7b6 b3c5 b5c5 c1c5 b6c5 d1g4 c5c4 g4e6 c8c7 g1f3 a7a5 f3d4 d8d7 e6c6 c7d8 d4e6 d8e7 f2f4 d7d6 c6d6 e7d6 f4f5 d6c6 h1g1 c6d6 h2h4 h7h6 h4h5 d6d7 g1g6 d7e7 g6h6 a5a4 h6g6 e7d6 g6g5 c4c3 h5h6 d6e5 h6h7 e5e4 h7h8Q e4d3 h8d4 d3c2 g5g2 c2b3 e6c5 b3a3 d4a4'
        }
        
        tests_fen