Compare commits

...

2 commits

Author SHA1 Message Date
Bruno BELANYI b3222276ab Improve 'board::BitboardIterator'
All checks were successful
continuous-integration/drone/push Build is passing
* Accurate 'size_hint'.
* Exact size.
* Fused iterator (keeps returning 'None' after returning 'None' once).
2022-07-18 18:21:16 +02:00
Bruno BELANYI 7c8dce8f49 Add 'Color::{forward,backward}_direction' 2022-07-18 17:37:24 +02:00
2 changed files with 26 additions and 1 deletions

View file

@ -14,4 +14,14 @@ impl Iterator for BitboardIterator {
Some(crate::board::Square::from_index(lsb))
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let size = self.0.count_ones() as usize;
(size, Some(size))
}
}
impl ExactSizeIterator for BitboardIterator {}
impl std::iter::FusedIterator for BitboardIterator {}

View file

@ -1,4 +1,4 @@
use super::Rank;
use super::{Direction, Rank};
/// An enum representing the color of a player.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
@ -65,6 +65,21 @@ impl Color {
Self::Black => Rank::Second,
}
}
/// Which way do pawns advance for this color.
#[inline(always)]
pub fn forward_direction(self) -> Direction {
match self {
Self::White => Direction::North,
Self::Black => Direction::South,
}
}
/// Which way do the opponent's pawns advance for this color.
#[inline(always)]
pub fn backward_direction(self) -> Direction {
(!self).forward_direction()
}
}
impl std::ops::Not for Color {