From b842b3520b798f10b6939b440cd3678ea6e3d0e3 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 5 Jan 2024 18:29:46 -0800 Subject: [PATCH] [board] Implement a MoveSet struct This struct represents a set of moves for a PlacedPiece --- board/src/moves/mod.rs | 3 ++ board/src/moves/move_set.rs | 68 +++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 board/src/moves/move_set.rs diff --git a/board/src/moves/mod.rs b/board/src/moves/mod.rs index 4289649..5839911 100644 --- a/board/src/moves/mod.rs +++ b/board/src/moves/mod.rs @@ -4,7 +4,10 @@ mod king; mod knight; mod r#move; mod move_generator; +mod move_set; mod pawn; pub use move_generator::Moves; pub use r#move::Move; + +pub(self) use move_set::MoveSet; diff --git a/board/src/moves/move_set.rs b/board/src/moves/move_set.rs new file mode 100644 index 0000000..b3330b9 --- /dev/null +++ b/board/src/moves/move_set.rs @@ -0,0 +1,68 @@ +use crate::{bitboard::BitBoard, piece::PlacedPiece, Move}; + +struct BitBoardSet { + quiet: BitBoard, + captures: BitBoard, +} + +struct MoveListSet { + quiet: Vec, + captures: Vec, +} + +/// A set of moves for a piece on the board. +pub(super) struct MoveSet { + piece: PlacedPiece, + bitboards: BitBoardSet, + move_lists: MoveListSet, +} + +impl MoveSet { + pub(super) fn new(piece: PlacedPiece) -> MoveSet { + MoveSet { + piece, + bitboards: BitBoardSet { + quiet: BitBoard::empty(), + captures: BitBoard::empty(), + }, + move_lists: MoveListSet { + quiet: Vec::new(), + captures: Vec::new(), + }, + } + } + + pub(super) fn quiet_moves( + mut self, + bitboard: BitBoard, + move_list: impl Iterator, + ) -> MoveSet { + self.bitboards.quiet = bitboard; + self.move_lists.quiet = move_list.collect(); + + self + } + + pub(super) fn capture_moves( + mut self, + bitboard: BitBoard, + move_list: impl Iterator, + ) -> MoveSet { + self.bitboards.captures = bitboard; + self.move_lists.captures = move_list.collect(); + + self + } + + /// Return a BitBoard representing all possible moves. + pub(super) fn bitboard(&self) -> BitBoard { + self.bitboards.captures | self.bitboards.quiet + } + + pub(super) fn moves(&self) -> impl Iterator { + self.move_lists + .captures + .iter() + .chain(self.move_lists.quiet.iter()) + } +}