[board] Implement a PositionBuilder; refactor piece bitboards into a PieceBitBoards struct
This makes Position immutable, even if declared mut. I think this will also make it easier to build positions going forward. Moving to a PieceBitBoards struct also required a lot of places that return owned BitBoards to return refs. This is basically fine except for adding a few more & here and there. This was a huge refactoring project. All the move generators and a bunch of BitBoard stuff needed to be updated.
This commit is contained in:
parent
939fac80d7
commit
24cce95a88
17 changed files with 472 additions and 381 deletions
83
board/src/position/builders.rs
Normal file
83
board/src/position/builders.rs
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
// 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -42,7 +42,7 @@ impl<'a> fmt::Display for DiagramFormatter<'a> {
|
|||
mod tests {
|
||||
use super::*;
|
||||
use crate::piece::{Color, Piece};
|
||||
use crate::Position;
|
||||
use crate::{Position, PositionBuilder};
|
||||
|
||||
#[test]
|
||||
fn empty() {
|
||||
|
|
@ -53,13 +53,9 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn one_king() {
|
||||
let mut pos = Position::empty();
|
||||
pos.place_piece(
|
||||
Piece::king(Color::Black),
|
||||
Square::from_algebraic_str("h3").expect("h3"),
|
||||
)
|
||||
.expect("Unable to place piece");
|
||||
|
||||
let pos = PositionBuilder::new()
|
||||
.place_piece(piece!(Black King on H3))
|
||||
.build();
|
||||
let diagram = DiagramFormatter(&pos);
|
||||
println!("{}", diagram);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,13 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
mod builders;
|
||||
mod diagram_formatter;
|
||||
mod flags;
|
||||
mod piece_sets;
|
||||
mod pieces;
|
||||
mod position;
|
||||
|
||||
pub use builders::Builder as PositionBuilder;
|
||||
pub use diagram_formatter::DiagramFormatter;
|
||||
pub use pieces::Pieces;
|
||||
pub use position::Position;
|
||||
|
|
|
|||
149
board/src/position/piece_sets.rs
Normal file
149
board/src/position/piece_sets.rs
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
use crate::{
|
||||
piece::{Piece, PlacedPiece},
|
||||
BitBoard, Color, Square,
|
||||
};
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum PlacePieceStrategy {
|
||||
Replace,
|
||||
PreserveExisting,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum PlacePieceError {
|
||||
ExisitingPiece,
|
||||
}
|
||||
|
||||
impl Default for PlacePieceStrategy {
|
||||
fn default() -> Self {
|
||||
Self::Replace
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
|
||||
pub(super) struct PieceBitBoards {
|
||||
by_color: ByColor,
|
||||
by_color_and_shape: ByColorAndShape,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
|
||||
struct ByColor(BitBoard, [BitBoard; 2]);
|
||||
|
||||
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
|
||||
struct ByColorAndShape([[BitBoard; 6]; 2]);
|
||||
|
||||
impl PieceBitBoards {
|
||||
pub(super) fn new(pieces: [[BitBoard; 6]; 2]) -> Self {
|
||||
let white_pieces = pieces[Color::White as usize]
|
||||
.iter()
|
||||
.fold(BitBoard::empty(), std::ops::BitOr::bitor);
|
||||
let black_pieces = pieces[Color::White as usize]
|
||||
.iter()
|
||||
.fold(BitBoard::empty(), std::ops::BitOr::bitor);
|
||||
let all_pieces = white_pieces | black_pieces;
|
||||
|
||||
Self {
|
||||
by_color: ByColor(all_pieces, [white_pieces, black_pieces]),
|
||||
by_color_and_shape: ByColorAndShape(pieces),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn all_pieces(&self) -> &BitBoard {
|
||||
self.by_color.all()
|
||||
}
|
||||
|
||||
pub(super) fn all_pieces_of_color(&self, color: Color) -> &BitBoard {
|
||||
self.by_color.bitboard(color)
|
||||
}
|
||||
|
||||
pub(super) fn bitboard_for_color(&self, color: Color) -> &BitBoard {
|
||||
self.by_color.bitboard(color)
|
||||
}
|
||||
|
||||
pub(super) fn bitboard_for_color_mut(&mut self, color: Color) -> &mut BitBoard {
|
||||
self.by_color.bitboard_mut(color)
|
||||
}
|
||||
|
||||
pub(super) fn bitboard_for_piece(&self, piece: Piece) -> &BitBoard {
|
||||
self.by_color_and_shape.bitboard_for_piece(piece)
|
||||
}
|
||||
|
||||
pub(super) fn bitboard_for_piece_mut(&mut self, piece: Piece) -> &mut BitBoard {
|
||||
self.by_color_and_shape.bitboard_for_piece_mut(piece)
|
||||
}
|
||||
|
||||
pub(super) fn place_piece(&mut self, piece: PlacedPiece) -> Result<(), PlacePieceError> {
|
||||
self.place_piece_with_strategy(piece, Default::default())
|
||||
}
|
||||
|
||||
pub(super) fn place_piece_with_strategy(
|
||||
&mut self,
|
||||
piece: PlacedPiece,
|
||||
strategy: PlacePieceStrategy,
|
||||
) -> Result<(), PlacePieceError> {
|
||||
let color = piece.color();
|
||||
let square = piece.square();
|
||||
|
||||
if self.by_color.bitboard(color).is_set(piece.square()) {
|
||||
match strategy {
|
||||
PlacePieceStrategy::Replace => todo!(),
|
||||
PlacePieceStrategy::PreserveExisting => {
|
||||
return Err(PlacePieceError::ExisitingPiece)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.by_color.set_square(square, color);
|
||||
self.bitboard_for_piece_mut(piece.piece())
|
||||
.set_square(square);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromIterator<PlacedPiece> for PieceBitBoards {
|
||||
fn from_iter<T: IntoIterator<Item = PlacedPiece>>(iter: T) -> Self {
|
||||
let mut pieces = Self::default();
|
||||
|
||||
for piece in iter {
|
||||
let _ = pieces.place_piece(piece);
|
||||
}
|
||||
|
||||
pieces
|
||||
}
|
||||
}
|
||||
|
||||
impl ByColor {
|
||||
fn all(&self) -> &BitBoard {
|
||||
&self.0
|
||||
}
|
||||
|
||||
pub(super) fn bitboard(&self, color: Color) -> &BitBoard {
|
||||
&self.1[color as usize]
|
||||
}
|
||||
|
||||
pub(super) fn bitboard_mut(&mut self, color: Color) -> &mut BitBoard {
|
||||
&mut self.1[color as usize]
|
||||
}
|
||||
|
||||
fn set_square(&mut self, square: Square, color: Color) {
|
||||
self.0.set_square(square);
|
||||
self.1[color as usize].set_square(square)
|
||||
}
|
||||
}
|
||||
|
||||
impl ByColorAndShape {
|
||||
fn bitboard_for_piece(&self, piece: Piece) -> &BitBoard {
|
||||
&self.0[piece.color() as usize][piece.shape() as usize]
|
||||
}
|
||||
|
||||
fn bitboard_for_piece_mut(&mut self, piece: Piece) -> &mut BitBoard {
|
||||
&mut self.0[piece.color() as usize][piece.shape() as usize]
|
||||
}
|
||||
|
||||
fn set_square(&mut self, square: Square, piece: Piece) {
|
||||
self.bitboard_for_piece_mut(piece).set_square(square);
|
||||
}
|
||||
}
|
||||
|
|
@ -38,7 +38,7 @@ impl<'a> Iterator for Pieces<'a> {
|
|||
}
|
||||
|
||||
let mut current_shape: Option<Shape> = None;
|
||||
let mut next_nonempty_bitboard: Option<BitBoard> = None;
|
||||
let mut next_nonempty_bitboard: Option<&BitBoard> = None;
|
||||
|
||||
while let Some(shape) = self.shape_iterator.next() {
|
||||
let piece = Piece::new(self.color, *shape);
|
||||
|
|
@ -76,15 +76,10 @@ impl<'a> Iterator for Pieces<'a> {
|
|||
mod tests {
|
||||
use super::*;
|
||||
use crate::piece::{Color, Piece, Shape};
|
||||
use crate::Position;
|
||||
use crate::Square;
|
||||
use crate::{Position, PositionBuilder};
|
||||
use std::collections::HashSet;
|
||||
|
||||
fn place_piece_in_position(pos: &mut Position, piece: Piece, sq: Square) {
|
||||
pos.place_piece(piece, sq)
|
||||
.expect("Unable to place {piece:?} queen on {sq}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty() {
|
||||
let pos = Position::empty();
|
||||
|
|
@ -94,40 +89,45 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn one() {
|
||||
let sq = Square::E4;
|
||||
|
||||
let mut pos = Position::empty();
|
||||
place_piece_in_position(&mut pos, Piece::new(Color::White, Shape::Queen), Square::E4);
|
||||
println!("{:?}", &pos);
|
||||
let pos = PositionBuilder::new()
|
||||
.place_piece(piece!(White Queen on E4))
|
||||
.build();
|
||||
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(), Some(piece!(White Queen on E4)));
|
||||
assert_eq!(pieces.next(), Some(piece!(White King on E1)));
|
||||
assert_eq!(pieces.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_pieces() {
|
||||
let mut pos = Position::empty();
|
||||
|
||||
place_piece_in_position(&mut pos, Piece::new(Color::White, Shape::Queen), Square::E4);
|
||||
place_piece_in_position(&mut pos, Piece::new(Color::White, Shape::King), Square::E1);
|
||||
place_piece_in_position(&mut pos, Piece::new(Color::White, Shape::Pawn), Square::B2);
|
||||
place_piece_in_position(&mut pos, Piece::new(Color::White, Shape::Pawn), Square::C2);
|
||||
|
||||
println!("{:?}", &pos);
|
||||
let pos = PositionBuilder::new()
|
||||
.place_piece(piece!(White Queen on E4))
|
||||
.place_piece(piece!(White King on A1))
|
||||
.place_piece(piece!(White Pawn on B2))
|
||||
.place_piece(piece!(White Pawn on C2))
|
||||
.build();
|
||||
println!("{}", crate::position::DiagramFormatter::new(&pos));
|
||||
|
||||
let expected_placed_pieces = HashSet::from([
|
||||
PlacedPiece::new(Piece::new(Color::White, Shape::Queen), Square::E4),
|
||||
PlacedPiece::new(Piece::new(Color::White, Shape::King), Square::E1),
|
||||
PlacedPiece::new(Piece::new(Color::White, Shape::Pawn), Square::B2),
|
||||
PlacedPiece::new(Piece::new(Color::White, Shape::Pawn), Square::C2),
|
||||
piece!(White Queen on E4),
|
||||
piece!(White King on A1),
|
||||
piece!(White Pawn on B2),
|
||||
piece!(White Pawn on C2),
|
||||
]);
|
||||
|
||||
let placed_pieces = HashSet::from_iter(pos.pieces(Color::White));
|
||||
|
||||
assert_eq!(placed_pieces, expected_placed_pieces);
|
||||
assert_eq!(
|
||||
placed_pieces,
|
||||
expected_placed_pieces,
|
||||
"{:#?}",
|
||||
placed_pieces
|
||||
.symmetric_difference(&expected_placed_pieces)
|
||||
.into_iter()
|
||||
.map(|pp| format!("{}", pp))
|
||||
.collect::<Vec<String>>()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
use super::{flags::Flags, Pieces};
|
||||
use super::{flags::Flags, piece_sets::PieceBitBoards, Pieces};
|
||||
use crate::{
|
||||
move_generator::Moves,
|
||||
piece::{Color, Piece, PiecePlacementError, PlacedPiece, Shape},
|
||||
piece::{Color, Piece, PlacedPiece, Shape},
|
||||
sight::Sight,
|
||||
BitBoard, Square,
|
||||
BitBoard, Move, Square,
|
||||
};
|
||||
use std::cell::OnceCell;
|
||||
|
||||
|
|
@ -21,15 +21,8 @@ pub(crate) enum BoardSide {
|
|||
pub struct Position {
|
||||
color_to_move: Color,
|
||||
flags: Flags,
|
||||
|
||||
/// Composite bitboards for all the pieces of a particular color.
|
||||
pieces_per_color: [BitBoard; 2],
|
||||
|
||||
/// Bitboards representing positions of particular piece types per color.
|
||||
pieces_per_type: [[BitBoard; 6]; 2],
|
||||
|
||||
pieces: PieceBitBoards,
|
||||
en_passant_square: Option<Square>,
|
||||
|
||||
sight: [OnceCell<BitBoard>; 2],
|
||||
}
|
||||
|
||||
|
|
@ -38,32 +31,14 @@ impl Position {
|
|||
Position {
|
||||
color_to_move: Color::White,
|
||||
flags: Default::default(),
|
||||
pieces_per_color: [BitBoard::empty(); 2],
|
||||
pieces_per_type: [
|
||||
[
|
||||
BitBoard::empty(),
|
||||
BitBoard::empty(),
|
||||
BitBoard::empty(),
|
||||
BitBoard::empty(),
|
||||
BitBoard::empty(),
|
||||
BitBoard::empty(),
|
||||
],
|
||||
[
|
||||
BitBoard::empty(),
|
||||
BitBoard::empty(),
|
||||
BitBoard::empty(),
|
||||
BitBoard::empty(),
|
||||
BitBoard::empty(),
|
||||
BitBoard::empty(),
|
||||
],
|
||||
],
|
||||
pieces: PieceBitBoards::default(),
|
||||
en_passant_square: None,
|
||||
sight: [OnceCell::new(), OnceCell::new()],
|
||||
}
|
||||
}
|
||||
|
||||
/// Return a starting position.
|
||||
pub fn starting() -> Position {
|
||||
pub fn starting() -> Self {
|
||||
let white_pieces = [
|
||||
BitBoard::new(0x00FF000000000000),
|
||||
BitBoard::new(0x4200000000000000),
|
||||
|
|
@ -82,19 +57,19 @@ impl Position {
|
|||
BitBoard::new(0x0008),
|
||||
];
|
||||
|
||||
Position {
|
||||
Self {
|
||||
color_to_move: Color::White,
|
||||
flags: Default::default(),
|
||||
pieces_per_color: [
|
||||
white_pieces.iter().fold(BitBoard::empty(), |a, b| a | *b),
|
||||
black_pieces.iter().fold(BitBoard::empty(), |a, b| a | *b),
|
||||
],
|
||||
pieces_per_type: [white_pieces, black_pieces],
|
||||
flags: Flags::default(),
|
||||
pieces: PieceBitBoards::new([white_pieces, black_pieces]),
|
||||
en_passant_square: None,
|
||||
sight: [OnceCell::new(), OnceCell::new()],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn player_to_move(&self) -> Color {
|
||||
self.color_to_move
|
||||
}
|
||||
|
||||
/// Returns true if the player has the right to castle on the given side of
|
||||
/// the board.
|
||||
///
|
||||
|
|
@ -112,39 +87,28 @@ impl Position {
|
|||
self.flags.player_has_right_to_castle(color, side)
|
||||
}
|
||||
|
||||
pub fn place_piece(&mut self, piece: Piece, square: Square) -> Result<(), PiecePlacementError> {
|
||||
let type_bb = self.bitboard_for_piece_mut(piece);
|
||||
|
||||
if type_bb.is_set(square) {
|
||||
return Err(PiecePlacementError::ExistsOnSquare);
|
||||
}
|
||||
|
||||
type_bb.set_square(square);
|
||||
|
||||
self.bitboard_for_color_mut(piece.color())
|
||||
.set_square(square);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn moves(&self) -> Moves {
|
||||
Moves::new(self, self.color_to_move)
|
||||
}
|
||||
|
||||
/// Return a BitBoard representing the set of squares containing a piece.
|
||||
#[inline]
|
||||
pub(crate) fn occupied_squares(&self) -> BitBoard {
|
||||
self.pieces_per_color[Color::White as usize] | self.pieces_per_color[Color::Black as usize]
|
||||
pub(crate) fn occupied_squares(&self) -> &BitBoard {
|
||||
&self.pieces.all_pieces()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn friendly_pieces(&self) -> BitBoard {
|
||||
self.bitboard_for_color(self.color_to_move)
|
||||
pub(crate) fn friendly_pieces(&self) -> &BitBoard {
|
||||
self.pieces.all_pieces_of_color(self.color_to_move)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn opposing_pieces(&self) -> BitBoard {
|
||||
self.bitboard_for_color(self.color_to_move.other())
|
||||
pub(crate) fn opposing_pieces(&self) -> &BitBoard {
|
||||
self.pieces.all_pieces_of_color(self.color_to_move.other())
|
||||
}
|
||||
|
||||
pub(crate) fn all_pieces(&self) -> (&BitBoard, &BitBoard) {
|
||||
(self.friendly_pieces(), self.opposing_pieces())
|
||||
}
|
||||
|
||||
/// Return a BitBoard representing the set of squares containing a piece.
|
||||
|
|
@ -154,28 +118,11 @@ impl Position {
|
|||
!self.occupied_squares()
|
||||
}
|
||||
|
||||
pub(crate) fn bitboard_for_piece(&self, piece: Piece) -> BitBoard {
|
||||
self.pieces_per_type[piece.color() as usize][piece.shape() as usize]
|
||||
}
|
||||
|
||||
fn bitboard_for_piece_mut(&mut self, piece: Piece) -> &mut BitBoard {
|
||||
&mut self.pieces_per_type[piece.color() as usize][piece.shape() as usize]
|
||||
}
|
||||
|
||||
pub(crate) fn bitboard_for_color(&self, color: Color) -> BitBoard {
|
||||
self.pieces_per_color[color as usize]
|
||||
}
|
||||
|
||||
fn bitboard_for_color_mut(&mut self, color: Color) -> &mut BitBoard {
|
||||
&mut self.pieces_per_color[color as usize]
|
||||
}
|
||||
|
||||
pub fn piece_on_square(&self, sq: Square) -> Option<PlacedPiece> {
|
||||
for color in Color::iter() {
|
||||
for shape in Shape::iter() {
|
||||
let piece = Piece::new(color, *shape);
|
||||
let bb = self.bitboard_for_piece(piece);
|
||||
if bb.is_set(sq) {
|
||||
if self.pieces.bitboard_for_piece(piece).is_set(sq) {
|
||||
return Some(PlacedPiece::new(piece, sq));
|
||||
}
|
||||
}
|
||||
|
|
@ -206,11 +153,49 @@ impl Position {
|
|||
}
|
||||
|
||||
fn king_square(&self) -> Square {
|
||||
self.bitboard_for_piece(Piece::king(self.color_to_move))
|
||||
self.pieces
|
||||
.bitboard_for_piece(Piece::king(self.color_to_move))
|
||||
.occupied_squares()
|
||||
.next()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub(crate) fn move_is_legal(&self, mv: Move) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
// crate::position methods
|
||||
impl Position {
|
||||
pub(super) fn new(
|
||||
player_to_move: Color,
|
||||
flags: Flags,
|
||||
pieces: PieceBitBoards,
|
||||
en_passant_square: Option<Square>,
|
||||
) -> Self {
|
||||
Self {
|
||||
color_to_move: player_to_move,
|
||||
flags,
|
||||
en_passant_square,
|
||||
pieces,
|
||||
sight: [OnceCell::new(), OnceCell::new()],
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn flags(&self) -> Flags {
|
||||
self.flags
|
||||
}
|
||||
}
|
||||
|
||||
// crate methods
|
||||
impl Position {
|
||||
pub(crate) fn bitboard_for_color(&self, color: Color) -> &BitBoard {
|
||||
self.pieces.bitboard_for_color(color)
|
||||
}
|
||||
|
||||
pub(crate) fn bitboard_for_piece(&self, piece: Piece) -> &BitBoard {
|
||||
self.pieces.bitboard_for_piece(piece)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
@ -226,46 +211,9 @@ impl Default for Position {
|
|||
}
|
||||
}
|
||||
|
||||
impl FromIterator<PlacedPiece> for Position {
|
||||
fn from_iter<T: IntoIterator<Item = PlacedPiece>>(iter: T) -> Self {
|
||||
let mut position = Position::empty();
|
||||
for placed_piece in iter {
|
||||
_ = position.place_piece(placed_piece.piece(), placed_piece.square());
|
||||
}
|
||||
|
||||
position
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::piece::Shape;
|
||||
|
||||
#[test]
|
||||
fn place_piece() {
|
||||
let mut position = Position::empty();
|
||||
|
||||
let piece = Piece::new(Color::White, Shape::Queen);
|
||||
let square = Square::E4;
|
||||
|
||||
position
|
||||
.place_piece(piece, square)
|
||||
.expect("Unable to place white queen on e4");
|
||||
|
||||
assert_eq!(
|
||||
position.bitboard_for_color(piece.color()).clone(),
|
||||
BitBoard::new(1 << 28)
|
||||
);
|
||||
assert_eq!(
|
||||
position.bitboard_for_piece(piece).clone(),
|
||||
BitBoard::new(1 << 28)
|
||||
);
|
||||
|
||||
position
|
||||
.place_piece(piece, square)
|
||||
.expect_err("Placed white queen on e4 a second time?!");
|
||||
}
|
||||
use crate::position;
|
||||
|
||||
#[test]
|
||||
fn king_is_in_check() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue