chess-puzzles

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

commit 14096028aee533f132200b020687e1ce3884105b
parent 3ccf4d4450965cc2853d9cbc8b81b75cfd44660a
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Mon, 22 Jan 2024 01:04:59 +0100

fen.c: simplify castling code

Diffstat:
MTODO | 2--
Mfen.c | 56++++++++++++++++++--------------------------------------
2 files changed, 18 insertions(+), 40 deletions(-)

diff --git a/TODO b/TODO @@ -8,8 +8,6 @@ - in check, checkmate. - test more chess960 black kingside and queenside castling. - test more long sequence and halfmove and movenumber counts. -- code cleanup: - - castling code can be much simplified. ? PGN output: add game termination state? - PGN output: add stalemate? - PGN output: but what if resign, time-out, draw offer? diff --git a/fen.c b/fen.c @@ -1088,7 +1088,8 @@ board_playmoves(struct board *b, const char *moves) { char square[3]; const char *castled, *s; - int firstmove, i, x, y, x2, y2, side, otherside, piece, takepiece, tookpiece; + int firstmove, i, x, y, x2, y2, side, otherside, piece; + int rookpiece, takepiece, tookpiece; int countfile, countrank, countboard, px, py; int promote, tookeps; @@ -1171,59 +1172,38 @@ board_playmoves(struct board *b, const char *moves) castled = NULL; /* castling */ - if (piece == 'K' && y == 7 && y2 == 7) { - /* white: kingside castling */ - if (x2 > x + 1 || (x2 > x && takepiece == 'R')) { - for (i = x2; i < 8; i++) { - if (getpiece(b, i, y2) == 'R') { - place(b, 0, x, y); /* clear previous square */ - place(b, 0, i, y2); /* clear rook square */ - place(b, 'R', x2 - 1, y2); /* rook next to king */ - castled = "O-O"; - break; - } - } - } else if (x2 < x - 1 || (x2 < x && takepiece == 'R')) { - /* white: queenside castling */ - for (i = x2; i >= 0; i--) { - if (getpiece(b, i, y2) == 'R') { - place(b, 0, x, y); /* clear previous square */ - place(b, 0, i, y2); /* clear rook square */ - place(b, 'R', x2 + 1, y2); /* rook next to king */ - castled = "O-O-O"; - break; - } - } - } - } else if (piece == 'k' && y == 0 && y2 == 0) { - /* black: kingside castling */ - if (x2 > x + 1 || (x2 > x && takepiece == 'r')) { + if ((piece == 'K' && y == 7 && y2 == 7) || + (piece == 'k' && y == 0 && y2 == 0)) { + rookpiece = piece == 'K' ? 'R' : 'r'; + + /* kingside castling */ + if (x2 > x + 1 || (x2 > x && takepiece == rookpiece)) { for (i = x2; i < 8; i++) { - if (getpiece(b, i, y2) == 'r') { + if (getpiece(b, i, y2) == rookpiece) { place(b, 0, x, y); /* clear previous square */ place(b, 0, i, y2); /* clear rook square */ - place(b, 'r', x2 - 1, y2); /* rook next to king */ + place(b, rookpiece, x2 - 1, y2); /* rook next to king */ + place(b, piece, x2, y2); /* place king */ + x2 = i; /* set square for highlight */ castled = "O-O"; break; } } - } else if (x2 < x - 1 || (x2 < x && takepiece == 'r')) { - /* black: queenside castling */ + } else if (x2 < x - 1 || (x2 < x && takepiece == rookpiece)) { + /* queenside castling */ for (i = x2; i >= 0; i--) { - if (getpiece(b, i, y2) == 'r') { + if (getpiece(b, i, y2) == rookpiece) { place(b, 0, x, y); /* clear previous square */ place(b, 0, i, y2); /* clear rook square */ - place(b, 'r', x2 + 1, y2); /* rook next to king */ + place(b, rookpiece, x2 + 1, y2); /* rook next to king */ + place(b, piece, x2, y2); /* place king */ + x2 = i; /* set square for highlight */ castled = "O-O-O"; break; } } } } - if (castled) { - place(b, piece, x2, y2); /* place king */ - x2 = i; /* set square for highlight */ - } /* remove the ability to castle */ if (piece == 'K') {