2023-12-19 11:13:06 -08:00
|
|
|
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
|
2024-01-19 18:08:41 -08:00
|
|
|
use super::{flags::Flags, piece_sets::PieceBitBoards, Pieces};
|
2024-01-06 16:24:46 -08:00
|
|
|
use crate::{
|
2024-01-17 08:40:09 -08:00
|
|
|
move_generator::Moves,
|
2024-01-19 18:08:41 -08:00
|
|
|
piece::{Color, Piece, PlacedPiece, Shape},
|
2024-01-19 18:11:27 -08:00
|
|
|
position::DiagramFormatter,
|
2024-01-21 09:05:42 -08:00
|
|
|
r#move::Castle,
|
2024-01-15 17:43:27 -08:00
|
|
|
sight::Sight,
|
2024-01-19 18:08:41 -08:00
|
|
|
BitBoard, Move, Square,
|
2024-01-06 16:24:46 -08:00
|
|
|
};
|
2024-01-19 18:11:27 -08:00
|
|
|
use std::{cell::OnceCell, fmt};
|
2023-12-20 11:45:12 -08:00
|
|
|
|
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-19 18:08:41 -08:00
|
|
|
pieces: PieceBitBoards,
|
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-19 18:08:41 -08:00
|
|
|
pieces: PieceBitBoards::default(),
|
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.
|
2024-01-19 18:08:41 -08:00
|
|
|
pub fn starting() -> Self {
|
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
|
|
|
];
|
|
|
|
|
2024-01-19 18:08:41 -08:00
|
|
|
Self {
|
2023-12-26 11:22:40 -07:00
|
|
|
color_to_move: Color::White,
|
2024-01-19 18:08:41 -08:00
|
|
|
flags: Flags::default(),
|
|
|
|
pieces: PieceBitBoards::new([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-19 18:08:41 -08:00
|
|
|
pub fn player_to_move(&self) -> Color {
|
|
|
|
self.color_to_move
|
|
|
|
}
|
|
|
|
|
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.
|
2024-01-21 09:05:42 -08:00
|
|
|
pub(crate) fn player_has_right_to_castle(&self, color: Color, castle: Castle) -> bool {
|
|
|
|
self.flags.player_has_right_to_castle(color, castle)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the player is able to castle on the given side of the board.
|
2024-01-08 14:41:54 -08:00
|
|
|
///
|
2024-01-21 09:05:42 -08:00
|
|
|
/// The following requirements must be met:
|
2024-01-08 14:41:54 -08:00
|
|
|
///
|
2024-01-21 09:05:42 -08:00
|
|
|
/// 1. The player must still have the right to castle on that side of the
|
|
|
|
/// board. The king and rook involved in the castle must not have moved.
|
2024-01-08 14:41:54 -08:00
|
|
|
/// 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
|
2024-01-21 09:05:42 -08:00
|
|
|
pub(crate) fn player_can_castle(&self, player: Color, castle: Castle) -> bool {
|
|
|
|
if !self.player_has_right_to_castle(player, castle.into()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
2024-01-08 14:41:54 -08:00
|
|
|
}
|
|
|
|
|
2024-01-21 09:23:39 -08:00
|
|
|
pub(crate) fn rook_for_castle(&self, player: Color, castle: Castle) -> Option<PlacedPiece> {
|
|
|
|
let square = match (player, castle) {
|
|
|
|
(Color::White, Castle::KingSide) => Square::H1,
|
|
|
|
(Color::White, Castle::QueenSide) => Square::A1,
|
|
|
|
(Color::Black, Castle::KingSide) => Square::H8,
|
|
|
|
(Color::Black, Castle::QueenSide) => Square::A8,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.piece_on_square(square)
|
|
|
|
}
|
|
|
|
|
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]
|
2024-01-19 18:08:41 -08:00
|
|
|
pub(crate) fn occupied_squares(&self) -> &BitBoard {
|
|
|
|
&self.pieces.all_pieces()
|
2023-12-29 08:31:58 -08:00
|
|
|
}
|
|
|
|
|
2024-01-15 16:03:06 -08:00
|
|
|
#[inline]
|
2024-01-19 18:08:41 -08:00
|
|
|
pub(crate) fn friendly_pieces(&self) -> &BitBoard {
|
|
|
|
self.pieces.all_pieces_of_color(self.color_to_move)
|
2024-01-15 16:03:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2024-01-19 18:08:41 -08:00
|
|
|
pub(crate) fn opposing_pieces(&self) -> &BitBoard {
|
|
|
|
self.pieces.all_pieces_of_color(self.color_to_move.other())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn all_pieces(&self) -> (&BitBoard, &BitBoard) {
|
|
|
|
(self.friendly_pieces(), self.opposing_pieces())
|
2024-01-15 16:03:06 -08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
2024-01-21 09:18:16 -08:00
|
|
|
if self.pieces.bitboard_for_piece(&piece).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 {
|
2024-01-19 18:08:41 -08:00
|
|
|
self.pieces
|
2024-01-21 09:18:16 -08:00
|
|
|
.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-19 18:08:41 -08:00
|
|
|
|
|
|
|
pub(crate) fn move_is_legal(&self, mv: Move) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// crate::position methods
|
|
|
|
impl Position {
|
|
|
|
pub(super) fn new(
|
|
|
|
player_to_move: Color,
|
|
|
|
flags: Flags,
|
|
|
|
pieces: PieceBitBoards,
|
|
|
|
en_passant_square: Option<Square>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
color_to_move: player_to_move,
|
|
|
|
flags,
|
|
|
|
en_passant_square,
|
|
|
|
pieces,
|
|
|
|
sight: [OnceCell::new(), OnceCell::new()],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn flags(&self) -> Flags {
|
|
|
|
self.flags
|
|
|
|
}
|
2024-01-21 09:18:16 -08:00
|
|
|
|
|
|
|
pub(super) fn piece_bitboards(&self) -> &PieceBitBoards {
|
|
|
|
&self.pieces
|
|
|
|
}
|
2024-01-19 18:08:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// crate methods
|
|
|
|
impl Position {
|
|
|
|
pub(crate) fn bitboard_for_color(&self, color: Color) -> &BitBoard {
|
|
|
|
self.pieces.bitboard_for_color(color)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn bitboard_for_piece(&self, piece: Piece) -> &BitBoard {
|
2024-01-21 09:18:16 -08:00
|
|
|
self.pieces.bitboard_for_piece(&piece)
|
2024-01-19 18:08:41 -08:00
|
|
|
}
|
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-19 18:11:27 -08:00
|
|
|
impl fmt::Display for Position {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", DiagramFormatter::new(self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-23 09:31:41 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-01-21 10:39:24 -08:00
|
|
|
use crate::{position, Castle, Color};
|
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());
|
|
|
|
}
|
2024-01-21 10:39:24 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rook_for_castle() {
|
|
|
|
let pos = position![
|
|
|
|
White King on E1,
|
|
|
|
White Rook on H1,
|
|
|
|
White Rook on A1,
|
|
|
|
];
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
pos.rook_for_castle(Color::White, Castle::KingSide),
|
|
|
|
Some(piece!(White Rook on H1))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
pos.rook_for_castle(Color::White, Castle::QueenSide),
|
|
|
|
Some(piece!(White Rook on A1))
|
|
|
|
);
|
|
|
|
}
|
2023-12-19 11:13:06 -08:00
|
|
|
}
|