2024-02-03 15:17:02 -08:00
|
|
|
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
|
|
|
|
use chessfriend_bitboard::BitBoard;
|
2024-02-25 08:51:23 -08:00
|
|
|
use chessfriend_core::{Color, Square};
|
2024-02-03 15:17:02 -08:00
|
|
|
|
2024-02-09 20:00:47 -08:00
|
|
|
#[repr(u8)]
|
2024-02-03 15:17:02 -08:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub enum Castle {
|
2024-02-09 20:00:47 -08:00
|
|
|
KingSide = 0,
|
|
|
|
QueenSide = 1,
|
2024-02-03 15:17:02 -08:00
|
|
|
}
|
|
|
|
|
2024-02-25 08:51:23 -08:00
|
|
|
pub struct Parameters {
|
2024-02-09 20:00:47 -08:00
|
|
|
/// Origin squares of the king and rook.
|
2024-04-25 13:28:24 -07:00
|
|
|
origin: Squares,
|
2024-02-09 20:00:47 -08:00
|
|
|
|
|
|
|
/// Target or destination squares for the king and rook.
|
2024-04-25 13:28:24 -07:00
|
|
|
target: Squares,
|
2024-02-09 20:00:47 -08:00
|
|
|
|
2024-07-13 08:08:48 -07:00
|
|
|
/// The set of squares that must be clear of any pieces in order to perform
|
|
|
|
/// this castle.
|
2024-04-25 13:28:24 -07:00
|
|
|
clear: BitBoard,
|
2024-02-09 20:00:47 -08:00
|
|
|
|
2024-07-13 08:08:48 -07:00
|
|
|
/// The set of squares that must not be attacked (i.e. visible to opposing
|
|
|
|
/// pieces) in order to perform this castle.
|
2024-04-25 13:28:24 -07:00
|
|
|
check: BitBoard,
|
2024-02-03 15:17:02 -08:00
|
|
|
}
|
|
|
|
|
2024-02-25 08:51:23 -08:00
|
|
|
impl Parameters {
|
|
|
|
pub fn king_origin_square(&self) -> Square {
|
2024-04-25 13:28:24 -07:00
|
|
|
self.origin.king
|
2024-02-25 08:51:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rook_origin_square(&self) -> Square {
|
2024-04-25 13:28:24 -07:00
|
|
|
self.origin.rook
|
2024-02-25 08:51:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn king_target_square(&self) -> Square {
|
2024-04-25 13:28:24 -07:00
|
|
|
self.target.king
|
2024-02-25 08:51:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rook_target_square(&self) -> Square {
|
2024-04-25 13:28:24 -07:00
|
|
|
self.target.rook
|
2024-02-25 08:51:23 -08:00
|
|
|
}
|
|
|
|
|
2024-07-13 08:08:48 -07:00
|
|
|
/// A [`BitBoard`] of the squares that must be clear of any piece in order
|
|
|
|
/// to perform this castle move.
|
2024-02-25 08:51:23 -08:00
|
|
|
pub fn clear_squares(&self) -> &BitBoard {
|
2024-04-25 13:28:24 -07:00
|
|
|
&self.clear
|
2024-02-25 08:51:23 -08:00
|
|
|
}
|
|
|
|
|
2024-07-13 08:08:48 -07:00
|
|
|
/// A [`BitBoard`] of the squares that must not be visible to opposing
|
|
|
|
/// pieces in order to perform this castle move.
|
2024-02-25 08:51:23 -08:00
|
|
|
pub fn check_squares(&self) -> &BitBoard {
|
2024-04-25 13:28:24 -07:00
|
|
|
&self.check
|
2024-02-25 08:51:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-03 15:17:02 -08:00
|
|
|
#[derive(Debug)]
|
2024-02-25 08:51:23 -08:00
|
|
|
struct Squares {
|
|
|
|
king: Square,
|
|
|
|
rook: Square,
|
2024-02-03 15:17:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Castle {
|
|
|
|
pub const ALL: [Castle; 2] = [Castle::KingSide, Castle::QueenSide];
|
|
|
|
|
2024-02-09 20:00:47 -08:00
|
|
|
/// Parameters for each castling move, organized by color and board-side.
|
2024-07-13 08:08:48 -07:00
|
|
|
const PARAMETERS: [[Parameters; 2]; Color::NUM] = [
|
2024-02-03 15:17:02 -08:00
|
|
|
[
|
2024-02-25 08:51:23 -08:00
|
|
|
Parameters {
|
2024-04-25 13:28:24 -07:00
|
|
|
origin: Squares {
|
2024-02-09 20:00:47 -08:00
|
|
|
king: Square::E1,
|
|
|
|
rook: Square::H1,
|
|
|
|
},
|
2024-04-25 13:28:24 -07:00
|
|
|
target: Squares {
|
2024-02-09 20:00:47 -08:00
|
|
|
king: Square::G1,
|
|
|
|
rook: Square::F1,
|
|
|
|
},
|
2024-04-25 13:28:24 -07:00
|
|
|
clear: BitBoard::new(0b0110_0000),
|
|
|
|
check: BitBoard::new(0b0111_0000),
|
2024-02-03 15:17:02 -08:00
|
|
|
},
|
2024-02-25 08:51:23 -08:00
|
|
|
Parameters {
|
2024-04-25 13:28:24 -07:00
|
|
|
origin: Squares {
|
2024-02-09 20:00:47 -08:00
|
|
|
king: Square::E1,
|
|
|
|
rook: Square::A1,
|
|
|
|
},
|
2024-04-25 13:28:24 -07:00
|
|
|
target: Squares {
|
2024-02-09 20:00:47 -08:00
|
|
|
king: Square::C1,
|
|
|
|
rook: Square::D1,
|
|
|
|
},
|
2024-04-25 13:28:24 -07:00
|
|
|
clear: BitBoard::new(0b0000_1110),
|
|
|
|
check: BitBoard::new(0b0001_1100),
|
2024-02-03 15:17:02 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
2024-02-25 08:51:23 -08:00
|
|
|
Parameters {
|
2024-04-25 13:28:24 -07:00
|
|
|
origin: Squares {
|
2024-02-09 20:00:47 -08:00
|
|
|
king: Square::E8,
|
|
|
|
rook: Square::H8,
|
|
|
|
},
|
2024-04-25 13:28:24 -07:00
|
|
|
target: Squares {
|
2024-02-09 20:00:47 -08:00
|
|
|
king: Square::G8,
|
|
|
|
rook: Square::F8,
|
|
|
|
},
|
2024-04-25 13:28:24 -07:00
|
|
|
clear: BitBoard::new(0b0110_0000 << (8 * 7)),
|
|
|
|
check: BitBoard::new(0b0111_0000 << (8 * 7)),
|
2024-02-03 15:17:02 -08:00
|
|
|
},
|
2024-02-25 08:51:23 -08:00
|
|
|
Parameters {
|
2024-04-25 13:28:24 -07:00
|
|
|
origin: Squares {
|
2024-02-09 20:00:47 -08:00
|
|
|
king: Square::E8,
|
|
|
|
rook: Square::A8,
|
|
|
|
},
|
2024-04-25 13:28:24 -07:00
|
|
|
target: Squares {
|
2024-02-09 20:00:47 -08:00
|
|
|
king: Square::C8,
|
|
|
|
rook: Square::D8,
|
|
|
|
},
|
2024-04-25 13:28:24 -07:00
|
|
|
clear: BitBoard::new(0b0000_1110 << (8 * 7)),
|
|
|
|
check: BitBoard::new(0b0001_1100 << (8 * 7)),
|
2024-02-03 15:17:02 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
];
|
2024-02-25 08:51:23 -08:00
|
|
|
|
2024-04-25 13:28:24 -07:00
|
|
|
pub fn parameters(self, color: Color) -> &'static Parameters {
|
|
|
|
&Castle::PARAMETERS[color as usize][self as usize]
|
2024-02-25 08:51:23 -08:00
|
|
|
}
|
2024-02-03 15:17:02 -08:00
|
|
|
}
|