2023-12-19 11:13:06 -08:00
|
|
|
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
|
2024-01-13 11:15:09 -08:00
|
|
|
use super::{flags::Flags, Pieces};
|
2024-01-06 16:24:46 -08:00
|
|
|
use crate::{
|
|
|
|
bitboard::BitBoard,
|
|
|
|
moves::Moves,
|
|
|
|
piece::{Color, Piece, PiecePlacementError, PlacedPiece, Shape},
|
|
|
|
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
|
|
|
|
2024-01-08 14:41:54 -08:00
|
|
|
/// A lateral side of the board relative to the player. Kingside is the side the
|
|
|
|
/// player's king starts on. Queenside is the side of the board the player's
|
|
|
|
/// queen starts on.
|
|
|
|
pub(crate) enum BoardSide {
|
|
|
|
King,
|
|
|
|
Queen,
|
|
|
|
}
|
|
|
|
|
2024-01-12 22:30:00 -08:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! position {
|
|
|
|
[$($color:ident $shape:ident on $square:ident,)*] => {
|
|
|
|
Position::from_iter([
|
|
|
|
$(piece!($color $shape on $square)),*
|
|
|
|
].into_iter())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
|
2024-01-08 14:41:54 -08:00
|
|
|
/// Position flags indicating various bits of game state. The flags are as
|
|
|
|
/// follows:
|
|
|
|
///
|
|
|
|
/// 0. white can castle king-side
|
|
|
|
/// 1. white can castle queen-side
|
|
|
|
/// 2. black can castle king-side
|
|
|
|
/// 3. black can castle queen-side
|
2024-01-13 11:15:09 -08:00
|
|
|
flags: Flags,
|
2024-01-08 14:41:54 -08:00
|
|
|
|
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,
|
2024-01-13 11:15:09 -08:00
|
|
|
flags: Default::default(),
|
2024-01-08 14:41:54 -08:00
|
|
|
pieces_per_color: [BitBoard::empty(); 2],
|
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-28 21:42:18 -07:00
|
|
|
BitBoard::new(0x00FF000000000000),
|
|
|
|
BitBoard::new(0x4200000000000000),
|
|
|
|
BitBoard::new(0x2400000000000000),
|
|
|
|
BitBoard::new(0x8100000000000000),
|
|
|
|
BitBoard::new(0x1000000000000000),
|
|
|
|
BitBoard::new(0x0800000000000000),
|
2023-12-20 11:45:12 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
let black_pieces = [
|
2023-12-28 21:42:18 -07:00
|
|
|
BitBoard::new(0xFF00),
|
|
|
|
BitBoard::new(0x0042),
|
|
|
|
BitBoard::new(0x0024),
|
|
|
|
BitBoard::new(0x0081),
|
|
|
|
BitBoard::new(0x0010),
|
|
|
|
BitBoard::new(0x0008),
|
2023-12-20 11:45:12 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
Position {
|
2023-12-26 11:22:40 -07:00
|
|
|
color_to_move: Color::White,
|
2024-01-13 11:15:09 -08:00
|
|
|
flags: Default::default(),
|
2023-12-20 11:45:12 -08:00
|
|
|
pieces_per_color: [
|
2024-01-06 16:24:46 -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],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 14:41:54 -08:00
|
|
|
/// Returns true if the player has the right to castle on the given side of
|
|
|
|
/// the board.
|
|
|
|
///
|
|
|
|
/// The right to castle on a particular side of the board is retained as
|
|
|
|
/// long as the player has not moved their king, or the rook on that side of
|
|
|
|
/// the board.
|
|
|
|
///
|
|
|
|
/// The following requirements must also be met:
|
|
|
|
///
|
|
|
|
/// 1. The spaces between the king and rook must be clear
|
|
|
|
/// 2. The king must not be in check
|
|
|
|
/// 3. In the course of castling on that side, the king must not pass
|
|
|
|
/// through a square that an enemy piece can see
|
|
|
|
pub(crate) fn player_has_right_to_castle(&self, color: Color, side: BoardSide) -> bool {
|
2024-01-13 11:15:09 -08:00
|
|
|
self.flags.player_has_right_to_castle(color, side)
|
2024-01-08 14:41:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set_player_has_right_to_castle_flag(&mut self, color: Color, side: BoardSide) {
|
2024-01-13 11:15:09 -08:00
|
|
|
self.flags.set_player_has_right_to_castle_flag(color, side);
|
2024-01-08 14:41:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_player_has_right_to_castle_flag(&mut self, color: Color, side: BoardSide) {
|
2024-01-13 11:15:09 -08:00
|
|
|
self.flags
|
|
|
|
.clear_player_has_right_to_castle_flag(color, side);
|
2024-01-08 14:41:54 -08:00
|
|
|
}
|
|
|
|
|
2024-01-06 16:24:46 -08: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
|
|
|
|
2024-01-06 16:24:46 -08: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
|
|
|
}
|
|
|
|
|
2024-01-06 16:24:46 -08:00
|
|
|
type_bb.place_piece_at(square);
|
2023-12-28 21:38:07 -07:00
|
|
|
|
2023-12-29 09:17:33 -08:00
|
|
|
let color_bb = &mut self.bitboard_for_color_mut(piece.color());
|
2024-01-06 16:24:46 -08:00
|
|
|
color_bb.place_piece_at(square);
|
2023-12-23 09:31:41 -07:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-12-31 11:34:04 -08:00
|
|
|
pub fn move_generator(&self, color: Color) -> Moves {
|
|
|
|
Moves::new(self, color)
|
2023-12-29 08:59:13 -08:00
|
|
|
}
|
|
|
|
|
2023-12-29 08:31:58 -08:00
|
|
|
/// Return a BitBoard representing the set of squares containing a piece.
|
2024-01-06 19:51:06 -08:00
|
|
|
#[inline]
|
2023-12-29 08:31:58 -08:00
|
|
|
pub(crate) fn occupied_squares(&self) -> BitBoard {
|
|
|
|
self.pieces_per_color[Color::White as usize] | self.pieces_per_color[Color::Black as usize]
|
|
|
|
}
|
|
|
|
|
2023-12-31 11:34:04 -08:00
|
|
|
/// Return a BitBoard representing the set of squares containing a piece.
|
|
|
|
/// This set is the inverse of `occupied_squares`.
|
2024-01-06 19:51:06 -08:00
|
|
|
#[inline]
|
2023-12-29 08:31:58 -08:00
|
|
|
pub(crate) fn empty_squares(&self) -> BitBoard {
|
2024-01-06 19:51:06 -08:00
|
|
|
!self.occupied_squares()
|
2023-12-29 08:31:58 -08:00
|
|
|
}
|
|
|
|
|
2024-01-06 16:24:46 -08:00
|
|
|
pub(crate) fn bitboard_for_piece(&self, piece: Piece) -> BitBoard {
|
|
|
|
self.pieces_per_type[piece.color() as usize][piece.shape() as usize]
|
2023-12-23 09:31:41 -07:00
|
|
|
}
|
|
|
|
|
2024-01-06 16:24:46 -08:00
|
|
|
fn bitboard_for_piece_mut(&mut self, piece: Piece) -> &mut BitBoard {
|
2023-12-29 09:17:33 -08: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
|
|
|
|
2024-01-06 16:24:46 -08:00
|
|
|
pub(crate) fn bitboard_for_color(&self, color: Color) -> BitBoard {
|
|
|
|
self.pieces_per_color[color as usize]
|
2023-12-28 21:38:07 -07:00
|
|
|
}
|
|
|
|
|
2023-12-29 08:31:58 -08:00
|
|
|
fn bitboard_for_color_mut(&mut self, color: Color) -> &mut BitBoard {
|
2023-12-28 21:38:07 -07:00
|
|
|
&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() {
|
2023-12-31 11:40:54 -08:00
|
|
|
let piece = Piece::new(color, *shape);
|
2023-12-27 08:31:02 -07:00
|
|
|
let bb = self.bitboard_for_piece(piece);
|
2024-01-06 16:24:46 -08:00
|
|
|
if bb.has_piece_at(sq) {
|
2023-12-26 21:34:01 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-11 08:18:31 -08:00
|
|
|
impl Default for Position {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::empty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-12 22:30:00 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2024-01-06 16:45:13 -08:00
|
|
|
let square = Square::E4;
|
2023-12-23 09:31:41 -07:00
|
|
|
|
|
|
|
position
|
2024-01-06 16:24:46 -08:00
|
|
|
.place_piece(piece, square)
|
2023-12-23 09:31:41 -07:00
|
|
|
.expect("Unable to place white queen on e4");
|
|
|
|
|
2023-12-28 21:38:07 -07:00
|
|
|
assert_eq!(
|
2023-12-29 09:17:33 -08:00
|
|
|
position.bitboard_for_color(piece.color()).clone(),
|
2023-12-28 21:42:18 -07:00
|
|
|
BitBoard::new(1 << 28)
|
2023-12-28 21:38:07 -07:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
position.bitboard_for_piece(piece).clone(),
|
2023-12-28 21:42:18 -07:00
|
|
|
BitBoard::new(1 << 28)
|
2023-12-28 21:38:07 -07:00
|
|
|
);
|
|
|
|
|
2023-12-23 09:31:41 -07:00
|
|
|
position
|
2024-01-06 16:24:46 -08:00
|
|
|
.place_piece(piece, square)
|
2023-12-23 09:31:41 -07:00
|
|
|
.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
|
|
|
}
|