Skip to content

Commit

Permalink
Chess960 castling bug (K-B1, R-C1)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexobviously committed Jun 23, 2024
1 parent 6040d59 commit 45113e9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/src/game/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions test/castling_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 45113e9

Please sign in to comment.