Repository: thomas-mauran/chess-tui (language: rust)

Issue:
"En Passant" Move Not Functioning Correctly
## Description

While playing a game in the Normal game mode, I attempted to perform an "en passant" move with a black pawn to capture a white pawn that had moved forward two squares in the previous turn. Although the black pawn moved to the correct position, the white pawn did not disappear from the board as it should have according to the rules of chess.

## To Reproduce

1. Clone the repository using `git clone https://github.com/thomas-mauran/chess-tui.git`
2. Run the project using `cargo run`.
3. Select the Normal game mode from the main menu.
4. Play the following moves:
    1. ♟ e2-e4     ♙ e2-e4
    2. ♚ e1-e2     ♙ e4-d5
    3. ♟ f2-f4     ♙ d5-c6

## Expected behavior

The white pawn should be removed from the board when the black pawn moves to its capture position during the "en passant" move.

## Screenshots

<img width="1157" alt="截屏2024-11-21 17 05 31" src="https://github.com/user-attachments/assets/cc5573e1-a4a9-4609-bc88-2445e8c766ff">

## Environment

- OS: MacOS
- Terminal Emulator: kitty
- Font: Hack Nerd Font
- Crate version: 1.4.0




Discussion / hints:
Nice catch, fixing that tonight

Fix patch:
diff --git a/src/board.rs b/src/board.rs
index 91f2138..a45fbcf 100644
--- a/src/board.rs
+++ b/src/board.rs
@@ -673,11 +673,6 @@ impl Board {
         if !from.is_valid() || !to.is_valid() {
             return;
         }
-        let direction_y: i32 = if self.player_turn == PieceColor::White {
-            -1
-        } else {
-            1
-        };
 
         let piece_type_from = get_piece_type(self.board, from);
         let piece_type_to = get_piece_type(self.board, to);
@@ -725,7 +720,7 @@ impl Board {
         // We check for en passant as the latest move
         if self.is_latest_move_en_passant(*from, *to) {
             // we kill the pawn
-            let row_index = to.row as i32 - direction_y;
+            let row_index = to.row as i32 + 1;
 
             self.board[row_index as usize][to.col as usize] = None;
         }


Test patch:
diff --git a/tests/pieces/pawn.rs b/tests/pieces/pawn.rs
index de719b6..60fad89 100644
--- a/tests/pieces/pawn.rs
+++ b/tests/pieces/pawn.rs
@@ -297,6 +297,31 @@ mod tests {
         );
         positions.sort();
         assert_eq!(right_positions, positions);
+
+        // Perform the en passant move
+        board.move_piece_on_the_board(&Coord { row: 3, col: 2 }, &Coord { row: 2, col: 3 });
+
+        let board_after_move = [
+            [None, None, None, None, None, None, None, None],
+            [None, None, None, None, None, None, None, None],
+            [
+                None,
+                None,
+                None,
+                Some((PieceType::Pawn, PieceColor::White)),
+                None,
+                None,
+                None,
+                None,
+            ],
+            [None, None, None, None, None, None, None, None],
+            [None, None, None, None, None, None, None, None],
+            [None, None, None, None, None, None, None, None],
+            [None, None, None, None, None, None, None, None],
+            [None, None, None, None, None, None, None, None],
+        ];
+
+        assert_eq!(board_after_move, board.board);
     }
 
     #[test]
@@ -340,6 +365,30 @@ mod tests {
         );
         positions.sort();
         assert_eq!(right_positions, positions);
+
+        board.move_piece_on_the_board(&Coord { row: 3, col: 2 }, &Coord { row: 2, col: 3 });
+
+        let board_after_move = [
+            [None, None, None, None, None, None, None, None],
+            [None, None, None, None, None, None, None, None],
+            [
+                None,
+                None,
+                None,
+                Some((PieceType::Pawn, PieceColor::Black)),
+                None,
+                None,
+                None,
+                None,
+            ],
+            [None, None, None, None, None, None, None, None],
+            [None, None, None, None, None, None, None, None],
+            [None, None, None, None, None, None, None, None],
+            [None, None, None, None, None, None, None, None],
+            [None, None, None, None, None, None, None, None],
+        ];
+
+        assert_eq!(board_after_move, board.board);
     }
 
     #[test]