From 5101d8c285ea40a9c01cb10601341273b34793aa Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 22 Jul 2022 10:33:33 +0200 Subject: [PATCH] Add naive bishop move generation --- src/movegen/bishop.rs | 69 +++++++++++++++++++++++++++++++++++++++++++ src/movegen/mod.rs | 1 + 2 files changed, 70 insertions(+) create mode 100644 src/movegen/bishop.rs diff --git a/src/movegen/bishop.rs b/src/movegen/bishop.rs new file mode 100644 index 0000000..0fe0247 --- /dev/null +++ b/src/movegen/bishop.rs @@ -0,0 +1,69 @@ +use crate::board::{Bitboard, Direction, Square}; + +#[allow(unused)] +pub fn bishop_moves(square: Square, blockers: Bitboard) -> Bitboard { + Direction::iter_bishop() + .map(|dir| dir.slide_board_with_blockers(square.into_bitboard(), blockers)) + .fold(Bitboard::EMPTY, |lhs, rhs| lhs | rhs) +} + +#[cfg(test)] +mod test { + use super::*; + use crate::board::{File, Rank}; + + #[test] + fn moves_lower_left_square() { + assert_eq!( + bishop_moves(Square::A1, Bitboard::EMPTY), + Bitboard::DIAGONAL - Square::A1 + ); + assert_eq!( + bishop_moves(Square::A1, Bitboard::ALL), + Square::B2.into_bitboard() + ); + assert_eq!( + bishop_moves(Square::A1, Square::D4.into_bitboard()), + Square::B2 | Square::C3 | Square::D4 + ); + assert_eq!( + bishop_moves(Square::A1, File::D.into_bitboard()), + Square::B2 | Square::C3 | Square::D4 + ); + } + + #[test] + fn moves_middle() { + let cross = Bitboard::DIAGONAL | Direction::South.move_board(Bitboard::ANTI_DIAGONAL); + assert_eq!( + bishop_moves(Square::D4, Bitboard::EMPTY), + cross - Square::D4 + ); + assert_eq!( + bishop_moves(Square::D4, Bitboard::ALL), + Square::C3 | Square::C5 | Square::E3 | Square::E5 + ); + assert_eq!( + bishop_moves(Square::D4, Rank::Fifth.into_bitboard()), + Square::A1 + | Square::B2 + | Square::C3 + | Square::C5 + | Square::E3 + | Square::E5 + | Square::F2 + | Square::G1 + ); + assert_eq!( + bishop_moves(Square::D4, File::E.into_bitboard()), + Square::A1 + | Square::A7 + | Square::B2 + | Square::B6 + | Square::C3 + | Square::C5 + | Square::E3 + | Square::E5 + ); + } +} diff --git a/src/movegen/mod.rs b/src/movegen/mod.rs index 35193b2..aacfcb4 100644 --- a/src/movegen/mod.rs +++ b/src/movegen/mod.rs @@ -1,3 +1,4 @@ // Move generation implementation details +mod bishop; mod king; mod knight;