Add utility functions for readability and refactor make_move

This commit is contained in:
stefiosif
2024-08-28 21:07:22 +03:00
parent 6c8f445fa9
commit 21076da68c
10 changed files with 91 additions and 61 deletions

View File

@@ -21,6 +21,13 @@ pub const fn square_to_bitboard_wrapping(square: usize) -> Bitboard {
1_u64.wrapping_shl(square as u32)
}
pub const fn bitboard_to_coords(bitboard: Bitboard) -> (usize, usize) {
match bitboard {
0 => (0, 0),
_ => (lsb(bitboard) / 8, lsb(bitboard) % 8),
}
}
pub const fn lsb(bitboard: Bitboard) -> usize {
bitboard.trailing_zeros() as usize
}

View File

@@ -167,6 +167,18 @@ impl Board {
pub fn set_state(&mut self, state: State) {
self.state = state;
}
pub fn piece_type_at(&self, square: usize, color: Color) -> Option<PieceType> {
let pieces = match color {
Color::White => &self.white_pieces,
Color::Black => &self.black_pieces,
};
pieces
.iter()
.find(|p| have_common_bit(p.bitboard, square_to_bitboard(square)))
.map(|p| p.piece_type)
}
}
impl Default for Board {
@@ -216,6 +228,8 @@ impl PieceType {
}
use std::ops::{Index, IndexMut};
use super::bitboard::square_to_bitboard;
impl Index<PieceType> for [Piece] {
type Output = Piece;

View File

@@ -1,7 +1,7 @@
use crate::board::fen::from_fen;
use String as FenError;
use super::board::Board;
use super::board::{Board, Color};
#[derive(Debug, PartialEq, Eq)]
pub struct Game {
@@ -22,6 +22,10 @@ impl Game {
pub const fn run(&self) {
Board::new();
}
pub const fn current_player(&self) -> Color {
self.board.state.current_player()
}
}
impl Default for Game {

View File

@@ -1,7 +1,3 @@
use u64 as Bitboard;
use crate::board::bitboard::lsb;
pub struct Square {}
impl Square {
@@ -82,13 +78,6 @@ pub const fn coords_to_square(rank: usize, file: usize) -> usize {
rank * 8 + file
}
pub const fn bitboard_to_coords(bitboard: Bitboard) -> (usize, usize) {
match bitboard {
0 => (0, 0),
_ => (lsb(bitboard) / 8, lsb(bitboard) % 8),
}
}
pub fn square_to_algebraic(square: usize) -> String {
let file = (square % 8) as u8;
let rank = (square / 8) as u8;

View File

@@ -3,10 +3,10 @@ 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,
pub castling_ability: [Castle; 2],
pub en_passant_target_square: Option<usize>,
pub halfmove_clock: u8,
pub fullmove_counter: u8,
}
impl State {
@@ -40,6 +40,17 @@ impl State {
self.en_passant_target_square
}
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],
}
}
pub fn set_castling_ability(&mut self, color: Color, castle: Castle) {
match color {
Color::White => self.castling_ability[0] = castle,
@@ -95,26 +106,23 @@ impl State {
}
}
pub fn change_side(&mut self) {
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,
}
}
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],
}
pub const fn current_player(&self) -> Color {
self.side_to_move
}
}