[board] Use Castle as the interface type for methods related to castling

Use BoardSide as an internal type for looking up generated bitboards, target squares, etc.
This commit is contained in:
Eryn Wells 2024-01-21 09:05:42 -08:00
parent 7071f6a742
commit 918b68f300
4 changed files with 71 additions and 37 deletions

View file

@ -1,5 +1,6 @@
// Eryn Wells <eryn@erynwells.me>
use crate::{position::BoardSide, r#move::Castle, Color};
use std::{fmt, str::FromStr};
pub enum Direction {
@ -157,12 +158,23 @@ impl Square {
impl Square {
pub(crate) const KING_STARTING_SQUARES: [Square; 2] = [Square::E1, Square::E8];
pub(crate) const KING_CASTLE_TARGET_SQUARES: [[Square; 2]; 2] =
[[Square::C1, Square::G1], [Square::C8, Square::G8]];
[[Square::G1, Square::C1], [Square::G8, Square::C8]];
pub(crate) const ROOK_CASTLE_TARGET_SQUARES: [[Square; 2]; 2] =
[[Square::F1, Square::D1], [Square::F8, Square::D8]];
pub fn from_algebraic_str(s: &str) -> Result<Square, ParseSquareError> {
s.parse()
}
pub fn king_castle_target(player: Color, castle: Castle) -> Square {
let board_side: BoardSide = castle.into();
Self::KING_CASTLE_TARGET_SQUARES[player as usize][board_side as usize]
}
pub fn rook_castle_target(player: Color, castle: Castle) -> Square {
let board_side: BoardSide = castle.into();
Self::ROOK_CASTLE_TARGET_SQUARES[player as usize][board_side as usize]
}
pub fn neighbor(self, direction: Direction) -> Option<Square> {
match direction {
Direction::North => Square::try_index(self as usize + 8),