1
0
Fork 0

wip: update MoveGen

This commit is contained in:
Paul-Nicolas Madelaine 2025-12-18 00:04:41 +01:00
parent a8b7b9ca63
commit bc0422a53e
2 changed files with 27 additions and 24 deletions

View file

@ -223,17 +223,15 @@ pub struct Moves<'l> {
impl<'l> MoveGen<Infallible> for Moves<'l> {
#[inline]
fn is_check(&mut self) -> ControlFlow<Infallible> {
self.is_check = true;
ControlFlow::Continue(())
}
#[inline]
fn en_passant_is_legal(&mut self) -> ControlFlow<Infallible> {
self.en_passant_is_legal = true;
fn checkers(&mut self, b: Bitboard) -> ControlFlow<Infallible> {
self.is_check = !b.is_empty();
ControlFlow::Continue(())
}
#[inline]
fn extend(&mut self, s: MoveSet) -> ControlFlow<Infallible> {
if let MoveSet::EnPassant { .. } = s {
self.en_passant_is_legal = true;
}
s.for_each(|raw| unsafe { self.array.push_unchecked(raw) });
ControlFlow::Continue(())
}

View file

@ -130,8 +130,10 @@ impl Position {
/// Returns `true` if the king is in check.
#[must_use]
pub fn is_check(&self) -> bool {
struct MoveGenImpl;
impl MoveGen<()> for MoveGenImpl {
struct MoveGenImpl {
checkers: Bitboard,
}
impl MoveGen<core::convert::Infallible> for MoveGenImpl {
#[inline]
fn roles(&self, _role: Role) -> bool {
false
@ -145,11 +147,16 @@ impl Position {
Bitboard::new()
}
#[inline]
fn is_check(&mut self) -> ControlFlow<()> {
ControlFlow::Break(())
fn checkers(&mut self, b: Bitboard) -> ControlFlow<core::convert::Infallible> {
self.checkers = b;
ControlFlow::Continue(())
}
}
self.generate_moves(&mut MoveGenImpl).is_break()
let mut acc = MoveGenImpl {
checkers: Bitboard::new(),
};
let ControlFlow::Continue(()) = self.generate_moves(&mut acc);
!acc.checkers.is_empty()
}
/// Returns `true` if there is no legal move on the position.
@ -184,8 +191,11 @@ impl Position {
self.to
}
#[inline]
fn en_passant_is_legal(&mut self) -> ControlFlow<()> {
ControlFlow::Break(())
fn extend(&mut self, s: MoveSet) -> ControlFlow<()> {
match s {
MoveSet::EnPassant { .. } => ControlFlow::Break(()),
_ => ControlFlow::Continue(()),
}
}
}
let mut moves = MoveGenImpl {
@ -761,11 +771,7 @@ pub(crate) trait MoveGen<S> {
}
#[inline]
fn is_check(&mut self) -> ControlFlow<S> {
ControlFlow::Continue(())
}
#[inline]
fn en_passant_is_legal(&mut self) -> ControlFlow<S> {
fn checkers(&mut self, _b: Bitboard) -> ControlFlow<S> {
ControlFlow::Continue(())
}
#[inline]
@ -829,6 +835,8 @@ impl Position {
| x & theirs.bishop()
| y & theirs.rook();
moves.checkers(checkers)?;
if moves.roles(Role::King) && global_mask_from.contains(king_square) {
let attacked = {
let blockers = blockers ^ ours.king();
@ -887,7 +895,6 @@ impl Position {
}
if checkers.len() > 1 {
moves.is_check()?;
return ControlFlow::Continue(());
}
@ -971,7 +978,6 @@ impl Position {
& candidates
& (!pinned | lookup::segment(king_square, to))
{
moves.en_passant_is_legal()?;
moves.extend(MoveSet::EnPassant { from, to })?;
}
}
@ -1006,7 +1012,6 @@ impl Position {
}
if checker.is_some() {
moves.is_check()?;
return ControlFlow::Continue(());
}
@ -1233,8 +1238,8 @@ impl MateMoveGenImpl {
}
impl MoveGen<Infallible> for MateMoveGenImpl {
#[inline]
fn is_check(&mut self) -> ControlFlow<Infallible> {
self.is_check = true;
fn checkers(&mut self, b: Bitboard) -> ControlFlow<Infallible> {
self.is_check = !b.is_empty();
ControlFlow::Continue(())
}
#[inline]