[board] Move position builders into a builders module
Move PositionBuilder to position_builder.rs and export it from the builders module.
This commit is contained in:
parent
1a907844d6
commit
4a5ae8ec59
2 changed files with 22 additions and 3 deletions
|
@ -1,83 +0,0 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
use super::{flags::Flags, piece_sets::PieceBitBoards};
|
||||
use crate::{
|
||||
piece::{PlacedPiece, Shape},
|
||||
square::Rank,
|
||||
BitBoard, Color, MakeMoveError, Move, Piece, Position, Square,
|
||||
};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Builder {
|
||||
player_to_move: Color,
|
||||
flags: Flags,
|
||||
pieces: BTreeMap<Square, Piece>,
|
||||
kings: [Square; 2],
|
||||
}
|
||||
|
||||
impl Builder {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn place_piece(&mut self, piece: PlacedPiece) -> &mut Self {
|
||||
let square = piece.square();
|
||||
|
||||
if piece.shape() == Shape::King {
|
||||
let color_index: usize = piece.color() as usize;
|
||||
self.pieces.remove(&self.kings[color_index]);
|
||||
self.kings[color_index] = square;
|
||||
}
|
||||
|
||||
self.pieces.insert(square, piece.piece());
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(&self) -> Position {
|
||||
let pieces = PieceBitBoards::from_iter(
|
||||
self.pieces
|
||||
.iter()
|
||||
.map(PlacedPiece::from)
|
||||
.filter(Self::is_piece_placement_valid),
|
||||
);
|
||||
|
||||
Position::new(self.player_to_move, self.flags, pieces, None)
|
||||
}
|
||||
}
|
||||
|
||||
impl Builder {
|
||||
fn is_piece_placement_valid(piece: &PlacedPiece) -> bool {
|
||||
if piece.shape() == Shape::Pawn {
|
||||
// Pawns cannot be placed on the first (back) rank of their side,
|
||||
// and cannot be placed on the final rank without a promotion.
|
||||
let rank = piece.square().rank();
|
||||
return rank != Rank::One && rank != Rank::Eight;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Builder {
|
||||
fn default() -> Self {
|
||||
let pieces = BTreeMap::from_iter([
|
||||
(
|
||||
Square::KING_STARTING_SQUARES[Color::White as usize],
|
||||
piece!(White King),
|
||||
),
|
||||
(
|
||||
Square::KING_STARTING_SQUARES[Color::Black as usize],
|
||||
piece!(Black King),
|
||||
),
|
||||
]);
|
||||
|
||||
Self {
|
||||
player_to_move: Color::White,
|
||||
flags: Flags::default(),
|
||||
pieces: pieces,
|
||||
kings: Square::KING_STARTING_SQUARES,
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue