diff options
Diffstat (limited to 'src/board.rs')
-rw-r--r-- | src/board.rs | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/src/board.rs b/src/board.rs index 7d0d6c5..2afbe90 100644 --- a/src/board.rs +++ b/src/board.rs @@ -13,7 +13,7 @@ pub struct Board { } -#[derive(Debug, num_enum::FromPrimitive)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, num_enum::FromPrimitive)] #[repr(usize)] pub enum PieceType { #[default] @@ -200,33 +200,37 @@ impl Board { /// *Blindlessly* apply a move without any validation /// Move should be validated beforehand - pub fn make_move(&mut self, mov: Move) { + pub fn make_move(&mut self, mov: Move) -> Option<PieceType> { // Target - match self.pieces + let captured_piece = match self.pieces .iter() .enumerate() - .find(|(piece_type, p)| *p & mov.target.to_bitboard() > 0) + .find(|(piece_type, bitboard)| *bitboard & mov.target.to_bitboard() > 0) { - Some((piece, bitboard)) => { - self.pieces[piece] ^= mov.target.to_bitboard(); + Some((piece_type, _)) => { + self.pieces[piece_type] ^= mov.target.to_bitboard(); + Some(PieceType::from(piece_type)) }, - None => {}, + None => None, }; // Source match self.pieces .iter() .enumerate() - .find(|(piece_type, p)| *p & mov.source.to_bitboard() > 0) + .find(|(piece_type, bitboard)| *bitboard & mov.source.to_bitboard() > 0) { - Some((piece, bitboard)) => { - self.pieces[piece] ^= mov.source.to_bitboard(); - self.pieces[piece] |= mov.target.to_bitboard(); + Some((piece_type, _)) => { + self.pieces[piece_type] ^= mov.source.to_bitboard(); + self.pieces[piece_type] |= mov.target.to_bitboard(); }, None => panic!("Move is malformed: source piece not found"), }; + self.update_occupancy(); self.ply += 1; + + captured_piece } } @@ -305,7 +309,12 @@ mod tests { let black_move = Move { source: Square::F7, target: Square::F5 }; println!("\n{:?}", black_move); - board.make_move(black_move); + + match board.make_move(black_move) { + Some(..) => panic!("No piece should have been captured"), + None => {}, + }; + board.print(); assert!(board.pieces[PieceType::PawnBlack as usize] & Square::F7.to_bitboard() == 0); @@ -314,7 +323,12 @@ mod tests { let white_move = Move { source: Square::D2, target: Square::A5 }; println!("\n{:?}", white_move); - board.make_move(white_move); + + match board.make_move(white_move) { + Some(captured) => assert!(captured == PieceType::PawnBlack), + None => panic!("A piece should have been captured"), + }; + board.print(); assert!(board.pieces[PieceType::PawnBlack as usize] & Square::A5.to_bitboard() == 0, "Target piece should be captured"); |