lichess-org / dartchess

Dart chess library for native platforms
https://pub.dev/packages/dartchess
GNU General Public License v3.0
35 stars 18 forks source link

fix en passant bug for Crazyhouse and castling bug #18

Closed dbergan closed 1 year ago

dbergan commented 1 year ago

A pair of bugs I uncovered while investigating the Crazyhouse perft today.

En Passant for Crazyhouse

The previous _copyWith() would carry the epSquare forward to the next move, which made this sequence possible:

Black: Qg5 White: Nxg5 Black: f5 (creates epSquare at f6) White: Q@f7 (copyWith() is given null for epSquare, which then copies f6 forward to Black) Black: gxf6 (captures the Queen on f7)

Castling

The previous _getCastlingSide() created all sorts of "legal" castling moves. e.g. depending on the setup, I saw things like Ka2, Kd4, and Kf3 (moving onto his own pawn or knight) resulting in a kingside castle, and Ke1 (moving onto its own square) resulting in a queenside castle. In one position I had 13 legal moves that resulted in a castle. They all involved moving onto another friendly piece or e1.

The corrected version only allows 2 castling options for kingside and 2 options for queenside: moving the king two squares to the left or right OR moving the king onto the rook

veloce commented 1 year ago

👍 thanks for this!

It's missing tests for all the error cases described above.