From 626ac6245e79e72554a2538c8f4b36266ff8a462 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 6 Jan 2024 20:34:16 -0800 Subject: [PATCH] [board] Copy the update_moves_with_ray macro into the rook move generator This is an ugly copy/paste operation, but reduces the code in rook::ClassicalMoveGenerator somewhat. --- board/src/moves/rook.rs | 55 +++++++++++++---------------------------- 1 file changed, 17 insertions(+), 38 deletions(-) diff --git a/board/src/moves/rook.rs b/board/src/moves/rook.rs index d5526da..82b8c76 100644 --- a/board/src/moves/rook.rs +++ b/board/src/moves/rook.rs @@ -1,7 +1,5 @@ // Eryn Wells -use std::io::empty; - use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet}; use crate::{ bitboard::BitBoard, @@ -29,44 +27,25 @@ impl<'pos> MoveGeneratorInternal for ClassicalMoveGenerator<'pos> { let mut all_moves = BitBoard::empty(); - // Find the first occupied square along the northward ray. Do this by - // looking at the squares along that ray in trailing (south to north) - // order. - let ray = BitBoard::ray(square, Direction::North); - if let Some(first_occupied_square) = (ray & blockers).occupied_squares_trailing().next() { - let remainder = BitBoard::ray(first_occupied_square, Direction::North); - let attack_ray = ray & !remainder; - all_moves |= attack_ray; - } else { - all_moves |= ray; + macro_rules! update_moves_with_ray { + ($direction:ident, $occupied_squares:tt) => { + let ray = BitBoard::ray(square, Direction::$direction); + if let Some(first_occupied_square) = + BitBoard::$occupied_squares(&(ray & blockers)).next() + { + let remainder = BitBoard::ray(first_occupied_square, Direction::$direction); + let attack_ray = ray & !remainder; + all_moves |= attack_ray; + } else { + all_moves |= ray; + } + }; } - let ray = BitBoard::ray(square, Direction::East); - if let Some(first_occupied_square) = (ray & blockers).occupied_squares_trailing().next() { - let remainder = BitBoard::ray(first_occupied_square, Direction::East); - let attack_ray = ray & !remainder; - all_moves |= attack_ray; - } else { - all_moves |= ray; - } - - let ray = BitBoard::ray(square, Direction::South); - if let Some(first_occupied_square) = (ray & blockers).occupied_squares().next() { - let remainder = BitBoard::ray(first_occupied_square, Direction::South); - let attack_ray = ray & !remainder; - all_moves |= attack_ray; - } else { - all_moves |= ray; - } - - let ray = BitBoard::ray(square, Direction::West); - if let Some(first_occupied_square) = (ray & blockers).occupied_squares().next() { - let remainder = BitBoard::ray(first_occupied_square, Direction::West); - let attack_ray = ray & !remainder; - all_moves |= attack_ray; - } else { - all_moves |= ray; - } + update_moves_with_ray!(North, occupied_squares_trailing); + update_moves_with_ray!(East, occupied_squares_trailing); + update_moves_with_ray!(South, occupied_squares); + update_moves_with_ray!(West, occupied_squares); let quiet_moves_bb = all_moves & (empty_squares | !friendly_pieces); let capture_moves_bb = all_moves & opposing_pieces;