Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "chess"
version = "3.2.0"
edition = "2018"
version = "4.0.0"
edition = "2021"
authors = ["Jordan Bray <[email protected]>"]
description = "This is a fast chess move generator. It has a very good set of documentation, so you should take advantage of that. It (now) generates all lookup tables with a build.rs file, which means that very little pseudo-legal move generation requires branching. There are some convenience functions that are exposed to, for example, find all the squares between two squares. This uses a copy-on-make style structure, and the Board structure is as slimmed down as possible to reduce the cost of copying the board. There are places to improve perft-test performance further, but I instead opt to be more feature-complete to make it useful in real applications. For example, I generate both a hash of the board and a pawn-hash of the board for use in evaluation lookup tables (using Zobrist hashing). There are two ways to generate moves, one is faster, the other has more features that will be useful if making a chess engine. See the documentation for more details."
build = "src/build.rs"
Expand All @@ -12,15 +12,18 @@ readme = "README.md"
keywords = ["chess", "move", "generator"]
license = "MIT"
documentation = "https://jordanbray.github.io/chess/chess/index.html"
rust-version = "1.56.0"

[dependencies]
arrayvec = "0.7.2"
const_for = "0.1.2"
nodrop = "0.1.14"
failure = "0.1.6"
thiserror = "1.0.44"

[profile.release]
opt-level = 3
debug = false
lto = "yes"

[profile.dev]
opt-level = 3
Expand All @@ -38,5 +41,6 @@ opt-level = 3
opt-level = 3

[build-dependencies]
rand = { version = "0.7.2", default_features = false, features = ["small_rng"] }
failure = "0.1.6"
const_for = "0.1.2"
rand = { version = "0.7.2", default-features = false, features = ["small_rng"] }
thiserror = "1.0.44"
35 changes: 26 additions & 9 deletions src/bitboard.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use const_for::const_for;

use crate::file::File;
use crate::rank::Rank;
use crate::square::*;
Expand Down Expand Up @@ -262,7 +264,7 @@ impl fmt::Display for BitBoard {
s.push_str(". ");
}
if x % 8 == 7 {
s.push_str("\n");
s.push('\n');
}
}
write!(f, "{}", s)
Expand All @@ -272,51 +274,66 @@ impl fmt::Display for BitBoard {
impl BitBoard {
/// Construct a new bitboard from a u64
#[inline]
pub fn new(b: u64) -> BitBoard {
pub const fn new(b: u64) -> BitBoard {
BitBoard(b)
}

/// Construct a new `BitBoard` with a particular `Square` set
#[inline]
pub fn set(rank: Rank, file: File) -> BitBoard {
pub const fn set(rank: Rank, file: File) -> BitBoard {
BitBoard::from_square(Square::make_square(rank, file))
}

/// Construct a new `BitBoard` with a particular `Square` set
#[inline]
pub fn from_square(sq: Square) -> BitBoard {
pub const fn from_square(sq: Square) -> BitBoard {
BitBoard(1u64 << sq.to_int())
}

/// Convert an `Option<Square>` to an `Option<BitBoard>`
#[inline]
#[deprecated(
since = "3.3.0",
note = "Method is considered an unnecessary shorthand for `square_option.map(BitBoard::from_square)`."
)]
pub fn from_maybe_square(sq: Option<Square>) -> Option<BitBoard> {
sq.map(|s| BitBoard::from_square(s))
sq.map(BitBoard::from_square)
}

/// Convert a `BitBoard` to a `Square`. This grabs the least-significant `Square`
#[inline]
pub fn to_square(&self) -> Square {
pub const fn to_square(self) -> Square {
Square::new(self.0.trailing_zeros() as u8)
}

/// Count the number of `Squares` set in this `BitBoard`
#[inline]
pub fn popcnt(&self) -> u32 {
pub const fn popcnt(&self) -> u32 {
self.0.count_ones()
}

/// Reverse this `BitBoard`. Look at it from the opponents perspective.
#[inline]
pub fn reverse_colors(&self) -> BitBoard {
pub const fn reverse_colors(&self) -> BitBoard {
BitBoard(self.0.swap_bytes())
}

/// Convert this `BitBoard` to a `usize` (for table lookups)
#[inline]
pub fn to_size(&self, rightshift: u8) -> usize {
pub const fn to_size(self, rightshift: u8) -> usize {
(self.0 >> rightshift) as usize
}

pub(crate) const fn from_array(arr: [u64; 8]) -> Self {
let mut accum = 0;

const_for!(i in 0..8 => {
accum <<= 8;
accum |= arr[i];
});

BitBoard::new(accum)
}
}

/// For the `BitBoard`, iterate over every `Square` set.
Expand Down
Loading