Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 7 additions & 9 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,14 @@ impl Board {
/// ```
#[inline]
pub fn status(&self) -> BoardStatus {
let moves = MoveGen::new_legal(&self).len();
match moves {
0 => {
if self.checkers == EMPTY {
BoardStatus::Stalemate
} else {
BoardStatus::Checkmate
}
if MoveGen::has_legal_moves(&self) {
BoardStatus::Ongoing
} else {
if self.checkers == EMPTY {
BoardStatus::Stalemate
} else {
BoardStatus::Checkmate
}
_ => BoardStatus::Ongoing,
}
}

Expand Down
39 changes: 39 additions & 0 deletions src/movegen/movegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,45 @@ impl MoveGen {
movelist
}

/// Check whether the board has any legal moves or not
#[inline(always)]
pub fn has_legal_moves(board: &Board) -> bool {
let checkers = *board.checkers();
let mask = !board.color_combined(board.side_to_move());
let mut movelist = NoDrop::new(ArrayVec::<[SquareAndBitBoard; 18]>::new());

let legal_fns = if checkers == EMPTY {
[
PawnType::legals::<NotInCheckType> ,
KnightType::legals::<NotInCheckType>,
BishopType::legals::<NotInCheckType>,
RookType::legals::<NotInCheckType> ,
QueenType::legals::<NotInCheckType> ,
KingType::legals::<NotInCheckType> ,
]
} else if checkers.popcnt() == 1 {
[
PawnType::legals::<InCheckType> ,
KnightType::legals::<InCheckType>,
BishopType::legals::<InCheckType>,
RookType::legals::<InCheckType> ,
QueenType::legals::<InCheckType> ,
KingType::legals::<InCheckType> ,
]
} else {
KingType::legals::<InCheckType>(&mut movelist, &board, mask);
return movelist.len() != 0
};

for f in legal_fns {
f(&mut movelist, &board, mask);
if movelist.len() != 0 { return true }
movelist.clear();
}

return false
}

/// Create a new `MoveGen` structure, only generating legal moves
#[inline(always)]
pub fn new_legal(board: &Board) -> MoveGen {
Expand Down