Introduction
Introduction Statistics Contact Development Disclaimer Help
fen.c: simplify castling code - 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 <[email protected]>
Date: Mon, 22 Jan 2024 01:04:59 +0100
fen.c: simplify castling code
Diffstat:
M TODO | 2 --
M fen.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, too…
+ 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 pr…
- place(b, 0, i, y2); /* clear r…
- place(b, 'R', x2 - 1, y2); /* …
- 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 pr…
- place(b, 0, i, y2); /* clear r…
- place(b, 'R', x2 + 1, y2); /* …
- 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 pr…
place(b, 0, i, y2); /* clear r…
- place(b, 'r', x2 - 1, y2); /* …
+ place(b, rookpiece, x2 - 1, y2…
+ place(b, piece, x2, y2); /* pl…
+ x2 = i; /* set square for high…
castled = "O-O";
break;
}
}
- } else if (x2 < x - 1 || (x2 < x && takepiece == 'r'))…
- /* black: queenside castling */
+ } else if (x2 < x - 1 || (x2 < x && takepiece == rookp…
+ /* 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 pr…
place(b, 0, i, y2); /* clear r…
- place(b, 'r', x2 + 1, y2); /* …
+ place(b, rookpiece, x2 + 1, y2…
+ place(b, piece, x2, y2); /* pl…
+ x2 = i; /* set square for high…
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') {
You are viewing proxied material from codemadness.org. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.