From efd5505b7f211e09e56dc6482c850b5ff0fe8737 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 23 Jan 2023 06:11:04 +0300 Subject: feat: implement apply_move --- src/board.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'src/board.rs') diff --git a/src/board.rs b/src/board.rs index ca526b7..e0f2a0b 100644 --- a/src/board.rs +++ b/src/board.rs @@ -195,6 +195,35 @@ impl Board { } moves } + + /// *Blindlessly* apply a move without any validation + /// Move should be validated beforehand + pub fn apply_move(&mut self, mov: Move) { + // Target + match self.pieces + .iter() + .enumerate() + .find(|(piece_type, p)| *p & mov.target.to_bitboard() > 0) + { + Some((piece, bitboard)) => { + self.pieces[piece] ^= mov.target.to_bitboard(); + }, + None => {}, + }; + + // Source + match self.pieces + .iter() + .enumerate() + .find(|(piece_type, p)| *p & mov.source.to_bitboard() > 0) + { + Some((piece, bitboard)) => { + self.pieces[piece] ^= mov.source.to_bitboard(); + self.pieces[piece] |= mov.target.to_bitboard(); + }, + None => panic!("Move is malformed: source piece not found"), + }; + } } @@ -265,4 +294,29 @@ mod tests { mov.print(); } } + + #[test] + fn test_apply_move() { + let fen = String::from("q1b2k2/5p1p/4p1pb/pPPp4/3N4/3nPB2/P2QKnR1/1R6 w - - 0 25"); + let mut board = Board::from_FEN(fen); + board.print(); + + let black_move = Move { source: Square::F7, target: Square::F5 }; + println!("\n{:?}", black_move); + board.apply_move(black_move); + board.print(); + + assert!(board.pieces[PieceType::PawnBlack as usize] & Square::F7.to_bitboard() == 0); + assert!(board.pieces[PieceType::PawnBlack as usize] & Square::F5.to_bitboard() > 0); + + let white_move = Move { source: Square::D2, target: Square::A5 }; + println!("\n{:?}", white_move); + board.apply_move(white_move); + board.print(); + + assert!(board.pieces[PieceType::PawnBlack as usize] & Square::A5.to_bitboard() == 0, "Target piece should be captured"); + assert!(board.pieces[PieceType::Queen as usize] & Square::D2.to_bitboard() == 0); + assert!(board.pieces[PieceType::Queen as usize] & Square::A5.to_bitboard() > 0); + + } } -- cgit v1.2.3