alexobviously / bishop

A chess logic package for Dart with flexible variant support
https://pub.dev/packages/bishop
Other
19 stars 8 forks source link

BUG Chess960: When rook is in check, castling is impossible. #11

Closed malaschitz closed 1 year ago

malaschitz commented 1 year ago
import 'package:bishop/bishop.dart' as bishop;

void main() {
  String pos = 'RKNQRNBB';
  String fen = '${pos.toLowerCase()}/pppppppp/8/8/8/8/PPPPPPPP/$pos w';
  bishop.Game game = bishop.Game(variant: bishop.Variant.chess960(), fen: fen);
  List<String> moves =
      'g2g3,d7d5,d2d4,c8b6,c1b3,f8d7,b3c5,g7g6,c5d7,d8d7,f1e3,f7f5,f2f4,e7e6,g3g4,g8f7,g4f5,e6f5,d1d3,d7a4,b2b3,a4d4'
          .split(',');
  for (var m in moves) {
    game.makeMoveString(m);
  }
  print(game.ascii());
  print(game.fen);
  var mvs = game.generateLegalMoves();
  for (var m in mvs) {
    print(game.toAlgebraic(m));
  }
}

Output:

   +------------------------+
 8 | r  k  .  .  r  .  .  b |
 7 | p  p  p  .  .  b  .  p |
 6 | .  n  .  .  .  .  p  . |
 5 | .  .  .  p  .  p  .  . |
 4 | .  .  .  q  .  P  .  . |
 3 | .  P  .  Q  N  .  .  . |
 2 | P  .  P  .  P  .  .  P |
 1 | R  K  .  .  R  .  B  B |
   +------------------------+
     a  b  c  d  e  f  g  h  
rk2r2b/ppp2b1p/1n4p1/3p1p2/3q1P2/1P1QN3/P1P1P2P/RK2R1BB w EAea - 0 12
b3b4
d3c4
d3b5
d3a6
d3e4
d3f5
d3d4
d3d2
d3d1
d3c3
e3c4
e3d5
e3f5
e3g4
e3d1
e3g2
e3f1
a2a3
a2a4
c2c3
c2c4
h2h3
h2h4
b1c1
e1d1
e1c1
e1f1
g1f2
h1g2
h1f3
h1e4
h1d5

In this position White should be allowed a big castle - move b1a1. In this move the king moves from b1 to c1. But castling is not among the legal moves.

malaschitz commented 1 year ago

Solved in https://github.com/alexobviously/bishop/commit/83ed2f5125b956f108db7a27070f7af00e8ad36c

alexobviously commented 1 year ago

I found a more accurate solution: https://github.com/alexobviously/bishop/commit/9ff23027c1ca62e213c0e0af38446c9bdb3c8328 Basically just moved the guard clause to check if the target square is the rook square before checking if it's attacked. Now it behaves as expected, and as a side effect (I think), a previously failing perft benchmark from early on is now working.

For quick future reference: https://lichess.org/analysis/chess960/rk2r2b/ppp2b1p/1n4p1/3p1p2/3q1P2/1P1QN3/P1P1P2P/RK2R1BB_w_EAea_-_0_12 - this is the expected behaviour (and what we currently have)