2023-12-19 11:13:06 -08:00
|
|
|
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
|
2023-12-26 11:25:27 -07:00
|
|
|
use super::Pieces;
|
2023-12-20 11:45:12 -08:00
|
|
|
use crate::bitboard::BitBoard;
|
2023-12-26 21:34:01 -07:00
|
|
|
use crate::piece::{Color, Piece, PiecePlacementError, PlacedPiece, Shape};
|
2023-12-26 11:25:27 -07:00
|
|
|
use crate::Square;
|
2023-12-20 11:45:12 -08:00
|
|
|
use std::fmt;
|
2023-12-26 09:17:57 -07:00
|
|
|
use std::fmt::Write;
|
2023-12-20 11:45:12 -08:00
|
|
|
|
2023-12-26 09:17:57 -07:00
|
|
|
#[derive(Clone, Eq, Hash, PartialEq)]
|
2023-12-19 11:13:06 -08:00
|
|
|
pub struct Position {
|
2023-12-26 11:22:40 -07:00
|
|
|
color_to_move: Color,
|
|
|
|
|
2023-12-19 11:13:06 -08:00
|
|
|
/// 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 {
|
2023-12-26 11:25:27 -07:00
|
|
|
pub fn empty() -> Position {
|
2023-12-19 11:13:06 -08:00
|
|
|
Position {
|
2023-12-26 11:22:40 -07:00
|
|
|
color_to_move: Color::White,
|
2023-12-22 08:50:03 -08:00
|
|
|
pieces_per_color: [BitBoard::empty(), BitBoard::empty()],
|
2023-12-20 11:45:12 -08:00
|
|
|
pieces_per_type: [
|
|
|
|
[
|
2023-12-22 08:50:03 -08:00
|
|
|
BitBoard::empty(),
|
|
|
|
BitBoard::empty(),
|
|
|
|
BitBoard::empty(),
|
|
|
|
BitBoard::empty(),
|
|
|
|
BitBoard::empty(),
|
|
|
|
BitBoard::empty(),
|
2023-12-20 11:45:12 -08:00
|
|
|
],
|
|
|
|
[
|
2023-12-22 08:50:03 -08:00
|
|
|
BitBoard::empty(),
|
|
|
|
BitBoard::empty(),
|
|
|
|
BitBoard::empty(),
|
|
|
|
BitBoard::empty(),
|
|
|
|
BitBoard::empty(),
|
|
|
|
BitBoard::empty(),
|
2023-12-20 11:45:12 -08:00
|
|
|
],
|
|
|
|
],
|
2023-12-19 11:13:06 -08:00
|
|
|
}
|
|
|
|
}
|
2023-12-20 11:45:12 -08:00
|
|
|
|
|
|
|
/// Return a starting position.
|
2023-12-27 07:59:05 -07:00
|
|
|
pub fn starting() -> Position {
|
2023-12-20 11:45:12 -08:00
|
|
|
let white_pieces = [
|
2023-12-22 08:50:03 -08:00
|
|
|
BitBoard::from_bit_field(0x00FF000000000000),
|
|
|
|
BitBoard::from_bit_field(0x4200000000000000),
|
|
|
|
BitBoard::from_bit_field(0x2400000000000000),
|
|
|
|
BitBoard::from_bit_field(0x8100000000000000),
|
|
|
|
BitBoard::from_bit_field(0x1000000000000000),
|
2023-12-27 07:59:05 -07:00
|
|
|
BitBoard::from_bit_field(0x0800000000000000),
|
2023-12-20 11:45:12 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
let black_pieces = [
|
2023-12-22 08:50:03 -08:00
|
|
|
BitBoard::from_bit_field(0xFF00),
|
|
|
|
BitBoard::from_bit_field(0x0042),
|
|
|
|
BitBoard::from_bit_field(0x0024),
|
|
|
|
BitBoard::from_bit_field(0x0081),
|
|
|
|
BitBoard::from_bit_field(0x0010),
|
2023-12-27 07:59:05 -07:00
|
|
|
BitBoard::from_bit_field(0x0008),
|
2023-12-20 11:45:12 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
Position {
|
2023-12-26 11:22:40 -07:00
|
|
|
color_to_move: Color::White,
|
2023-12-20 11:45:12 -08:00
|
|
|
pieces_per_color: [
|
2023-12-22 08:50:03 -08:00
|
|
|
white_pieces.iter().fold(BitBoard::empty(), |a, b| a | b),
|
|
|
|
black_pieces.iter().fold(BitBoard::empty(), |a, b| a | b),
|
2023-12-20 11:45:12 -08:00
|
|
|
],
|
|
|
|
pieces_per_type: [white_pieces, black_pieces],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-26 11:25:27 -07:00
|
|
|
pub fn place_piece(
|
|
|
|
&mut self,
|
|
|
|
piece: &Piece,
|
|
|
|
square: &Square,
|
|
|
|
) -> Result<(), PiecePlacementError> {
|
2023-12-28 21:38:07 -07:00
|
|
|
let type_bb = self.bitboard_for_piece_mut(piece);
|
2023-12-23 09:31:41 -07:00
|
|
|
|
2023-12-28 21:38:07 -07:00
|
|
|
if type_bb.has_piece_at(&square) {
|
2023-12-26 11:20:01 -07:00
|
|
|
return Err(PiecePlacementError::ExistsOnSquare);
|
2023-12-23 09:31:41 -07:00
|
|
|
}
|
|
|
|
|
2023-12-28 21:38:07 -07:00
|
|
|
type_bb.place_piece_at(&square);
|
|
|
|
|
|
|
|
let color_bb = &mut self.bitboard_for_color_mut(piece.color);
|
|
|
|
color_bb.place_piece_at(&square);
|
2023-12-23 09:31:41 -07:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-12-28 21:38:07 -07:00
|
|
|
pub(crate) fn bitboard_for_piece(&self, piece: Piece) -> &BitBoard {
|
2023-12-23 20:14:24 -07:00
|
|
|
&self.pieces_per_type[piece.color as usize][piece.shape as usize]
|
2023-12-23 09:31:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn bitboard_for_piece_mut(&mut self, piece: &Piece) -> &mut BitBoard {
|
2023-12-23 20:14:24 -07:00
|
|
|
&mut self.pieces_per_type[piece.color as usize][piece.shape as usize]
|
2023-12-23 09:31:41 -07:00
|
|
|
}
|
2023-12-26 09:17:57 -07:00
|
|
|
|
2023-12-28 21:38:07 -07:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2023-12-26 21:34:01 -07:00
|
|
|
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);
|
2023-12-27 08:31:02 -07:00
|
|
|
let bb = self.bitboard_for_piece(piece);
|
2023-12-26 21:34:01 -07:00
|
|
|
if bb.has_piece_at(&sq) {
|
|
|
|
return Some(PlacedPiece::new(piece, sq));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2023-12-26 11:25:27 -07:00
|
|
|
pub fn pieces(&self, color: Color) -> Pieces {
|
2023-12-26 09:19:38 -07:00
|
|
|
Pieces::new(&self, color)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-26 09:17:57 -07:00
|
|
|
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");
|
|
|
|
}
|
2023-12-27 08:00:39 -07:00
|
|
|
output.push_str(" ],\n}");
|
2023-12-26 09:17:57 -07:00
|
|
|
|
|
|
|
write!(f, "{}", output)
|
|
|
|
}
|
2023-12-20 11:45:12 -08:00
|
|
|
}
|
|
|
|
|
2023-12-23 09:31:41 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2023-12-26 09:16:55 -07:00
|
|
|
use crate::piece::Shape;
|
2023-12-23 09:31:41 -07:00
|
|
|
|
|
|
|
#[test]
|
2023-12-28 21:38:07 -07:00
|
|
|
fn place_piece() {
|
2023-12-23 09:31:41 -07:00
|
|
|
let mut position = Position::empty();
|
|
|
|
|
2023-12-26 09:16:55 -07:00
|
|
|
let piece = Piece::new(Color::White, Shape::Queen);
|
2023-12-28 21:38:07 -07:00
|
|
|
let square = Square::e4();
|
2023-12-23 09:31:41 -07:00
|
|
|
|
|
|
|
position
|
|
|
|
.place_piece(&piece, &square)
|
|
|
|
.expect("Unable to place white queen on e4");
|
|
|
|
|
2023-12-28 21:38:07 -07:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
|
2023-12-23 09:31:41 -07:00
|
|
|
position
|
|
|
|
.place_piece(&piece, &square)
|
|
|
|
.expect_err("Placed white queen on e4 a second time?!");
|
2023-12-20 11:45:12 -08:00
|
|
|
}
|
2023-12-19 11:13:06 -08:00
|
|
|
}
|