aboutsummaryrefslogtreecommitdiff
path: root/src/board
diff options
context:
space:
mode:
Diffstat (limited to 'src/board')
-rw-r--r--src/board/mod.rs21
-rw-r--r--src/board/piece.rs11
2 files changed, 32 insertions, 0 deletions
diff --git a/src/board/mod.rs b/src/board/mod.rs
index 1e4fd93..01a5b03 100644
--- a/src/board/mod.rs
+++ b/src/board/mod.rs
@@ -392,6 +392,27 @@ impl Board {
pub fn threefold_repetition(&self) -> bool {
self.positions.iter().filter(|&&p| p == self.hash).count() >= 3
}
+
+ pub fn is_theoretically_winnable(&self, color: Color) -> bool {
+ if [
+ Piece::Pawn,
+ Piece::Rook,
+ Piece::Queen,
+ ].iter().any(|&piece| {
+ self.piece_sets[piece.colored(color) as usize].pop_count() > 0
+ }) {
+ return true;
+ };
+
+ let minor_pieces = [
+ Piece::Knight,
+ Piece::Bishop,
+ ].iter().fold(0, |acc, &piece| {
+ acc + self.piece_sets[piece.colored(color) as usize].pop_count()
+ });
+
+ minor_pieces > 1
+ }
}
diff --git a/src/board/piece.rs b/src/board/piece.rs
index 1889e94..3de27ca 100644
--- a/src/board/piece.rs
+++ b/src/board/piece.rs
@@ -1,3 +1,5 @@
+use super::color::Color;
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, num_enum::FromPrimitive)]
#[repr(usize)]
pub enum Piece {
@@ -21,6 +23,15 @@ impl Piece {
let index = *self as usize;
Self::from(index % 6)
}
+
+ /// Note: assumes self is without color!
+ pub fn colored(&self, color: Color) -> Self {
+ match color {
+ Color::White => *self,
+ Color::Black => Self::from(*self as usize + 6)
+ }
+ }
+
// Return the price of the peice
pub fn static_eval(&self) -> f32 {
match self.without_color() {