Skip to content

Commit

Permalink
Merge 38e4163 into c20a4f9
Browse files Browse the repository at this point in the history
  • Loading branch information
sfleischman105 committed Nov 28, 2017
2 parents c20a4f9 + 38e4163 commit d9b3309
Show file tree
Hide file tree
Showing 27 changed files with 405 additions and 111 deletions.
85 changes: 83 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pleco/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "pleco"
# REMINDER TO CHANGE IN README
version = "0.2.0"
version = "0.2.1"
authors = ["Stephen Fleischman <stephenf@cs.washington.edu>"]
description = "A blazingly-fast chess engine and Chess AI."
homepage = "https://github.com/sfleischman105/Pleco"
Expand Down
2 changes: 1 addition & 1 deletion pleco/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ To use Pleco inside your own Rust projects, [Pleco.rs is available as a library

```
[dependencies]
pleco = "0.2.0"
pleco = "0.2.1"
```

And add the following to a `main.rs` or `lib.rs`:
Expand Down
2 changes: 1 addition & 1 deletion pleco/benches/bit_benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn bench_popcount_1000_rust(b: &mut Bencher) {
b.iter(|| {
black_box({
for bits in BIT_SETS_DENSE_1000.iter() {
black_box({black_box((*bits).0).count_ones();})
black_box({black_box(black_box((*bits).0)).count_ones();})
}
})
})
Expand Down
38 changes: 22 additions & 16 deletions pleco/src/board/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
//! This module contains `Board`, the Object representing the current state of a chessboard.
//! All modifications to the current state of the board is done through this object, as well as
//! gathering information about the current state of the board.
//!
//! This module also contains structures used by the board, such as `castling_rights` for
//! determining castling rights throughout a game. Other utilities that may be of use
//! are `eval`, which takes a board and evaluates it, and `PGN`, which parses a PGN File into
//! a board.


pub mod movegen;
Expand Down Expand Up @@ -38,10 +43,11 @@ use std::cmp::{PartialEq,max,min};
lazy_static! {
/// Statically initialized lookup tables created when first ran.
/// Nothing will ever be mutated in here, so it is safe to pass around.
/// See `pleco::MagicHelper` for more information.
/// See `core::magic_helper::MagicHelper` for more information.
pub static ref MAGIC_HELPER: MagicHelper<'static,'static> = MagicHelper::new();
}

/// Represents possible Errors encountered while building a `Board` from a fen string.
#[derive(Debug, Clone)]
pub enum FenBuildError {
SquareSmallerRank,
Expand Down Expand Up @@ -975,22 +981,22 @@ impl Board {
MoveGen::generate::<Legal, AllGenType>(self)
}

/// Get a List of all PseudoLegal [BitMove]s for the player whose turn it is to move.
/// Works exactly the same as [Board::generate_moves()], but doesn't guarantee that all
/// Get a List of all PseudoLegal `BitMove`s for the player whose turn it is to move.
/// Works exactly the same as `Board::generate_moves()`, but doesn't guarantee that all
/// the moves are legal for the current position. Moves need to be checked with a
/// [Board::legal_move(move)] in order to be certain of a legal move.
/// `Board::legal_move(move)` in order to be certain of a legal move.
pub fn generate_pseudolegal_moves(&self) -> MoveList {
MoveGen::generate::<PseudoLegal, AllGenType>(self)
}

/// Get a List of legal [BitMove]s for the player whose turn it is to move or a certain type.
/// Get a List of legal `BitMove`s for the player whose turn it is to move or a certain type.
///
/// This method already takes into account if the Board is currently in check, and will return
/// legal moves only. If a non-ALL GenType is supplied, only a subset of the total moves will be given.
///
/// # Panics
///
/// Panics if given [GenTypes::QuietChecks] while the current board is in check
/// Panics if given `GenTypes::QuietChecks` while the current board is in check
///
/// # Examples
///
Expand All @@ -1014,17 +1020,17 @@ impl Board {
}
}

/// Get a List of all PseudoLegal [BitMove]s for the player whose turn it is to move.
/// Works exactly the same as [Board::generate_moves()], but doesn't guarantee that all
/// Get a List of all PseudoLegal `BitMove`s for the player whose turn it is to move.
/// Works exactly the same as `Board::generate_moves()`, but doesn't guarantee that all
/// the moves are legal for the current position. Moves need to be checked with a
/// [Board::legal_move(move)] in order to be certain of a legal move.
/// `Board::legal_move(move)` in order to be certain of a legal move.
///
/// This method already takes into account if the Board is currently in check.
/// If a non-ALL GenType is supplied, only a subset of the total moves will be given.
///
/// # Panics
///
/// Panics if given [GenTypes::QuietChecks] while the current board is in check
/// Panics if given `GenTypes::QuietChecks` while the current board is in check
pub fn generate_pseudolegal_moves_of_type(&self, gen_type: GenTypes) -> MoveList {
match gen_type {
GenTypes::All => MoveGen::generate::<PseudoLegal,AllGenType>(self),
Expand Down Expand Up @@ -1604,12 +1610,12 @@ impl Board {

// ------- CHECKING -------

/// Return if current side to move is in check
/// Return if current side to move is in check.
pub fn in_check(&self) -> bool {
self.state.checkers_bb.is_not_empty()
}

/// Return if the current side to move is in check_mate.
/// Return if the current side to move is in check mate.
///
/// This method can be computationally expensive, do not use outside of Engines.
pub fn checkmate(&self) -> bool {
Expand All @@ -1620,7 +1626,7 @@ impl Board {
///
/// This method can be computationally expensive, do not use outside of Engines.
pub fn stalemate(&self) -> bool {
!self.in_check() && self.generate_moves().is_empty()
!self.in_check() && (self.generate_moves().is_empty() || self.state.rule_50 >= 50)
}

/// Return the BitBoard of Checks on the current player's king.
Expand Down Expand Up @@ -1971,15 +1977,15 @@ enum RandGen {
///
/// Create one `Board` with at least 5 moves played that is created in a pseudo-random
/// fashion.
/// ```rust
/// ```
/// let rand_boards: Board = RandBoard::new()
/// .pseudo_random(12455)
/// .min_moves(5)
/// .one();
/// ```
///
/// Create a `Vec` of 10 random `Board`s that are guaranteedd to not be in check.
/// ```rust
/// Create a `Vec` of 10 random `Board`s that are guaranteed to not be in check.
/// ```
/// let rand_boards: Vec<Board> = RandBoard::new()
/// .pseudo_random(12455)
/// .no_check(5)
Expand Down
10 changes: 10 additions & 0 deletions pleco/src/board/piece_locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl PieceLocations {
/// # Panics
///
/// Panics if Square is of index higher than 63.
#[inline]
pub fn place(&mut self, square: SQ, player: Player, piece: Piece) {
assert!(square.is_okay());
self.data[square.0 as usize] = self.create_sq(player, piece);
Expand All @@ -59,6 +60,7 @@ impl PieceLocations {
/// # Panics
///
/// Panics if Square is of index higher than 63.
#[inline]
pub fn remove(&mut self, square: SQ) {
assert!(square.is_okay());
self.data[square.0 as usize] = 0b0111
Expand All @@ -69,6 +71,7 @@ impl PieceLocations {
/// # Panics
///
/// Panics if square is of index higher than 63.
#[inline]
pub fn piece_at(&self, square: SQ) -> Option<Piece> {
assert!(square.is_okay());
let byte: u8 = self.data[square.0 as usize] & 0b0111;
Expand All @@ -93,6 +96,7 @@ impl PieceLocations {
/// # Panics
///
/// Panics if Square is of index higher than 63.
#[inline]
pub fn piece_at_for_player(&self, square: SQ, player: Player) -> Option<Piece> {
let op = self.player_piece_at(square);
if op.is_some() {
Expand All @@ -112,6 +116,7 @@ impl PieceLocations {
/// # Panics
///
/// Panics if Square is of index higher than 63.
#[inline]
pub fn player_at(&self, square: SQ) -> Option<Player> {
let byte: u8 = self.data[square.0 as usize];
if byte == 0b0111 || byte == 0b1111 {
Expand All @@ -130,6 +135,7 @@ impl PieceLocations {
/// # Panics
///
/// Panics if Square is of index higher than 63.
#[inline]
pub fn player_piece_at(&self, square: SQ) -> Option<(Player, Piece)> {
let byte: u8 = self.data[square.0 as usize];
match byte {
Expand All @@ -153,13 +159,15 @@ impl PieceLocations {
}

/// Returns if there is a `SQ` is occupied.
#[inline]
pub fn at_square(&self, square: SQ) -> bool {
assert!(square.is_okay());
let byte: u8 = self.data[square.0 as usize];
byte != 0b0111 && byte != 0b1111
}

/// Returns the first square (if any) that a piece / player is at.
#[inline]
pub fn first_square(&self, piece: Piece, player: Player) -> Option<SQ> {
let target = self.create_sq(player, piece);
for x in 0..64 {
Expand All @@ -171,6 +179,7 @@ impl PieceLocations {
}

/// Returns if the Board contains a particular piece / player.
#[inline]
pub fn contains(&self, piece: Piece, player: Player) -> bool {
self.first_square(piece,player).is_some()
}
Expand Down Expand Up @@ -221,6 +230,7 @@ impl PieceLocations {


/// Helper method to return the bit representation of a given piece and player.
#[inline]
fn create_sq(&self, player: Player, piece: Piece) -> u8 {
let mut loc: u8 = match piece {
Piece::P => 0b0000,
Expand Down
Loading

0 comments on commit d9b3309

Please sign in to comment.