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::{
|
2024-01-17 08:40:09 -08:00
|
|
|
move_generator::Moves,
|
2024-01-06 16:24:46 -08:00
|
|
|
piece::{Color, Piece, PiecePlacementError, PlacedPiece, Shape},
|
2024-01-15 17:43:27 -08:00
|
|
|
sight::Sight,
|
2024-01-14 10:57:22 -08:00
|
|
|
BitBoard, Square,
|
2024-01-06 16:24:46 -08:00
|
|
|
};
|
2024-01-15 17:43:27 -08:00
|
|
|
use std::cell::OnceCell;
|
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-19 17:59:34 -08:00
|
|
|
#[derive(Clone, Debug, Eq, 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-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],
|
2024-01-15 16:03:06 -08:00
|
|
|
|
|
|
|
en_passant_square: Option<Square>,
|
2024-01-15 17:43:27 -08:00
|
|
|
|
|
|
|
sight: [OnceCell<BitBoard>; 2],
|
2023-12-19 11:13:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
],
|
|
|
|
],
|
2024-01-15 16:03:06 -08:00
|
|
|
en_passant_square: None,
|
2024-01-15 17:43:27 -08:00
|
|
|
sight: [OnceCell::new(), OnceCell::new()],
|
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-15 16:03:06 -08:00
|
|
|
en_passant_square: None,
|
2024-01-15 17:43:27 -08:00
|
|
|
sight: [OnceCell::new(), OnceCell::new()],
|
2023-12-20 11:45:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-15 17:42:27 -08:00
|
|
|
if type_bb.is_set(square) {
|
2023-12-26 11:20:01 -07:00
|
|
|
return Err(PiecePlacementError::ExistsOnSquare);
|
2023-12-23 09:31:41 -07:00
|
|
|
}
|
|
|
|
|
2024-01-15 17:16:14 -08:00
|
|
|
type_bb.set_square(square);
|
2023-12-28 21:38:07 -07:00
|
|
|
|
2024-01-17 08:46:41 -08:00
|
|
|
self.bitboard_for_color_mut(piece.color())
|
|
|
|
.set_square(square);
|
2023-12-23 09:31:41 -07:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-01-17 08:43:48 -08:00
|
|
|
pub fn moves(&self) -> Moves {
|
|
|
|
Moves::new(self, self.color_to_move)
|
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]
|
|
|
|
}
|
|
|
|
|
2024-01-15 16:03:06 -08:00
|
|
|
#[inline]
|
|
|
|
pub(crate) fn friendly_pieces(&self) -> BitBoard {
|
|
|
|
self.bitboard_for_color(self.color_to_move)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn opposing_pieces(&self) -> BitBoard {
|
|
|
|
self.bitboard_for_color(self.color_to_move.other())
|
|
|
|
}
|
|
|
|
|
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-15 17:42:27 -08:00
|
|
|
if bb.is_set(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-15 16:03:06 -08:00
|
|
|
|
|
|
|
pub fn en_passant_square(&self) -> Option<Square> {
|
|
|
|
self.en_passant_square
|
|
|
|
}
|
2024-01-15 17:43:27 -08:00
|
|
|
|
|
|
|
pub(crate) fn sight_of_player(&self, color: Color) -> BitBoard {
|
|
|
|
*self.sight[color as usize].get_or_init(|| {
|
|
|
|
self.pieces(color).fold(BitBoard::empty(), |acc, pp| {
|
|
|
|
acc | pp.sight_in_position(&self)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn is_king_in_check(&self) -> bool {
|
|
|
|
let sight_of_opposing_player = self.sight_of_player(self.color_to_move.other());
|
2024-01-17 08:48:23 -08:00
|
|
|
sight_of_opposing_player.is_set(self.king_square())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn king_square(&self) -> Square {
|
|
|
|
self.bitboard_for_piece(Piece::king(self.color_to_move))
|
2024-01-15 17:43:27 -08:00
|
|
|
.occupied_squares()
|
|
|
|
.next()
|
2024-01-17 08:48:23 -08:00
|
|
|
.unwrap()
|
|
|
|
}
|
2024-01-17 08:44:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
impl Position {
|
|
|
|
pub(crate) fn test_set_en_passant_square(&mut self, square: Square) {
|
|
|
|
self.en_passant_square = Some(square);
|
2024-01-15 17:43:27 -08:00
|
|
|
}
|
|
|
|
}
|
2023-12-26 09:19:38 -07:00
|
|
|
|
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-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
|
|
|
}
|
2024-01-15 17:43:27 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn king_is_in_check() {
|
|
|
|
let pos = position![
|
|
|
|
White King on E1,
|
|
|
|
Black Rook on E8,
|
|
|
|
];
|
|
|
|
assert!(pos.is_king_in_check());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn king_is_not_in_check() {
|
|
|
|
let pos = position![
|
|
|
|
White King on F1,
|
|
|
|
Black Rook on E8,
|
|
|
|
];
|
|
|
|
assert!(!pos.is_king_in_check());
|
|
|
|
}
|
2023-12-19 11:13:06 -08:00
|
|
|
}
|