2024-01-24 17:08:27 -08:00
|
|
|
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
|
2024-01-28 15:43:47 -08:00
|
|
|
use crate::{Move, MoveBuilder};
|
2024-01-24 09:18:12 -08:00
|
|
|
use chessfriend_bitboard::BitBoard;
|
2024-01-28 15:43:47 -08:00
|
|
|
use chessfriend_core::{Color, Piece, PlacedPiece, Shape};
|
2024-01-05 18:29:46 -08:00
|
|
|
|
2024-01-28 15:43:47 -08:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
2024-01-05 18:29:46 -08:00
|
|
|
struct BitBoardSet {
|
|
|
|
quiet: BitBoard,
|
|
|
|
captures: BitBoard,
|
|
|
|
}
|
|
|
|
|
2024-01-22 08:20:38 -08:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2024-01-05 18:29:46 -08:00
|
|
|
struct MoveListSet {
|
|
|
|
quiet: Vec<Move>,
|
|
|
|
captures: Vec<Move>,
|
|
|
|
}
|
|
|
|
|
2024-01-22 08:20:38 -08:00
|
|
|
impl MoveListSet {
|
|
|
|
pub fn contains(&self, mv: &Move) -> bool {
|
|
|
|
self.quiet.contains(mv) || self.captures.contains(mv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 18:29:46 -08:00
|
|
|
/// A set of moves for a piece on the board.
|
2024-01-22 08:20:38 -08:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub(crate) struct MoveSet {
|
2024-01-05 18:29:46 -08:00
|
|
|
piece: PlacedPiece,
|
|
|
|
bitboards: BitBoardSet,
|
|
|
|
move_lists: MoveListSet,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MoveSet {
|
|
|
|
pub(super) fn new(piece: PlacedPiece) -> MoveSet {
|
|
|
|
MoveSet {
|
|
|
|
piece,
|
2024-01-28 15:43:47 -08:00
|
|
|
bitboards: BitBoardSet::default(),
|
2024-01-05 18:29:46 -08:00
|
|
|
move_lists: MoveListSet {
|
|
|
|
quiet: Vec::new(),
|
|
|
|
captures: Vec::new(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-28 15:43:47 -08:00
|
|
|
pub(super) fn quiet_moves(mut self, bitboard: BitBoard) -> MoveSet {
|
2024-01-05 18:29:46 -08:00
|
|
|
self.bitboards.quiet = bitboard;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-01-28 15:43:47 -08:00
|
|
|
pub(super) fn capture_moves(mut self, bitboard: BitBoard) -> MoveSet {
|
2024-01-05 18:29:46 -08:00
|
|
|
self.bitboards.captures = bitboard;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a BitBoard representing all possible moves.
|
|
|
|
pub(super) fn bitboard(&self) -> BitBoard {
|
|
|
|
self.bitboards.captures | self.bitboards.quiet
|
|
|
|
}
|
|
|
|
|
2024-01-28 15:43:47 -08:00
|
|
|
pub(super) fn moves(&self) -> impl Iterator<Item = Move> + '_ {
|
|
|
|
let piece = self.piece.piece();
|
|
|
|
let from_square = self.piece.square();
|
|
|
|
|
|
|
|
self.bitboards
|
|
|
|
.quiet
|
|
|
|
.occupied_squares()
|
|
|
|
.map(move |to_square| MoveBuilder::new(*piece, from_square, to_square).build())
|
|
|
|
.chain(
|
|
|
|
self.bitboards
|
|
|
|
.captures
|
|
|
|
.occupied_squares()
|
|
|
|
.map(move |to_square| {
|
|
|
|
MoveBuilder::new(*piece, from_square, to_square)
|
|
|
|
.capturing(PlacedPiece::new(
|
|
|
|
Piece::new(Color::White, Shape::Pawn),
|
|
|
|
to_square,
|
|
|
|
))
|
|
|
|
.build()
|
|
|
|
}),
|
|
|
|
)
|
2024-01-05 18:29:46 -08:00
|
|
|
}
|
|
|
|
}
|