[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
|
@ -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