Refactor project structure to use submodules
This commit is contained in:
133
src/board/state.rs
Normal file
133
src/board/state.rs
Normal file
@@ -0,0 +1,133 @@
|
||||
use crate::{board::board::Color, movegen::r#move::MoveType};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
pub struct State {
|
||||
side_to_move: Color,
|
||||
castling_ability: [Castle; 2],
|
||||
en_passant_target_square: Option<usize>,
|
||||
halfmove_clock: u8,
|
||||
fullmove_counter: u8,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
side_to_move: Color::White,
|
||||
castling_ability: [Castle::Both, Castle::Both],
|
||||
en_passant_target_square: None,
|
||||
halfmove_clock: 0,
|
||||
fullmove_counter: 1,
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn load_state(
|
||||
side_to_move: Color,
|
||||
castling_ability: [Castle; 2],
|
||||
en_passant_target_square: Option<usize>,
|
||||
halfmove_clock: u8,
|
||||
fullmove_counter: u8,
|
||||
) -> Self {
|
||||
Self {
|
||||
side_to_move,
|
||||
castling_ability,
|
||||
en_passant_target_square,
|
||||
halfmove_clock,
|
||||
fullmove_counter,
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn en_passant_target_square(&self) -> Option<usize> {
|
||||
self.en_passant_target_square
|
||||
}
|
||||
|
||||
pub fn set_castling_ability(&mut self, color: Color, castle: Castle) {
|
||||
match color {
|
||||
Color::White => self.castling_ability[0] = castle,
|
||||
Color::Black => self.castling_ability[1] = castle,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_castling_state_quiet(&mut self, src: usize, color: Color) {
|
||||
self.update_castling_state_capture(src, color);
|
||||
|
||||
match (src, color) {
|
||||
(4, Color::White) => self.set_castling_ability(color, Castle::None),
|
||||
(60, Color::Black) => self.set_castling_ability(color, Castle::None),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_castling_state_capture(&mut self, target: usize, color: Color) {
|
||||
let (short_square, long_square) = match color {
|
||||
Color::White => (7, 0),
|
||||
Color::Black => (63, 56),
|
||||
};
|
||||
|
||||
if target == short_square {
|
||||
match self.castling_ability(color) {
|
||||
Castle::Both => self.set_castling_ability(color, Castle::Long),
|
||||
Castle::Short => self.set_castling_ability(color, Castle::None),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
if target == long_square {
|
||||
match self.castling_ability(color) {
|
||||
Castle::Both => self.set_castling_ability(color, Castle::Short),
|
||||
Castle::Long => self.set_castling_ability(color, Castle::None),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_half_move(&mut self, move_type: MoveType, pawn_move: bool) {
|
||||
if move_type == MoveType::Capture || pawn_move {
|
||||
self.halfmove_clock = 0;
|
||||
} else {
|
||||
self.halfmove_clock += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_full_move(&mut self, color: Color) {
|
||||
match color {
|
||||
Color::White => (),
|
||||
Color::Black => self.fullmove_counter += 1,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn change_side(&mut self) {
|
||||
self.side_to_move = match self.side_to_move {
|
||||
Color::White => Color::Black,
|
||||
Color::Black => Color::White,
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn next_turn(&self) -> Color {
|
||||
self.side_to_move
|
||||
}
|
||||
|
||||
pub fn set_en_passant_target_square(&mut self, square: Option<usize>) {
|
||||
self.en_passant_target_square = square;
|
||||
}
|
||||
|
||||
pub const fn castling_ability(&self, color: Color) -> Castle {
|
||||
match color {
|
||||
Color::White => self.castling_ability[0],
|
||||
Color::Black => self.castling_ability[1],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for State {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
pub enum Castle {
|
||||
Short,
|
||||
Long,
|
||||
Both,
|
||||
None,
|
||||
}
|
||||
Reference in New Issue
Block a user