Add naive bishop move generation

This commit is contained in:
Bruno BELANYI 2022-07-22 10:33:33 +02:00
parent f0847a4e49
commit 8ff47231a0
2 changed files with 71 additions and 0 deletions

70
src/movegen/bishop.rs Normal file
View file

@ -0,0 +1,70 @@
use crate::board::{Bitboard, Direction, Square};
/// Compute a bishop's movement given a set of blockers that cannot be moved past.
#[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
);
}
}

View file

@ -1,3 +1,4 @@
// Move generation implementation details
mod bishop;
mod king;
mod knight;