[board] Implement iter() on board
This iterator yields (Square, Piece) tuples for all pieces on the board.
This commit is contained in:
parent
f60cb8cf69
commit
724a98c2e2
3 changed files with 42 additions and 0 deletions
|
@ -90,6 +90,10 @@ impl Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
|
pub fn iter(&self) -> impl Iterator<Item = (Square, Piece)> {
|
||||||
|
self.pieces.iter()
|
||||||
|
}
|
||||||
|
|
||||||
/// A [`BitBoard`] of squares occupied by pieces of all colors.
|
/// A [`BitBoard`] of squares occupied by pieces of all colors.
|
||||||
pub fn occupancy(&self) -> BitBoard {
|
pub fn occupancy(&self) -> BitBoard {
|
||||||
self.pieces.occpuancy()
|
self.pieces.occpuancy()
|
||||||
|
|
|
@ -60,6 +60,10 @@ impl PieceSet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn iter(&self) -> impl Iterator<Item = (Square, Piece)> {
|
||||||
|
self.mailbox.iter()
|
||||||
|
}
|
||||||
|
|
||||||
/// A [`BitBoard`] representing all the pieces on the board.
|
/// A [`BitBoard`] representing all the pieces on the board.
|
||||||
pub fn occpuancy(&self) -> BitBoard {
|
pub fn occpuancy(&self) -> BitBoard {
|
||||||
self.color_occupancy
|
self.color_occupancy
|
||||||
|
|
|
@ -22,6 +22,13 @@ impl Mailbox {
|
||||||
pub fn remove(&mut self, square: Square) {
|
pub fn remove(&mut self, square: Square) {
|
||||||
self.0[square as usize] = None;
|
self.0[square as usize] = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn iter(&self) -> impl Iterator<Item = (Square, Piece)> {
|
||||||
|
Square::ALL
|
||||||
|
.into_iter()
|
||||||
|
.zip(self.0)
|
||||||
|
.filter_map(|(square, piece)| piece.map(|piece| (square, piece)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Mailbox {
|
impl Default for Mailbox {
|
||||||
|
@ -45,3 +52,30 @@ impl FromIterator<(Square, Piece)> for Mailbox {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use chessfriend_core::piece;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn iter_returns_all_pieces() {
|
||||||
|
let mut mailbox = Mailbox::new();
|
||||||
|
mailbox.set(piece!(White Queen), Square::C3);
|
||||||
|
mailbox.set(piece!(White Rook), Square::H8);
|
||||||
|
mailbox.set(piece!(Black Bishop), Square::E4);
|
||||||
|
mailbox.set(piece!(Black Pawn), Square::F6);
|
||||||
|
|
||||||
|
let pieces = mailbox.iter().collect::<HashSet<_>>();
|
||||||
|
assert_eq!(
|
||||||
|
pieces,
|
||||||
|
HashSet::from([
|
||||||
|
(Square::C3, piece!(White Queen)),
|
||||||
|
(Square::H8, piece!(White Rook)),
|
||||||
|
(Square::E4, piece!(Black Bishop)),
|
||||||
|
(Square::F6, piece!(Black Pawn)),
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue