2023-12-26 09:19:38 -07:00
|
|
|
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
|
2023-12-26 11:25:27 -07:00
|
|
|
use super::Position;
|
2023-12-26 09:19:38 -07:00
|
|
|
use crate::bitboard::BitBoard;
|
|
|
|
use crate::piece::{Color, Piece, PlacedPiece, Shape};
|
2023-12-26 11:25:27 -07:00
|
|
|
use crate::Square;
|
2023-12-26 09:19:38 -07:00
|
|
|
|
|
|
|
pub struct Pieces<'a> {
|
|
|
|
color: Color,
|
|
|
|
position: &'a Position,
|
|
|
|
|
|
|
|
current_shape: Option<Shape>,
|
|
|
|
|
2023-12-31 11:40:54 -08:00
|
|
|
shape_iterator: Box<dyn Iterator<Item = &'static Shape>>,
|
2023-12-26 09:19:38 -07:00
|
|
|
square_iterator: Option<Box<dyn Iterator<Item = Square>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Pieces<'a> {
|
|
|
|
pub(crate) fn new(position: &Position, color: Color) -> Pieces {
|
|
|
|
Pieces {
|
|
|
|
color,
|
|
|
|
position,
|
|
|
|
current_shape: None,
|
|
|
|
shape_iterator: Box::new(Shape::iter()),
|
|
|
|
square_iterator: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for Pieces<'a> {
|
|
|
|
type Item = PlacedPiece;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if let Some(square_iterator) = &mut self.square_iterator {
|
|
|
|
if let (Some(square), Some(shape)) = (square_iterator.next(), self.current_shape) {
|
|
|
|
return Some(PlacedPiece::new(Piece::new(self.color, shape), square));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut current_shape: Option<Shape> = None;
|
|
|
|
let mut next_nonempty_bitboard: Option<&BitBoard> = None;
|
|
|
|
|
|
|
|
while let Some(shape) = self.shape_iterator.next() {
|
2023-12-31 11:40:54 -08:00
|
|
|
let piece = Piece::new(self.color, *shape);
|
2023-12-26 09:19:38 -07:00
|
|
|
|
2023-12-27 08:31:02 -07:00
|
|
|
let bitboard = self.position.bitboard_for_piece(piece);
|
2023-12-26 09:19:38 -07:00
|
|
|
if bitboard.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
next_nonempty_bitboard = Some(bitboard);
|
2023-12-31 11:40:54 -08:00
|
|
|
current_shape = Some(*shape);
|
2023-12-26 09:19:38 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let (Some(bitboard), Some(shape)) = (next_nonempty_bitboard, current_shape) {
|
|
|
|
let mut square_iterator = bitboard.occupied_squares();
|
|
|
|
|
|
|
|
let mut next_placed_piece: Option<PlacedPiece> = None;
|
|
|
|
if let Some(square) = square_iterator.next() {
|
|
|
|
next_placed_piece = Some(PlacedPiece::new(Piece::new(self.color, shape), square));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.square_iterator = Some(Box::new(square_iterator));
|
|
|
|
self.current_shape = Some(shape);
|
|
|
|
|
|
|
|
return next_placed_piece;
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::piece::{Color, Piece, Shape};
|
2023-12-26 11:25:27 -07:00
|
|
|
use crate::Position;
|
|
|
|
use crate::Square;
|
|
|
|
use std::collections::HashSet;
|
2023-12-26 09:19:38 -07:00
|
|
|
|
|
|
|
fn square_at(sq: &str) -> Square {
|
2023-12-26 13:28:25 -07:00
|
|
|
Square::from_algebraic_str(sq).expect(sq)
|
2023-12-26 09:19:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn place_piece_in_position(pos: &mut Position, sq: &str, piece: Piece) {
|
|
|
|
pos.place_piece(&piece, &square_at(sq))
|
|
|
|
.expect("Unable to place {piece:?} queen on {sq}");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty() {
|
|
|
|
let pos = Position::empty();
|
|
|
|
let mut pieces = pos.pieces(Color::White);
|
|
|
|
assert_eq!(pieces.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn one() {
|
2023-12-26 13:28:25 -07:00
|
|
|
let sq = Square::from_algebraic_str("e4").expect("e4");
|
2023-12-26 09:19:38 -07:00
|
|
|
|
|
|
|
let mut pos = Position::empty();
|
|
|
|
pos.place_piece(&Piece::new(Color::White, Shape::Queen), &sq)
|
|
|
|
.expect("Unable to place white queen on e4");
|
|
|
|
println!("{:?}", &pos);
|
|
|
|
|
|
|
|
let mut pieces = pos.pieces(Color::White);
|
|
|
|
assert_eq!(
|
|
|
|
pieces.next(),
|
|
|
|
Some(PlacedPiece::new(Piece::new(Color::White, Shape::Queen), sq))
|
|
|
|
);
|
|
|
|
assert_eq!(pieces.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_pieces() {
|
|
|
|
let mut pos = Position::empty();
|
|
|
|
|
|
|
|
place_piece_in_position(&mut pos, "e4", Piece::new(Color::White, Shape::Queen));
|
|
|
|
place_piece_in_position(&mut pos, "e1", Piece::new(Color::White, Shape::King));
|
|
|
|
place_piece_in_position(&mut pos, "b2", Piece::new(Color::White, Shape::Pawn));
|
|
|
|
place_piece_in_position(&mut pos, "c2", Piece::new(Color::White, Shape::Pawn));
|
|
|
|
|
|
|
|
println!("{:?}", &pos);
|
|
|
|
|
|
|
|
let expected_placed_pieces = HashSet::from([
|
|
|
|
PlacedPiece::new(Piece::new(Color::White, Shape::Queen), square_at("e4")),
|
|
|
|
PlacedPiece::new(Piece::new(Color::White, Shape::King), square_at("e1")),
|
|
|
|
PlacedPiece::new(Piece::new(Color::White, Shape::Pawn), square_at("b2")),
|
|
|
|
PlacedPiece::new(Piece::new(Color::White, Shape::Pawn), square_at("c2")),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let placed_pieces = HashSet::from_iter(pos.pieces(Color::White));
|
|
|
|
|
|
|
|
assert_eq!(placed_pieces, expected_placed_pieces);
|
|
|
|
}
|
|
|
|
}
|