wip
This commit is contained in:
parent
ad3c6d0cfa
commit
4f93a095b5
5 changed files with 47 additions and 24 deletions
|
@ -1,22 +1,34 @@
|
|||
//! Sets of squares.
|
||||
|
||||
use crate::board::*;
|
||||
|
||||
use std::iter::ExactSizeIterator;
|
||||
use std::iter::FusedIterator;
|
||||
|
||||
/// A set of squares.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Bitboard(pub(crate) u64);
|
||||
pub struct Bitboard(pub u64);
|
||||
|
||||
impl Bitboard {
|
||||
/// Returns an empty bitboard.
|
||||
#[inline]
|
||||
pub fn new() -> Self {
|
||||
Self(0)
|
||||
}
|
||||
|
||||
/// Returns `true` if the bitboard is empty.
|
||||
#[inline]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0 == 0
|
||||
}
|
||||
|
||||
/// Returns `true` if the bitboard contains the given square.
|
||||
#[inline]
|
||||
pub fn contains(&self, square: Square) -> bool {
|
||||
self.0 & (1 << square as u8) != 0
|
||||
}
|
||||
|
||||
/// Returns the first square in the bitboard, if it is not empty.
|
||||
#[inline]
|
||||
pub fn first(&self) -> Option<Square> {
|
||||
let mask = self.0;
|
||||
|
@ -26,6 +38,7 @@ impl Bitboard {
|
|||
}
|
||||
}
|
||||
|
||||
/// Removes the first square in the bitboard and returns it, if the bitboard is not empty.
|
||||
#[inline]
|
||||
pub fn pop(&mut self) -> Option<Square> {
|
||||
let Self(ref mut mask) = self;
|
||||
|
@ -37,13 +50,27 @@ impl Bitboard {
|
|||
square
|
||||
}
|
||||
|
||||
///Inserts a square in the bitboard.
|
||||
#[inline]
|
||||
pub fn insert(&mut self, square: Square) {
|
||||
self.0 |= 1 << square as u8;
|
||||
}
|
||||
|
||||
/// Removes a square from the bitboard.
|
||||
#[inline]
|
||||
pub fn trans(&self, direction: Direction) -> Self {
|
||||
pub fn remove(&mut self, square: Square) {
|
||||
self.0 &= !(1 << square as u8)
|
||||
}
|
||||
|
||||
/// Returns the square that is horizontaly symmetric to the current square.
|
||||
#[inline]
|
||||
pub fn mirror(self) -> Self {
|
||||
let [a, b, c, d, e, f, g, h] = self.0.to_le_bytes();
|
||||
Self(u64::from_le_bytes([h, g, f, e, d, c, b, a]))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn trans(&self, direction: Direction) -> Self {
|
||||
match direction {
|
||||
Direction::North => Self(self.0 << 8),
|
||||
Direction::NorthEast => Self(self.0 << 9) & !File::A.bitboard(),
|
||||
|
@ -55,17 +82,6 @@ impl Bitboard {
|
|||
Direction::NorthWest => Self(self.0 << 7) & !File::H.bitboard(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn contains(&self, square: Square) -> bool {
|
||||
self.0 & (1 << square as u8) != 0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn mirror(self) -> Bitboard {
|
||||
let [a, b, c, d, e, f, g, h] = self.0.to_le_bytes();
|
||||
Self(u64::from_le_bytes([h, g, f, e, d, c, b, a]))
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::BitOr for Bitboard {
|
||||
|
|
18
src/board.rs
18
src/board.rs
|
@ -3,9 +3,9 @@
|
|||
use crate::bitboard::*;
|
||||
|
||||
macro_rules! container {
|
||||
($a:ident, $b:ident, $n:literal) => {
|
||||
($vis:vis, $a:ident, $b:ident, $n:literal) => {
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub(crate) struct $b<T>(pub(crate) [T; $n]);
|
||||
$vis struct $b<T>(pub(crate) [T; $n]);
|
||||
#[allow(unused)]
|
||||
impl<T> $b<T> {
|
||||
#[inline]
|
||||
|
@ -35,7 +35,7 @@ pub enum Color {
|
|||
Black,
|
||||
}
|
||||
|
||||
container!(Color, ByColor, 2);
|
||||
container!(pub, Color, ByColor, 2);
|
||||
|
||||
impl Color {
|
||||
#[inline]
|
||||
|
@ -93,7 +93,7 @@ pub enum File {
|
|||
H,
|
||||
}
|
||||
|
||||
container!(File, ByFile, 8);
|
||||
container!(pub(crate), File, ByFile, 8);
|
||||
|
||||
impl File {
|
||||
#[inline]
|
||||
|
@ -165,7 +165,7 @@ pub enum Rank {
|
|||
Eighth,
|
||||
}
|
||||
|
||||
container!(Rank, ByRank, 8);
|
||||
container!(pub(crate), Rank, ByRank, 8);
|
||||
|
||||
impl Rank {
|
||||
#[inline]
|
||||
|
@ -243,7 +243,7 @@ pub enum Square{
|
|||
A8, B8, C8, D8, E8, F8, G8, H8,
|
||||
}
|
||||
|
||||
container!(Square, BySquare, 64);
|
||||
container!(pub(crate), Square, BySquare, 64);
|
||||
|
||||
impl Square {
|
||||
#[inline]
|
||||
|
@ -500,7 +500,7 @@ impl Role {
|
|||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub(crate) struct ByRole<T>(pub(crate) [T; 6]);
|
||||
pub struct ByRole<T>(pub(crate) [T; 6]);
|
||||
#[allow(unused)]
|
||||
impl<T> ByRole<T> {
|
||||
#[inline]
|
||||
|
@ -572,7 +572,7 @@ pub(crate) enum Direction {
|
|||
West,
|
||||
}
|
||||
|
||||
container!(Direction, ByDirection, 8);
|
||||
container!(pub(crate), Direction, ByDirection, 8);
|
||||
|
||||
impl Direction {
|
||||
#[inline]
|
||||
|
@ -614,7 +614,7 @@ pub enum CastlingSide {
|
|||
Long,
|
||||
}
|
||||
|
||||
container!(CastlingSide, ByCastlingSide, 2);
|
||||
container!(pub(crate), CastlingSide, ByCastlingSide, 2);
|
||||
|
||||
impl CastlingSide {
|
||||
#[inline]
|
||||
|
|
|
@ -73,10 +73,10 @@
|
|||
//! - etc.
|
||||
|
||||
pub(crate) mod array_vec;
|
||||
pub(crate) mod bitboard;
|
||||
pub(crate) mod magics;
|
||||
pub(crate) mod rays;
|
||||
|
||||
pub mod bitboard;
|
||||
pub mod board;
|
||||
pub mod lookup;
|
||||
pub mod position;
|
||||
|
|
|
@ -281,6 +281,12 @@ impl Position {
|
|||
}
|
||||
}
|
||||
|
||||
/// Returns the bitboards for every piece type and color.
|
||||
#[inline]
|
||||
pub fn bitboards(&self) -> ByColor<ByRole<Bitboard>> {
|
||||
self.setup.bitboards()
|
||||
}
|
||||
|
||||
pub(crate) fn move_from_uci<'l>(&'l self, uci: UciMove) -> Result<Move<'l>, InvalidUciMove> {
|
||||
struct VisitorImpl<const ROLE: u8> {
|
||||
role: Role,
|
||||
|
|
|
@ -383,8 +383,9 @@ impl Setup {
|
|||
}
|
||||
}
|
||||
|
||||
/// Returns the bitboards for every piece type and color.
|
||||
#[inline]
|
||||
pub(crate) fn bitboards(&self) -> ByColor<ByRole<Bitboard>> {
|
||||
pub fn bitboards(&self) -> ByColor<ByRole<Bitboard>> {
|
||||
let Self {
|
||||
w,
|
||||
p_b_q,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue