jordanbray / chess

A rust library to manage chess move generation
https://jordanbray.github.io/chess/
MIT License
234 stars 54 forks source link

null_move() should reset en-passant square #17

Closed fanzier closed 5 years ago

fanzier commented 5 years ago

Currently this fails:

let board = Board::from_fen("rnbqkbnr/pppp2pp/8/4pP2/8/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 0".into()) .unwrap();
assert!(board.null_move().unwrap().is_sane());

The reason is that the en-passant square is left untouched by the null_move() function but should be reset.

jordanbray commented 5 years ago

Ok. I'll confirm this tonight and get a patch out this week sometime.

Thanks

On Wed, May 15, 2019, 12:44 PM Fabian Zaiser notifications@github.com wrote:

Currently this fails:

let board = Board::from_fen("rnbqkbnr/pppp2pp/8/4pP2/8/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 0".into()) .unwrap(); assert!(board.null_move().unwrap().is_sane());

The reason is that the en-passant square is left untouched by the null_move() function but should be reset.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jordanbray/chess/issues/17?email_source=notifications&email_token=AADJ5PJXWWUTLFIDXSEJAD3PVQ4XZA5CNFSM4HNFDNNKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4GT7EVKA, or mute the thread https://github.com/notifications/unsubscribe-auth/AADJ5PJ6MB2EO4ZPHZSGGB3PVQ4XZANCNFSM4HNFDNNA .

fanzier commented 5 years ago

Thanks for the quick reply (and for writing the library!).

I think this patch should work:

diff --git a/src/board.rs b/src/board.rs
index 521f901..331db91 100644
--- a/src/board.rs
+++ b/src/board.rs
@@ -613,6 +613,9 @@ impl Board {
     /// Switch the color of the player without actually making a move.  Returns None if the current
     /// player is in check.
     ///
+    /// Note that this erases the en-passant information, so applying this function twice doesn't
+    /// always give the same result back.
+    ///
     /// ```
     /// use chess::{Board, Color};
     ///
@@ -631,6 +634,7 @@ impl Board {
             let mut result = *self;
             result.side_to_move = !result.side_to_move;
             result.hash ^= Zobrist::color();
+            result.remove_ep();
             result.update_pin_info();
             Some(result)
         }
@@ -1090,3 +1094,14 @@ impl Board {
         &self.checkers
     }
 }
+
+#[test]
+fn test_null_move_en_passant() {
+    let start =
+        Board::from_fen("rnbqkbnr/pppp2pp/8/4pP2/8/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 0".into())
+            .unwrap();
+    let expected =
+        Board::from_fen("rnbqkbnr/pppp2pp/8/4pP2/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 0".into())
+            .unwrap();
+    assert_eq!(start.null_move().unwrap(), expected);
+}
jordanbray commented 5 years ago

Fixed in 3.0.2.