Move State struct impl to its own file

This commit is contained in:
2024-07-16 19:03:16 +03:00
parent 8d779d69d0
commit ae95a941a2
6 changed files with 130 additions and 129 deletions

118
src/state.rs Normal file
View File

@@ -0,0 +1,118 @@
use crate::board::Color;
#[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 get_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, source: usize, color: Color) {
self.update_castling_state_capture(source, color);
match (source, 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.get_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.get_castling_ability(color) {
Castle::Both => self.set_castling_ability(color, Castle::Short),
Castle::Long => self.set_castling_ability(color, Castle::None),
_ => (),
}
}
}
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, sq: Option<usize>) {
self.en_passant_target_square = sq;
}
pub const fn get_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,
}