Add State struct as part of Board struct
This commit is contained in:
25
src/board.rs
25
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_bishop_attacks, get_king_attacks, get_knight_attacks, get_pawn_attacks, get_queen_attacks,
|
||||||
get_rook_attacks,
|
get_rook_attacks,
|
||||||
};
|
};
|
||||||
|
use crate::game::State;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct Board {
|
pub struct Board {
|
||||||
white_pieces: [Piece; 6],
|
pub white_pieces: [Piece; 6],
|
||||||
black_pieces: [Piece; 6],
|
pub black_pieces: [Piece; 6],
|
||||||
|
pub state: State,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
@@ -31,6 +33,7 @@ impl Board {
|
|||||||
Piece::new(0x800000000000000, Kind::Queen, Color::Black),
|
Piece::new(0x800000000000000, Kind::Queen, Color::Black),
|
||||||
Piece::new(0x1000000000000000, Kind::King, 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::Queen, Color::Black),
|
||||||
Piece::new(0x0, Kind::King, 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 {
|
pub fn set_state(&mut self, state: State) {
|
||||||
let white_blockers = self.white_pieces.iter().fold(0, |acc, p| p.bitboard | acc);
|
self.state = state;
|
||||||
let black_blockers = self.black_pieces.iter().fold(0, |acc, p| p.bitboard | acc);
|
}
|
||||||
|
|
||||||
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 {
|
pub fn is_attacked(&self, sq: usize, opponent_color: Color) -> bool {
|
||||||
|
|||||||
20
src/fen.rs
20
src/fen.rs
@@ -5,7 +5,7 @@ use String as FenError;
|
|||||||
pub fn from_fen(fen: &str) -> Result<Game, FenError> {
|
pub fn from_fen(fen: &str) -> Result<Game, FenError> {
|
||||||
let fen_parts: Vec<_> = fen.split_whitespace().collect();
|
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])?;
|
let side_to_move = side_to_move(fen_parts[1])?;
|
||||||
|
|
||||||
@@ -17,16 +17,14 @@ pub fn from_fen(fen: &str) -> Result<Game, FenError> {
|
|||||||
|
|
||||||
let fullmove_counter = fullmove_counter(fen_parts[5])?;
|
let fullmove_counter = fullmove_counter(fen_parts[5])?;
|
||||||
|
|
||||||
Ok(Game {
|
board.set_state(State::load_state(
|
||||||
board,
|
side_to_move,
|
||||||
state: State::load_state(
|
castling_ability,
|
||||||
side_to_move,
|
en_passant_target_square,
|
||||||
castling_ability,
|
halfmove_clock,
|
||||||
en_passant_target_square,
|
fullmove_counter,
|
||||||
halfmove_clock,
|
));
|
||||||
fullmove_counter,
|
Ok(Game { board })
|
||||||
),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn piece_placement(pieces: &str) -> Result<Board, FenError> {
|
pub fn piece_placement(pieces: &str) -> Result<Board, FenError> {
|
||||||
|
|||||||
@@ -7,14 +7,12 @@ use String as FenError;
|
|||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
pub board: Board,
|
pub board: Board,
|
||||||
pub state: State,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Game {
|
impl Game {
|
||||||
pub const fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
board: Board::new(),
|
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 {
|
pub struct State {
|
||||||
side_to_move: Color,
|
side_to_move: Color,
|
||||||
castling_ability: u8,
|
castling_ability: u8,
|
||||||
@@ -68,6 +66,10 @@ impl State {
|
|||||||
fullmove_counter,
|
fullmove_counter,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const fn get_en_passant_target_square(&self) -> u8 {
|
||||||
|
self.en_passant_target_square
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|||||||
Reference in New Issue
Block a user