[board] Replace Moves' separate properties with a Vec of Iterators

This makes iteration easier. Move to the next iterator if the current one returns None.
No tests for Moves yet though.
This commit is contained in:
Eryn Wells 2023-12-31 11:34:04 -08:00
parent c2c0af20fb
commit 878d771d86
3 changed files with 44 additions and 15 deletions

View file

@ -4,5 +4,5 @@ mod r#move;
mod move_generator; mod move_generator;
mod pawn; mod pawn;
pub use move_generator::MoveGenerator; pub use move_generator::Moves;
pub use r#move::Move; pub use r#move::Move;

View file

@ -1,20 +1,48 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use super::pawn::PawnMoveGenerator;
use super::Move;
use crate::piece::Color;
use crate::Position; use crate::Position;
use super::pawn::PawnMoveGenerator; pub struct Moves<'pos> {
position: &'pos Position,
pub struct MoveGenerator<'a> { color: Color,
pub(super) position: &'a Position, iterators: Vec<Box<dyn Iterator<Item = Move> + 'pos>>,
index: usize,
pawn_move_generator: PawnMoveGenerator<'a>,
} }
impl<'a> MoveGenerator<'a> { impl<'a> Moves<'a> {
pub fn new(position: &Position) -> MoveGenerator { pub fn new(position: &Position, color: Color) -> Moves {
MoveGenerator { Moves {
position, position,
pawn_move_generator: PawnMoveGenerator::new(position), color,
iterators: vec![
Box::new(PawnMoveGenerator::new(position, color)),
],
index: 0,
} }
} }
fn current_iterator(&mut self) -> &mut Box<dyn Iterator<Item = Move> + 'a> {
&mut self.iterators[self.index]
}
}
impl<'a> std::iter::Iterator for Moves<'a> {
type Item = Move;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.iterators.len() {
return None;
}
let next_move = self.current_iterator().next();
if next_move.is_none() && self.index < self.iterators.len() {
self.index += 1;
}
next_move
}
} }

View file

@ -2,7 +2,7 @@
use super::Pieces; use super::Pieces;
use crate::bitboard::BitBoard; use crate::bitboard::BitBoard;
use crate::moves::MoveGenerator; use crate::moves::Moves;
use crate::piece::{Color, Piece, PiecePlacementError, PlacedPiece, Shape}; use crate::piece::{Color, Piece, PiecePlacementError, PlacedPiece, Shape};
use crate::Square; use crate::Square;
use std::fmt; use std::fmt;
@ -94,8 +94,8 @@ impl Position {
Ok(()) Ok(())
} }
pub fn move_generator(&self) -> MoveGenerator { pub fn move_generator(&self, color: Color) -> Moves {
MoveGenerator::new(self) Moves::new(self, color)
} }
/// Return a BitBoard representing the set of squares containing a piece. /// Return a BitBoard representing the set of squares containing a piece.
@ -103,7 +103,8 @@ impl Position {
self.pieces_per_color[Color::White as usize] | self.pieces_per_color[Color::Black as usize] self.pieces_per_color[Color::White as usize] | self.pieces_per_color[Color::Black as usize]
} }
/// Return a BitBoard representing the set of squares containing a piece. This set is the inverse of `occupied_squares`. /// Return a BitBoard representing the set of squares containing a piece.
/// This set is the inverse of `occupied_squares`.
pub(crate) fn empty_squares(&self) -> BitBoard { pub(crate) fn empty_squares(&self) -> BitBoard {
!(self.pieces_per_color[Color::White as usize] !(self.pieces_per_color[Color::White as usize]
| self.pieces_per_color[Color::Black as usize]) | self.pieces_per_color[Color::Black as usize])