aboutsummaryrefslogtreecommitdiff
path: root/src/board.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/board.rs')
-rw-r--r--src/board.rs54
1 files changed, 54 insertions, 0 deletions
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);
+
+ }
}