1
0
Fork 0

use macro

This commit is contained in:
Paul-Nicolas Madelaine 2025-11-13 23:10:22 +01:00
parent c19498e6b5
commit 34df546eaa

View file

@ -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::<PAWN>($($x),+),
Role::Knight => $f::<KNIGHT>($($x),+),
Role::Bishop => $f::<BISHOP>($($x),+),
Role::Rook => $f::<ROOK>($($x),+),
Role::Queen => $f::<QUEEN>($($x),+),
Role::King => $f::<KING>($($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<Move<'l>, 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)
}
}