202 lines
5.9 KiB
Rust
202 lines
5.9 KiB
Rust
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
use super::Pieces;
|
|
use crate::{
|
|
bitboard::BitBoard,
|
|
moves::Moves,
|
|
piece::{Color, Piece, PiecePlacementError, PlacedPiece, Shape},
|
|
Square,
|
|
};
|
|
use std::fmt;
|
|
use std::fmt::Write;
|
|
|
|
#[derive(Clone, Eq, Hash, PartialEq)]
|
|
pub struct Position {
|
|
color_to_move: Color,
|
|
|
|
/// 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],
|
|
}
|
|
|
|
impl Position {
|
|
pub fn empty() -> Position {
|
|
Position {
|
|
color_to_move: Color::White,
|
|
pieces_per_color: [BitBoard::empty(), BitBoard::empty()],
|
|
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(),
|
|
],
|
|
],
|
|
}
|
|
}
|
|
|
|
/// Return a starting position.
|
|
pub fn starting() -> Position {
|
|
let white_pieces = [
|
|
BitBoard::new(0x00FF000000000000),
|
|
BitBoard::new(0x4200000000000000),
|
|
BitBoard::new(0x2400000000000000),
|
|
BitBoard::new(0x8100000000000000),
|
|
BitBoard::new(0x1000000000000000),
|
|
BitBoard::new(0x0800000000000000),
|
|
];
|
|
|
|
let black_pieces = [
|
|
BitBoard::new(0xFF00),
|
|
BitBoard::new(0x0042),
|
|
BitBoard::new(0x0024),
|
|
BitBoard::new(0x0081),
|
|
BitBoard::new(0x0010),
|
|
BitBoard::new(0x0008),
|
|
];
|
|
|
|
Position {
|
|
color_to_move: Color::White,
|
|
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],
|
|
}
|
|
}
|
|
|
|
pub fn place_piece(&mut self, piece: Piece, square: Square) -> Result<(), PiecePlacementError> {
|
|
let type_bb = self.bitboard_for_piece_mut(piece);
|
|
|
|
if type_bb.has_piece_at(square) {
|
|
return Err(PiecePlacementError::ExistsOnSquare);
|
|
}
|
|
|
|
type_bb.place_piece_at(square);
|
|
|
|
let color_bb = &mut self.bitboard_for_color_mut(piece.color());
|
|
color_bb.place_piece_at(square);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn move_generator(&self, color: Color) -> Moves {
|
|
Moves::new(self, color)
|
|
}
|
|
|
|
/// Return a BitBoard representing the set of squares containing a piece.
|
|
pub(crate) fn occupied_squares(&self) -> BitBoard {
|
|
self.pieces_per_color[Color::White as usize] | self.pieces_per_color[Color::Black as usize]
|
|
}
|
|
|
|
/// Return a BitBoard representing the set of squares containing a piece.
|
|
/// This set is the inverse of `occupied_squares`.
|
|
pub(crate) fn empty_squares(&self) -> BitBoard {
|
|
!(self.pieces_per_color[Color::White as usize]
|
|
| self.pieces_per_color[Color::Black as usize])
|
|
}
|
|
|
|
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.has_piece_at(sq) {
|
|
return Some(PlacedPiece::new(piece, sq));
|
|
}
|
|
}
|
|
}
|
|
|
|
None
|
|
}
|
|
|
|
pub fn pieces(&self, color: Color) -> Pieces {
|
|
Pieces::new(&self, color)
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for Position {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
let mut output = String::new();
|
|
|
|
output.push_str("Position {\n");
|
|
write!(output, " color_to_move: {:?},\n", self.color_to_move)?;
|
|
|
|
output.push_str(" pieces_per_color: [\n");
|
|
for bb in self.pieces_per_color {
|
|
write!(output, " {bb:?},\n")?;
|
|
}
|
|
output.push_str(" ],\n");
|
|
|
|
output.push_str(" pieces_per_type: [\n");
|
|
for color_bbs in self.pieces_per_type {
|
|
output.push_str(" [\n");
|
|
for bb in color_bbs {
|
|
write!(output, " {bb:?},\n")?;
|
|
}
|
|
output.push_str(" ],\n");
|
|
}
|
|
output.push_str(" ],\n}");
|
|
|
|
write!(f, "{}", output)
|
|
}
|
|
}
|
|
|
|
#[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?!");
|
|
}
|
|
}
|