diff options
| -rw-r--r-- | engine/src/lib.rs | 18 | ||||
| -rw-r--r-- | model/src/moves.rs | 3 | ||||
| -rw-r--r-- | model/src/possible_moves.rs | 16 |
3 files changed, 37 insertions, 0 deletions
diff --git a/engine/src/lib.rs b/engine/src/lib.rs index 6a12b83..8139530 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -20,6 +20,7 @@ mod transposition_table; pub const ENGINE_NAME: &str = "Ampere"; pub const ENGINE_AUTHOR: &str = "Mica White"; +pub const ENGINE_ABOUT: &str = "Ampere Checkers Bot v1.0\nCopyright Mica White"; pub struct Engine<'a> { position: Mutex<CheckersBitBoard>, @@ -160,6 +161,11 @@ impl<'a> Engine<'a> { self.debug.store(debug, Ordering::Release); } + pub fn is_legal_move(&self, checker_move: Move) -> bool { + let position = self.position.lock(); + PossibleMoves::moves(*position).contains(checker_move) + } + pub fn reset_position(&self) { self.set_position(CheckersBitBoard::starting_position()) } @@ -169,6 +175,18 @@ impl<'a> Engine<'a> { *position_ptr = position; } + pub fn apply_move(&self, checker_move: Move) -> Option<()> { + unsafe { + if self.is_legal_move(checker_move) { + let mut position = self.position.lock(); + *position = checker_move.apply_to(*position); + Some(()) + } else { + None + } + } + } + pub fn start_evaluation(&'static self, settings: EvaluationSettings) { // finish the pondering thread let mut pondering_task = self.pondering_task.lock(); diff --git a/model/src/moves.rs b/model/src/moves.rs index f6b1fce..720ded6 100644 --- a/model/src/moves.rs +++ b/model/src/moves.rs @@ -89,6 +89,9 @@ impl Move { /// This functions results in undefined behavior if: /// * The piece moves in a direction which would move it outside of the board /// * The starting position of this move doesn't contain a piece + /// * The end position already contains a piece + /// * A jump occurs where jumps are not allowed + /// * A move is not a jump even though jumps are available pub const unsafe fn apply_to(self, board: CheckersBitBoard) -> CheckersBitBoard { match self.jump { false => match self.direction { diff --git a/model/src/possible_moves.rs b/model/src/possible_moves.rs index 3dbc45b..0319d93 100644 --- a/model/src/possible_moves.rs +++ b/model/src/possible_moves.rs @@ -731,6 +731,22 @@ impl PossibleMoves { pub const fn can_jump(self) -> bool { (self.backward_right_movers & 2) != 0 } + + /// Returns true if the given move is possible + pub const fn contains(self, checker_move: Move) -> bool { + if checker_move.is_jump() != self.can_jump() { + return false; + } + + let bits = match checker_move.direction() { + MoveDirection::ForwardLeft => self.forward_left_movers, + MoveDirection::ForwardRight => self.forward_right_movers, + MoveDirection::BackwardLeft => self.backward_left_movers, + MoveDirection::BackwardRight => self.backward_right_movers, + }; + + (bits >> checker_move.start()) & 1 == 1 + } } #[cfg(test)] |
