Add en passant

This commit is contained in:
Kumu 2022-01-01 17:12:46 -05:00
parent 2e8f0f6005
commit 509ffd524c

View file

@ -3,6 +3,8 @@ use crate::api_types;
use crate::api_types::{PieceKind, Color, Move, Coordinates};
use std::collections::{HashMap, HashSet};
use std::ops;
use tracing::span;
use crate::api_types::Color::White;
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub(crate) struct Position {
@ -148,12 +150,16 @@ impl Piece {
};
// can move to visible squares diagonally ahead of a piece of opposite color is there
// OR if it is the en passant target square
for pos in self.visible.borrow().iter() {
if let Some(piece) = state.board.get(&pos) {
if piece.color == opposite_color {
possible_moves.insert(pos.clone());
}
}
if state.en_passant_target.as_ref() == Some(pos) {
possible_moves.insert(pos.clone());
}
}
// can move to square directly ahead if no piece is there
@ -194,6 +200,7 @@ impl Piece {
pub(crate) struct GameState {
board: HashMap<Position, Piece>,
visible: HashSet<Position>,
en_passant_target: Option<Position>,
}
impl GameState {
@ -246,9 +253,33 @@ impl GameState {
panic!("No piece found at position specified in move");
}
Some(piece) => {piece}
};
}.clone();
if piece.get_possible_moves(&from_pos, &self).contains(&to_pos) {
// if pawn is moving to en passant square, remove captured pawn
if piece.kind == PieceKind::Pawn && Some(&to_pos) == self.en_passant_target.as_ref() {
let pawn_pos = &to_pos + match piece.color {
Color::Black => {(0, 1)}
White => {(0, -1)}
};
self.board.remove(&pawn_pos.unwrap());
}
// update en passant square if a pawn moves 2 squares ahead
if piece.kind == PieceKind::Pawn &&
(
(piece.color == Color::White && from_pos.rank == 1 && to_pos.rank == 3) ||
(piece.color == Color::Black && from_pos.rank == 6 && to_pos.rank == 4)
) {
// en passant square is square behind where pawn moves to
self.en_passant_target = &to_pos + match piece.color {
Color::Black => {(0, 1)}
White => {(0, -1)}
};
} else {
self.en_passant_target = None;
}
// move the piece
if let Some(piece) = self.board.remove(&from_pos) {
self.board.insert(to_pos, piece);
}