From 2133fb3cad04335588e95198e1f758813a673f92 Mon Sep 17 00:00:00 2001 From: stefiosif Date: Sun, 9 Jun 2024 15:21:11 +0300 Subject: [PATCH] Add State struct as part of Board struct --- src/board.rs | 25 +++++++++++++++++++------ src/fen.rs | 20 +++++++++----------- src/game.rs | 8 +++++--- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/board.rs b/src/board.rs index dcce0bd..27dc7d1 100644 --- a/src/board.rs +++ b/src/board.rs @@ -5,11 +5,13 @@ use crate::attack::{ get_bishop_attacks, get_king_attacks, get_knight_attacks, get_pawn_attacks, get_queen_attacks, get_rook_attacks, }; +use crate::game::State; #[derive(Debug, PartialEq, Eq)] pub struct Board { - white_pieces: [Piece; 6], - black_pieces: [Piece; 6], + pub white_pieces: [Piece; 6], + pub black_pieces: [Piece; 6], + pub state: State, } impl Board { @@ -31,6 +33,7 @@ impl Board { Piece::new(0x800000000000000, Kind::Queen, Color::Black), Piece::new(0x1000000000000000, Kind::King, Color::Black), ], + state: State::new(), } } @@ -52,6 +55,7 @@ impl Board { Piece::new(0x0, Kind::Queen, Color::Black), Piece::new(0x0, Kind::King, Color::Black), ], + state: State::new(), } } @@ -62,11 +66,20 @@ impl Board { }; } - pub fn get_all_occupancies(&self) -> Bitboard { - let white_blockers = self.white_pieces.iter().fold(0, |acc, p| p.bitboard | acc); - let black_blockers = self.black_pieces.iter().fold(0, |acc, p| p.bitboard | acc); + pub fn set_state(&mut self, state: State) { + self.state = state; + } - white_blockers | black_blockers + pub fn get_white_occupancies(&self) -> Bitboard { + self.white_pieces.iter().fold(0, |acc, p| p.bitboard | acc) + } + + pub fn get_black_occupancies(&self) -> Bitboard { + self.black_pieces.iter().fold(0, |acc, p| p.bitboard | acc) + } + + pub fn get_all_occupancies(&self) -> Bitboard { + self.get_white_occupancies() | self.get_black_occupancies() } pub fn is_attacked(&self, sq: usize, opponent_color: Color) -> bool { diff --git a/src/fen.rs b/src/fen.rs index 7bd2854..9428f98 100644 --- a/src/fen.rs +++ b/src/fen.rs @@ -5,7 +5,7 @@ use String as FenError; pub fn from_fen(fen: &str) -> Result { let fen_parts: Vec<_> = fen.split_whitespace().collect(); - let board = piece_placement(fen_parts[0])?; + let mut board = piece_placement(fen_parts[0])?; let side_to_move = side_to_move(fen_parts[1])?; @@ -17,16 +17,14 @@ pub fn from_fen(fen: &str) -> Result { let fullmove_counter = fullmove_counter(fen_parts[5])?; - Ok(Game { - board, - state: State::load_state( - side_to_move, - castling_ability, - en_passant_target_square, - halfmove_clock, - fullmove_counter, - ), - }) + board.set_state(State::load_state( + side_to_move, + castling_ability, + en_passant_target_square, + halfmove_clock, + fullmove_counter, + )); + Ok(Game { board }) } pub fn piece_placement(pieces: &str) -> Result { diff --git a/src/game.rs b/src/game.rs index 3b1234f..78284ab 100644 --- a/src/game.rs +++ b/src/game.rs @@ -7,14 +7,12 @@ use String as FenError; #[derive(Debug, PartialEq, Eq)] pub struct Game { pub board: Board, - pub state: State, } impl Game { pub const fn new() -> Self { Self { board: Board::new(), - state: State::new(), } } @@ -33,7 +31,7 @@ impl Default for Game { } } -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq, Clone, Copy)] pub struct State { side_to_move: Color, castling_ability: u8, @@ -68,6 +66,10 @@ impl State { fullmove_counter, } } + + pub const fn get_en_passant_target_square(&self) -> u8 { + self.en_passant_target_square + } } use std::fmt;