use crate::{board::board::Color, movegen::r#move::MoveType}; #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub struct State { side_to_move: Color, pub castling_ability: [Castle; 2], pub en_passant_square: Option, pub halfmove_clock: u8, pub fullmove_counter: u8, } impl State { pub const fn new() -> Self { Self { side_to_move: Color::White, castling_ability: [Castle::Both, Castle::Both], en_passant_square: None, halfmove_clock: 0, fullmove_counter: 1, } } pub const fn load_state( side_to_move: Color, castling_ability: [Castle; 2], en_passant_square: Option, halfmove_clock: u8, fullmove_counter: u8, ) -> Self { Self { side_to_move, castling_ability, en_passant_square, halfmove_clock, fullmove_counter, } } pub const fn en_passant_square(&self) -> Option { self.en_passant_square } pub fn set_en_passant_square(&mut self, square: Option) { self.en_passant_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], } } 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) | (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 revert_full_move(&mut self, color: Color) { match color { Color::White => (), Color::Black => self.fullmove_counter -= 1, } } pub fn change_side(&mut self) -> Color { self.side_to_move = match self.side_to_move { Color::White => Color::Black, Color::Black => Color::White, }; self.side_to_move } pub const fn current_player(&self) -> Color { self.side_to_move } } impl Default for State { fn default() -> Self { Self::new() } } #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum Castle { Short, Long, Both, None, }