[board] Populate the color bitboards when placing pieces
This commit is contained in:
parent
e1e27fc668
commit
af53eb932c
1 changed files with 26 additions and 8 deletions
|
@ -79,18 +79,21 @@ impl Position {
|
||||||
piece: &Piece,
|
piece: &Piece,
|
||||||
square: &Square,
|
square: &Square,
|
||||||
) -> Result<(), PiecePlacementError> {
|
) -> Result<(), PiecePlacementError> {
|
||||||
let bitboard = self.bitboard_for_piece_mut(piece);
|
let type_bb = self.bitboard_for_piece_mut(piece);
|
||||||
|
|
||||||
if bitboard.has_piece_at(&square) {
|
if type_bb.has_piece_at(&square) {
|
||||||
return Err(PiecePlacementError::ExistsOnSquare);
|
return Err(PiecePlacementError::ExistsOnSquare);
|
||||||
}
|
}
|
||||||
|
|
||||||
bitboard.place_piece_at(&square);
|
type_bb.place_piece_at(&square);
|
||||||
|
|
||||||
|
let color_bb = &mut self.bitboard_for_color_mut(piece.color);
|
||||||
|
color_bb.place_piece_at(&square);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn bitboard_for_piece(&self, piece: Piece) -> &BitBoard {
|
pub(crate) fn bitboard_for_piece(&self, piece: Piece) -> &BitBoard {
|
||||||
&self.pieces_per_type[piece.color as usize][piece.shape as usize]
|
&self.pieces_per_type[piece.color as usize][piece.shape as usize]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,6 +101,14 @@ impl Position {
|
||||||
&mut self.pieces_per_type[piece.color as usize][piece.shape as usize]
|
&mut self.pieces_per_type[piece.color as usize][piece.shape as usize]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(self) fn bitboard_for_color(&self, color: Color) -> &BitBoard {
|
||||||
|
&self.pieces_per_color[color as usize]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(self) 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> {
|
pub fn piece_on_square(&self, sq: Square) -> Option<PlacedPiece> {
|
||||||
for color in Color::iter() {
|
for color in Color::iter() {
|
||||||
for shape in Shape::iter() {
|
for shape in Shape::iter() {
|
||||||
|
@ -150,20 +161,27 @@ mod tests {
|
||||||
use crate::piece::Shape;
|
use crate::piece::Shape;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn place_piece_at() {
|
fn place_piece() {
|
||||||
let mut position = Position::empty();
|
let mut position = Position::empty();
|
||||||
|
|
||||||
let piece = Piece::new(Color::White, Shape::Queen);
|
let piece = Piece::new(Color::White, Shape::Queen);
|
||||||
let square = Square::from_algebraic_str("e4").expect("Unable to get e4 square");
|
let square = Square::e4();
|
||||||
|
|
||||||
position
|
position
|
||||||
.place_piece(&piece, &square)
|
.place_piece(&piece, &square)
|
||||||
.expect("Unable to place white queen on e4");
|
.expect("Unable to place white queen on e4");
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
position.bitboard_for_color(piece.color).clone(),
|
||||||
|
BitBoard::from_bit_field(1 << 28)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
position.bitboard_for_piece(piece).clone(),
|
||||||
|
BitBoard::from_bit_field(1 << 28)
|
||||||
|
);
|
||||||
|
|
||||||
position
|
position
|
||||||
.place_piece(&piece, &square)
|
.place_piece(&piece, &square)
|
||||||
.expect_err("Placed white queen on e4 a second time?!");
|
.expect_err("Placed white queen on e4 a second time?!");
|
||||||
|
|
||||||
println!("{:?}", position);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue