misc
This commit is contained in:
parent
faccfbc1c5
commit
a6a00fc819
3 changed files with 97 additions and 94 deletions
155
src/position.rs
155
src/position.rs
|
|
@ -62,14 +62,6 @@ pub struct Position {
|
|||
|
||||
const MAX_LEGAL_MOVES: usize = 218;
|
||||
|
||||
impl std::fmt::Debug for Position {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
f.debug_tuple("Position")
|
||||
.field(&self.as_setup().to_string())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Position {
|
||||
/// Returns the initial position of a chess game.
|
||||
///
|
||||
|
|
@ -484,6 +476,14 @@ impl Position {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Position {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
f.debug_tuple("Position")
|
||||
.field(&self.as_setup().to_string())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
/// A legal move.
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Move<'l> {
|
||||
|
|
@ -657,7 +657,7 @@ impl<'l> Move<'l> {
|
|||
},
|
||||
suffix: {
|
||||
let pos = self.make();
|
||||
let mut visitor = MateCollector::new();
|
||||
let mut visitor = MateVisitorImpl::new();
|
||||
pos.generate_moves(&mut visitor);
|
||||
visitor.is_check.then(|| match visitor.is_mate {
|
||||
true => SanSuffix::Checkmate,
|
||||
|
|
@ -976,11 +976,6 @@ impl Position {
|
|||
| d.knight(king_square) & theirs.knight()
|
||||
| x & theirs.bishop()
|
||||
| y & theirs.rook();
|
||||
let blockers_x_ray = blockers & !(x | y);
|
||||
let pinned = (d.bishop(king_square, blockers_x_ray) & theirs.bishop()
|
||||
| d.rook(king_square, blockers_x_ray) & theirs.rook())
|
||||
.map(|sq| d.segment(king_square, sq))
|
||||
.reduce_or();
|
||||
|
||||
if visitor.roles(Role::King) && global_mask_from.contains(king_square) {
|
||||
let attacked = {
|
||||
|
|
@ -1058,6 +1053,12 @@ impl Position {
|
|||
return;
|
||||
}
|
||||
|
||||
let blockers_x_ray = blockers & !(x | y);
|
||||
let pinned = (d.bishop(king_square, blockers_x_ray) & theirs.bishop()
|
||||
| d.rook(king_square, blockers_x_ray) & theirs.rook())
|
||||
.map(|sq| d.segment(king_square, sq))
|
||||
.reduce_or();
|
||||
|
||||
let checker = checkers.first();
|
||||
let block_check = checker
|
||||
.map(|checker| d.segment(king_square, checker))
|
||||
|
|
@ -1091,7 +1092,7 @@ impl Position {
|
|||
to,
|
||||
role: Role::Pawn,
|
||||
}));
|
||||
visitor.moves(WithPromotion::new((targets & promotion).map(|to| {
|
||||
visitor.moves(MoveAndPromote::new((targets & promotion).map(|to| {
|
||||
RawMove {
|
||||
kind: MoveType::PawnAdvancePromotion,
|
||||
from: unsafe { to.trans_unchecked(!forward) },
|
||||
|
|
@ -1113,7 +1114,7 @@ impl Position {
|
|||
to,
|
||||
role: Role::Pawn,
|
||||
}));
|
||||
visitor.moves(WithPromotion::new((targets & promotion).map(|to| {
|
||||
visitor.moves(MoveAndPromote::new((targets & promotion).map(|to| {
|
||||
RawMove {
|
||||
kind: MoveType::PawnAttackPromotion,
|
||||
from: unsafe { to.trans_unchecked(!kside) },
|
||||
|
|
@ -1135,7 +1136,7 @@ impl Position {
|
|||
to,
|
||||
role: Role::Pawn,
|
||||
}));
|
||||
visitor.moves(WithPromotion::new((targets & promotion).map(|to| {
|
||||
visitor.moves(MoveAndPromote::new((targets & promotion).map(|to| {
|
||||
RawMove {
|
||||
kind: MoveType::PawnAttackPromotion,
|
||||
from: unsafe { to.trans_unchecked(!qside) },
|
||||
|
|
@ -1295,63 +1296,6 @@ impl Position {
|
|||
}
|
||||
}
|
||||
|
||||
struct WithPromotion<I: Iterator<Item = RawMove> + ExactSizeIterator + FusedIterator> {
|
||||
inner: I,
|
||||
cur: std::mem::MaybeUninit<RawMove>,
|
||||
role: Role,
|
||||
}
|
||||
impl<I> WithPromotion<I>
|
||||
where
|
||||
I: Iterator<Item = RawMove> + ExactSizeIterator + FusedIterator,
|
||||
{
|
||||
#[inline]
|
||||
fn new(inner: I) -> Self {
|
||||
Self {
|
||||
inner,
|
||||
cur: std::mem::MaybeUninit::uninit(),
|
||||
role: Role::King,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<I> Iterator for WithPromotion<I>
|
||||
where
|
||||
I: Iterator<Item = RawMove> + ExactSizeIterator + FusedIterator,
|
||||
{
|
||||
type Item = RawMove;
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<RawMove> {
|
||||
if self.role == Role::King {
|
||||
self.cur.write(self.inner.next()?);
|
||||
self.role = Role::Knight;
|
||||
}
|
||||
let raw = unsafe { self.cur.assume_init() };
|
||||
let res = RawMove {
|
||||
role: self.role,
|
||||
..raw
|
||||
};
|
||||
self.role = unsafe { Role::transmute((self.role as u8).unchecked_add(1)) };
|
||||
Some(res)
|
||||
}
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let len = self.len();
|
||||
(len, Some(len))
|
||||
}
|
||||
}
|
||||
impl<I> FusedIterator for WithPromotion<I> where
|
||||
I: Iterator<Item = RawMove> + ExactSizeIterator + FusedIterator
|
||||
{
|
||||
}
|
||||
impl<I> ExactSizeIterator for WithPromotion<I>
|
||||
where
|
||||
I: Iterator<Item = RawMove> + ExactSizeIterator + FusedIterator,
|
||||
{
|
||||
#[inline]
|
||||
fn len(&self) -> usize {
|
||||
unsafe { self.inner.len().unchecked_mul(4) }
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn aux_play_normal(setup: &mut Setup, role: Role, from: Square, target: Square) {
|
||||
let from = from.bitboard();
|
||||
|
|
@ -1458,11 +1402,11 @@ fn aux_play_castle(setup: &mut Setup, side: CastlingSide) {
|
|||
setup.castling_rights.unset(setup.turn, CastlingSide::Long);
|
||||
}
|
||||
|
||||
struct MateCollector {
|
||||
struct MateVisitorImpl {
|
||||
is_check: bool,
|
||||
is_mate: bool,
|
||||
}
|
||||
impl MateCollector {
|
||||
impl MateVisitorImpl {
|
||||
#[inline]
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
|
|
@ -1471,7 +1415,7 @@ impl MateCollector {
|
|||
}
|
||||
}
|
||||
}
|
||||
impl Visitor for MateCollector {
|
||||
impl Visitor for MateVisitorImpl {
|
||||
#[inline]
|
||||
fn is_check(&mut self) {
|
||||
self.is_check = true;
|
||||
|
|
@ -1486,3 +1430,60 @@ impl Visitor for MateCollector {
|
|||
self.is_mate &= iter.len() == 0;
|
||||
}
|
||||
}
|
||||
|
||||
struct MoveAndPromote<I: Iterator<Item = RawMove> + ExactSizeIterator + FusedIterator> {
|
||||
inner: I,
|
||||
cur: std::mem::MaybeUninit<RawMove>,
|
||||
role: Role,
|
||||
}
|
||||
impl<I> MoveAndPromote<I>
|
||||
where
|
||||
I: Iterator<Item = RawMove> + ExactSizeIterator + FusedIterator,
|
||||
{
|
||||
#[inline]
|
||||
fn new(inner: I) -> Self {
|
||||
Self {
|
||||
inner,
|
||||
cur: std::mem::MaybeUninit::uninit(),
|
||||
role: Role::King,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<I> Iterator for MoveAndPromote<I>
|
||||
where
|
||||
I: Iterator<Item = RawMove> + ExactSizeIterator + FusedIterator,
|
||||
{
|
||||
type Item = RawMove;
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<RawMove> {
|
||||
if self.role == Role::King {
|
||||
self.cur.write(self.inner.next()?);
|
||||
self.role = Role::Knight;
|
||||
}
|
||||
let raw = unsafe { self.cur.assume_init() };
|
||||
let res = RawMove {
|
||||
role: self.role,
|
||||
..raw
|
||||
};
|
||||
self.role = unsafe { Role::transmute((self.role as u8).unchecked_add(1)) };
|
||||
Some(res)
|
||||
}
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let len = self.len();
|
||||
(len, Some(len))
|
||||
}
|
||||
}
|
||||
impl<I> FusedIterator for MoveAndPromote<I> where
|
||||
I: Iterator<Item = RawMove> + ExactSizeIterator + FusedIterator
|
||||
{
|
||||
}
|
||||
impl<I> ExactSizeIterator for MoveAndPromote<I>
|
||||
where
|
||||
I: Iterator<Item = RawMove> + ExactSizeIterator + FusedIterator,
|
||||
{
|
||||
#[inline]
|
||||
fn len(&self) -> usize {
|
||||
unsafe { self.inner.len().unchecked_mul(4) }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue