From 45113e97379819f1069d72bfbd10619eecd9aa93 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Sun, 23 Jun 2024 20:29:29 +0700 Subject: [PATCH] Chess960 castling bug (K-B1, R-C1) --- lib/src/game/game.dart | 9 +++++++-- test/castling_test.dart | 9 +++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/src/game/game.dart b/lib/src/game/game.dart index 387981e..26854de 100644 --- a/lib/src/game/game.dart +++ b/lib/src/game/game.dart @@ -418,23 +418,28 @@ class Game { int rookTargetFile = i == 0 ? targetFile - 1 : targetFile + 1; int rookTargetSq = size.square(rookTargetFile, royalRank); // where the rook lands + // Check rook target square is empty (or occupied by the rook/king already) if (board[rookTargetSq].isNotEmpty && rookTargetSq != rookSq && rookTargetSq != from) { continue; } + // Check king target square is empty (or occupied by the castling rook) if (board[targetSq].isNotEmpty && targetSq != rookSq && - targetSq != square) continue; + targetSq != square) { + continue; + } + int numMidSqs = (targetFile - castlingFile!).abs(); bool valid = true; if (!options.ignorePieces) { int numRookMidSquares = (targetFile - rookFile).abs(); if (numRookMidSquares > 1) { for (int j = 1; j <= numRookMidSquares; j++) { - int midFile = rookFile + (i == 0 ? -j : j); + int midFile = rookFile + (rookFile > targetFile ? -j : j); int midSq = size.square(midFile, royalRank); if (board[midSq].isNotEmpty && midFile != castlingFile) { valid = false; diff --git a/test/castling_test.dart b/test/castling_test.dart index c540889..cd3ae0d 100644 --- a/test/castling_test.dart +++ b/test/castling_test.dart @@ -21,6 +21,15 @@ void main() { expect(moves.from(g.size.squareNumber('c1')).length, 3); }); // https://github.com/alexobviously/bishop/issues/77 + test('Chess960 K-B1:R-C1 castling', () { + final g = Game( + variant: CommonVariants.chess960(), + fen: 'rkr4n/ppppqbpp/3bpp1n/8/8/2BPPN2/PPPQBPPP/RKR4N w KQkq - 6 7', + ); + final moves = g.generateLegalMoves(); + expect(moves.castlingMoves.length, 1); + expect(moves.from(g.size.squareNumber('b1')).length, 1); + }); test('Castling rights when a rook takes a rook', () { final g = Game( fen: 'rnbqk1nr/ppp1ppb1/6p1/3p4/8/2N5/PPPPPPP1/R1BQKBNR w KQkq - 0 5',