aboutsummaryrefslogtreecommitdiff
path: root/src/board/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/board/mod.rs')
-rw-r--r--src/board/mod.rs59
1 files changed, 31 insertions, 28 deletions
diff --git a/src/board/mod.rs b/src/board/mod.rs
index 579488b..aab3136 100644
--- a/src/board/mod.rs
+++ b/src/board/mod.rs
@@ -32,6 +32,13 @@ pub struct Board {
attacks: Attacks,
}
+impl Default for Board {
+ fn default() -> Self {
+ Board::new()
+ }
+
+}
+
impl Board {
pub fn new() -> Self {
let default_fen = String::from("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
@@ -68,7 +75,7 @@ impl Board {
.iter()
.enumerate()
.find(|(_, bitboard)| *bitboard & square_bb > 0)
- .and_then(|(pt, _)| Some(Piece::from(pt)))
+ .map(|(pt, _)| Piece::from(pt))
}
pub fn ep_bitboard(&self) -> Bitboard {
@@ -143,7 +150,7 @@ impl Board {
self.zobrist_toggle_piece(source_piece, mov.target);
}
}
- Piece::from(source_piece)
+ source_piece
},
None => {
self.print();
@@ -290,22 +297,19 @@ impl Board {
}
// Return captured piece to target square
- match captured_piece {
- Some(target_piece) => {
- match mov.kind {
- // Return pawn captured by En Passant pawn if needed
- MoveKind::EnPassant => {
- let original_dead_pawn_bb = Square::from_coords(mov.source.rank(), mov.target.file()).to_bitboard();
- self.piece_sets[target_piece as usize] |= original_dead_pawn_bb;
- self.occupancy |= original_dead_pawn_bb;
- },
- _ => {
- self.piece_sets[target_piece as usize] |= move_target_bb;
- self.occupancy |= move_target_bb;
- },
- }
- },
- None => {}
+ if let Some(target_piece) = captured_piece {
+ match mov.kind {
+ // Return pawn captured by En Passant pawn if needed
+ MoveKind::EnPassant => {
+ let original_dead_pawn_bb = Square::from_coords(mov.source.rank(), mov.target.file()).to_bitboard();
+ self.piece_sets[target_piece as usize] |= original_dead_pawn_bb;
+ self.occupancy |= original_dead_pawn_bb;
+ },
+ _ => {
+ self.piece_sets[target_piece as usize] |= move_target_bb;
+ self.occupancy |= move_target_bb;
+ },
+ }
}
@@ -381,16 +385,15 @@ mod tests {
fn make_move() {
let fen = String::from("q1b2k2/5p1p/4p1pb/pPPp4/3N4/3nPB2/P2QKnR1/1R6 w - - 0 25");
let mut board = Board::from_FEN(fen);
- let initial_board = board.clone();
+ let initial_board = board;
board.print();
let black_move = Move { source: Square::F7, target: Square::F5, kind: MoveKind::Quiet };
println!("\n{:?}", black_move);
- match board.make_move(black_move) {
- Some(..) => panic!("No piece should be captured"),
- None => {},
- };
+ if let Some(..) = board.make_move(black_move) {
+ panic!("No piece should be captured");
+ }
board.print();
@@ -423,7 +426,7 @@ mod tests {
fn unmake_move() {
let fen = String::from("q1b2k2/5p1p/4p1pb/pPPp4/3N4/3nPB2/P2QKnR1/1R6 w - - 0 25");
let mut board = Board::from_FEN(fen);
- let initial_board = board.clone();
+ let initial_board = board;
let mov = Move { source: Square::D2, target: Square::A5, kind: MoveKind::Capture };
@@ -442,9 +445,9 @@ mod tests {
fn is_square_attacked() {
let board = Board::new();
- assert_eq!(board.is_square_attacked(Square::E2, Color::White), true);
- assert_eq!(board.is_square_attacked(Square::E2, Color::Black), false);
- assert_eq!(board.is_square_attacked(Square::E4, Color::White), false);
- assert_eq!(board.is_square_attacked(Square::B6, Color::Black), true);
+ assert!(board.is_square_attacked(Square::E2, Color::White));
+ assert!(!board.is_square_attacked(Square::E2, Color::Black));
+ assert!(!board.is_square_attacked(Square::E4, Color::White));
+ assert!(board.is_square_attacked(Square::B6, Color::Black));
}
}