[board] Remove Flags struct, replace it with Castle and supporting structs
Encapsulate castling rights within a small module. Castle provides the API to the rest of the board package. Rights encodes the castling rights for each player. Parameters defines castling parameters for each player.
This commit is contained in:
parent
9f2bfc0457
commit
0b100d5f14
5 changed files with 210 additions and 203 deletions
|
@ -1,7 +1,12 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
use chessfriend_bitboard::BitBoard;
|
||||
use chessfriend_core::{Color, Square};
|
||||
mod parameters;
|
||||
mod rights;
|
||||
|
||||
pub use rights::Rights;
|
||||
|
||||
use chessfriend_core::Color;
|
||||
use parameters::Parameters;
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
|
@ -10,118 +15,10 @@ pub enum Castle {
|
|||
QueenSide = 1,
|
||||
}
|
||||
|
||||
pub struct Parameters {
|
||||
/// Origin squares of the king and rook.
|
||||
origin: Squares,
|
||||
|
||||
/// Target or destination squares for the king and rook.
|
||||
target: Squares,
|
||||
|
||||
/// The set of squares that must be clear of any pieces in order to perform
|
||||
/// this castle.
|
||||
clear: BitBoard,
|
||||
|
||||
/// The set of squares that must not be attacked (i.e. visible to opposing
|
||||
/// pieces) in order to perform this castle.
|
||||
check: BitBoard,
|
||||
}
|
||||
|
||||
impl Parameters {
|
||||
pub fn king_origin_square(&self) -> Square {
|
||||
self.origin.king
|
||||
}
|
||||
|
||||
pub fn rook_origin_square(&self) -> Square {
|
||||
self.origin.rook
|
||||
}
|
||||
|
||||
pub fn king_target_square(&self) -> Square {
|
||||
self.target.king
|
||||
}
|
||||
|
||||
pub fn rook_target_square(&self) -> Square {
|
||||
self.target.rook
|
||||
}
|
||||
|
||||
/// A [`BitBoard`] of the squares that must be clear of any piece in order
|
||||
/// to perform this castle move.
|
||||
pub fn clear_squares(&self) -> &BitBoard {
|
||||
&self.clear
|
||||
}
|
||||
|
||||
/// A [`BitBoard`] of the squares that must not be visible to opposing
|
||||
/// pieces in order to perform this castle move.
|
||||
pub fn check_squares(&self) -> &BitBoard {
|
||||
&self.check
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Squares {
|
||||
king: Square,
|
||||
rook: Square,
|
||||
}
|
||||
|
||||
impl Castle {
|
||||
pub const ALL: [Castle; 2] = [Castle::KingSide, Castle::QueenSide];
|
||||
|
||||
/// Parameters for each castling move, organized by color and board-side.
|
||||
const PARAMETERS: [[Parameters; 2]; Color::NUM] = [
|
||||
[
|
||||
Parameters {
|
||||
origin: Squares {
|
||||
king: Square::E1,
|
||||
rook: Square::H1,
|
||||
},
|
||||
target: Squares {
|
||||
king: Square::G1,
|
||||
rook: Square::F1,
|
||||
},
|
||||
clear: BitBoard::new(0b0110_0000),
|
||||
check: BitBoard::new(0b0111_0000),
|
||||
},
|
||||
Parameters {
|
||||
origin: Squares {
|
||||
king: Square::E1,
|
||||
rook: Square::A1,
|
||||
},
|
||||
target: Squares {
|
||||
king: Square::C1,
|
||||
rook: Square::D1,
|
||||
},
|
||||
clear: BitBoard::new(0b0000_1110),
|
||||
check: BitBoard::new(0b0001_1100),
|
||||
},
|
||||
],
|
||||
[
|
||||
Parameters {
|
||||
origin: Squares {
|
||||
king: Square::E8,
|
||||
rook: Square::H8,
|
||||
},
|
||||
target: Squares {
|
||||
king: Square::G8,
|
||||
rook: Square::F8,
|
||||
},
|
||||
clear: BitBoard::new(0b0110_0000 << (8 * 7)),
|
||||
check: BitBoard::new(0b0111_0000 << (8 * 7)),
|
||||
},
|
||||
Parameters {
|
||||
origin: Squares {
|
||||
king: Square::E8,
|
||||
rook: Square::A8,
|
||||
},
|
||||
target: Squares {
|
||||
king: Square::C8,
|
||||
rook: Square::D8,
|
||||
},
|
||||
clear: BitBoard::new(0b0000_1110 << (8 * 7)),
|
||||
check: BitBoard::new(0b0001_1100 << (8 * 7)),
|
||||
},
|
||||
],
|
||||
];
|
||||
|
||||
pub fn parameters(self, color: Color) -> &'static Parameters {
|
||||
&Castle::PARAMETERS[color as usize][self as usize]
|
||||
&Parameters::BY_COLOR[color as usize][self as usize]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue