Skip to content

Commit

Permalink
Continue implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillbobyrev committed Jun 2, 2024
1 parent 3d8d055 commit afdba2d
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 270 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"byteorder",
"capturable",
"CCRL",
"centipawn",
"clippy",
"Codecov",
"doctest",
Expand Down
7 changes: 7 additions & 0 deletions src/chess/attacks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
//! Mappings of occupied squares to the attacked squares for each piece. The
//! mappings are pre-calculated where possible to provide an efficient way of
//! generating moves.
//!
//! The implementation uses BMI2 (if available) for performance ([reference]),
//! specifically the PEXT instruction for [PEXT Bitboards].
//!
//! [reference]: https://www.chessprogramming.org/BMI2
//! [PEXT Bitboards]: https://www.chessprogramming.org/BMI2#PEXTBitboards
// TODO: This code is probably by far the least appealing in the project.
// Refactor it and make it nicer.

Expand Down
17 changes: 8 additions & 9 deletions src/chess/bitboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
//! that can be used together bitboard-based approach to compensate its
//! shortcomings).
//!
//! The implementation is based on [PEXT Bitboards] idea, which is an
//! improvement over Fancy Magic Bitboards.
//!
//! For visualizing and debugging the bitboards, there is a [BitboardCalculator] tool.
//!
//! [Bitboards]: https://www.chessprogramming.org/Bitboards
//! [BitboardCalculator]: https://gekomad.github.io/Cinnamon/BitboardCalculator/
//! [PEXT Bitboards]: https://www.chessprogramming.org/BMI2#PEXTBitboards
use std::fmt::Write;
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, Not, Shl, Shr, Sub, SubAssign};
Expand All @@ -18,15 +25,7 @@ use std::{fmt, mem};
use itertools::Itertools;

use crate::chess::core::{
Direction,
File,
Piece,
PieceKind,
Player,
Rank,
Square,
BOARD_SIZE,
BOARD_WIDTH,
Direction, File, Piece, PieceKind, Player, Rank, Square, BOARD_SIZE, BOARD_WIDTH,
};

/// Represents a set of squares and provides common operations (e.g. AND, OR,
Expand Down
21 changes: 8 additions & 13 deletions src/chess/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,7 @@ use anyhow::{bail, Context};
use crate::chess::attacks;
use crate::chess::bitboard::{Bitboard, Board, Pieces};
use crate::chess::core::{
CastleRights,
File,
Move,
MoveList,
Piece,
Player,
Promotion,
Rank,
Square,
BOARD_WIDTH,
CastleRights, File, Move, MoveList, Piece, Player, Promotion, Rank, Square, BOARD_WIDTH,
};

/// State of the chess game: board, half-move counters and castling rights,
Expand Down Expand Up @@ -89,7 +80,8 @@ impl Position {
}
}

#[must_use] pub fn empty() -> Self {
#[must_use]
pub fn empty() -> Self {

Check warning on line 84 in src/chess/position.rs

View workflow job for this annotation

GitHub Actions / Build docs

missing documentation for an associated function
Self {
board: Board::empty(),
castling: CastleRights::NONE,
Expand Down Expand Up @@ -534,15 +526,18 @@ impl Position {
/// Returns true if the player to move is in check.
#[must_use]
pub fn in_check(&self) -> bool {
// TODO: This is very expensive and is likely to be a bottleneck.
// Cache the attack info and/or whether the king is in check.
self.attack_info().checkers.has_any()
}

/// Returns true if the game is over.
#[must_use]
fn is_checkmate(&self) -> bool {
pub fn is_checkmate(&self) -> bool {
self.in_check() && self.generate_moves().is_empty()
}

fn is_stalemate(&self) -> bool {
pub fn is_stalemate(&self) -> bool {

Check warning on line 540 in src/chess/position.rs

View workflow job for this annotation

GitHub Actions / Build docs

missing documentation for a method
!self.in_check() && self.generate_moves().is_empty()
}
}
Expand Down
Loading

0 comments on commit afdba2d

Please sign in to comment.