diff --git a/src/position.rs b/src/position.rs index c7bffc4..22472b4 100644 --- a/src/position.rs +++ b/src/position.rs @@ -60,6 +60,26 @@ pub struct Position(Setup); pub(crate) const MAX_LEGAL_MOVES: usize = 218; +const PAWN: u8 = Role::Pawn as u8; +const KNIGHT: u8 = Role::Knight as u8; +const BISHOP: u8 = Role::Bishop as u8; +const ROOK: u8 = Role::Rook as u8; +const QUEEN: u8 = Role::Queen as u8; +const KING: u8 = Role::King as u8; + +macro_rules! map_role { + ($f:ident, $role:ident, $($x:expr),+) => ( + match $role { + Role::Pawn => $f::($($x),+), + Role::Knight => $f::($($x),+), + Role::Bishop => $f::($($x),+), + Role::Rook => $f::($($x),+), + Role::Queen => $f::($($x),+), + Role::King => $f::($($x),+), + } + ) +} + impl Position { /// Returns the initial position of a chess game. /// @@ -366,14 +386,7 @@ impl Position { } else { role }; - match role { - Role::Pawn => aux::<1>(self, promotion, from, to), - Role::Knight => aux::<2>(self, promotion, from, to), - Role::Bishop => aux::<3>(self, promotion, from, to), - Role::Rook => aux::<4>(self, promotion, from, to), - Role::Queen => aux::<5>(self, promotion, from, to), - Role::King => aux::<6>(self, promotion, from, to), - } + map_role!(aux, role, self, promotion, from, to) } pub(crate) fn move_from_san<'l>(&'l self, san: &San) -> Result, InvalidSan> { @@ -482,14 +495,7 @@ impl Position { }, } } - match role { - Role::Pawn => aux::<1>(self, promotion, from, to), - Role::Knight => aux::<2>(self, promotion, from, to), - Role::Bishop => aux::<3>(self, promotion, from, to), - Role::Rook => aux::<4>(self, promotion, from, to), - Role::Queen => aux::<5>(self, promotion, from, to), - Role::King => aux::<6>(self, promotion, from, to), - } + map_role!(aux, role, self, promotion, from, to) } }