Implement a whole new move crate

This commit is contained in:
Eryn Wells 2024-02-09 20:00:47 -08:00
parent 0bedf2aa9f
commit c55b7c4877
7 changed files with 512 additions and 148 deletions

View file

@ -1,17 +1,26 @@
// Eryn Wells <eryn@erynwells.me>
use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Color, Square};
use chessfriend_core::Square;
#[repr(u16)]
#[repr(u8)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Castle {
KingSide = 0b10,
QueenSide = 0b11,
KingSide = 0,
QueenSide = 1,
}
pub(crate) struct CastlingParameters {
/// Origin squares of the king and rook.
origin_squares: Squares,
/// Target or destination squares for the king and rook.
target_squares: Squares,
/// The set of squares that must be clear of any pieces in order to perform this castle.
clear_squares: BitBoard,
/// The set of squares that must not be attacked in order to perform this castle.
check_squares: BitBoard,
}
@ -24,87 +33,59 @@ pub(crate) struct Squares {
impl Castle {
pub const ALL: [Castle; 2] = [Castle::KingSide, Castle::QueenSide];
const STARTING_SQUARES: [[Squares; 2]; 2] = [
/// Parameters for each castling move, organized by color and board-side.
const PARAMETERS: [[CastlingParameters; 2]; 2] = [
[
Squares {
king: Square::E1,
rook: Square::H1,
},
Squares {
king: Square::E1,
rook: Square::A1,
},
],
[
Squares {
king: Square::E8,
rook: Square::H8,
},
Squares {
king: Square::E8,
rook: Square::A8,
},
],
];
const TARGET_SQUARES: [[Squares; 2]; 2] = [
[
Squares {
king: Square::G1,
rook: Square::F1,
},
Squares {
king: Square::C1,
rook: Square::D1,
},
],
[
Squares {
king: Square::G8,
rook: Square::F8,
},
Squares {
king: Square::C8,
rook: Square::D8,
},
],
];
pub(crate) fn starting_squares(&self, color: Color) -> &'static Squares {
&Castle::STARTING_SQUARES[color as usize][self.into_index()]
}
pub(crate) fn target_squares(&self, color: Color) -> &'static Squares {
&Castle::TARGET_SQUARES[color as usize][self.into_index()]
}
pub(crate) fn into_index(&self) -> usize {
match self {
Castle::KingSide => 0,
Castle::QueenSide => 1,
}
}
pub(crate) fn parameters(&self) -> CastlingParameters {
match self {
Castle::KingSide => CastlingParameters {
CastlingParameters {
origin_squares: Squares {
king: Square::E1,
rook: Square::H1,
},
target_squares: Squares {
king: Square::G1,
rook: Square::F1,
},
clear_squares: BitBoard::new(0b01100000),
check_squares: BitBoard::new(0b01110000),
},
Castle::QueenSide => CastlingParameters {
CastlingParameters {
origin_squares: Squares {
king: Square::E1,
rook: Square::A1,
},
target_squares: Squares {
king: Square::C1,
rook: Square::D1,
},
clear_squares: BitBoard::new(0b00001110),
check_squares: BitBoard::new(0b00011100),
},
}
}
}
impl CastlingParameters {
pub fn clear_squares(&self) -> &BitBoard {
&self.clear_squares
}
pub fn check_squares(&self) -> &BitBoard {
&self.check_squares
}
],
[
CastlingParameters {
origin_squares: Squares {
king: Square::E8,
rook: Square::H8,
},
target_squares: Squares {
king: Square::G8,
rook: Square::F8,
},
clear_squares: BitBoard::new(0b01100000 << 8 * 7),
check_squares: BitBoard::new(0b01110000 << 8 * 7),
},
CastlingParameters {
origin_squares: Squares {
king: Square::E8,
rook: Square::A8,
},
target_squares: Squares {
king: Square::C8,
rook: Square::D8,
},
clear_squares: BitBoard::new(0b00001110 << 8 * 7),
check_squares: BitBoard::new(0b00011100 << 8 * 7),
},
],
];
}