From dbe3539a66863cdcabd43798775d65c1ad4de245 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 22 Jul 2022 10:33:45 +0200 Subject: [PATCH] Add naive rook move generation --- src/movegen/mod.rs | 1 + src/movegen/rook.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/movegen/rook.rs diff --git a/src/movegen/mod.rs b/src/movegen/mod.rs index aacfcb4..9f4f280 100644 --- a/src/movegen/mod.rs +++ b/src/movegen/mod.rs @@ -2,3 +2,4 @@ mod bishop; mod king; mod knight; +mod rook; diff --git a/src/movegen/rook.rs b/src/movegen/rook.rs new file mode 100644 index 0000000..31fd7d8 --- /dev/null +++ b/src/movegen/rook.rs @@ -0,0 +1,54 @@ +use crate::board::{Bitboard, Direction, Square}; + +#[allow(unused)] +pub fn rook_moves(square: Square, blockers: Bitboard) -> Bitboard { + Direction::iter_rook() + .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!( + rook_moves(Square::A1, Bitboard::EMPTY), + (File::A.into_bitboard() | Rank::First.into_bitboard()) - Square::A1 + ); + assert_eq!( + rook_moves(Square::A1, Bitboard::ALL), + Square::A2 | Square::B1 + ); + assert_eq!( + rook_moves(Square::A1, Rank::First.into_bitboard()), + (File::A.into_bitboard() | Square::B1) - Square::A1 + ); + assert_eq!( + rook_moves(Square::A1, File::A.into_bitboard()), + (Rank::First.into_bitboard() | Square::A2) - Square::A1 + ); + } + + #[test] + fn moves_middle() { + assert_eq!( + rook_moves(Square::D4, Bitboard::EMPTY), + (File::D.into_bitboard() | Rank::Fourth.into_bitboard()) - Square::D4 + ); + assert_eq!( + rook_moves(Square::D4, Bitboard::ALL), + Square::C4 | Square::D3 | Square::D5 | Square::E4 + ); + assert_eq!( + rook_moves(Square::D4, Rank::Fourth.into_bitboard()), + (File::D.into_bitboard() | Square::C4 | Square::E4) - Square::D4 + ); + assert_eq!( + rook_moves(Square::D4, File::D.into_bitboard()), + (Rank::Fourth.into_bitboard() | Square::D3 | Square::D5) - Square::D4 + ); + } +}