diff options
| author | mrw1593 <botahamec@outlook.com> | 2021-07-13 17:54:25 -0400 |
|---|---|---|
| committer | mrw1593 <botahamec@outlook.com> | 2021-07-13 17:54:25 -0400 |
| commit | bf9e9af7f45433def26d2d2094a04798cb0282c0 (patch) | |
| tree | 786e145253a329c4da28edc94eae68b719b6d45d /model/src/moves.rs | |
| parent | d2b28aa31a84f43bf5c4f375357e0f013337cdaf (diff) | |
Updated docs and tests
Diffstat (limited to 'model/src/moves.rs')
| -rw-r--r-- | model/src/moves.rs | 148 |
1 files changed, 89 insertions, 59 deletions
diff --git a/model/src/moves.rs b/model/src/moves.rs index 30024d6..626d85a 100644 --- a/model/src/moves.rs +++ b/model/src/moves.rs @@ -1,59 +1,89 @@ -use crate::CheckersBitBoard;
-
-#[derive(Copy, Clone, Eq, PartialEq)]
-pub enum MoveDirection {
- ForwardLeft = 0,
- ForwardRight = 1,
- BackwardLeft = 2,
- BackwardRight = 3,
-}
-
-#[derive(Copy, Clone)]
-pub struct Move {
- start: u32,
- direction: MoveDirection,
- jump: bool,
-}
-
-impl Move {
- pub const fn new(start: usize, direction: MoveDirection, jump: bool) -> Self {
- Self {
- start: start as u32,
- direction,
- jump,
- }
- }
-
- pub const unsafe fn apply_to(self, board: CheckersBitBoard) -> CheckersBitBoard {
- match self.jump {
- false => match self.direction {
- MoveDirection::ForwardLeft => {
- board.move_piece_forward_left_unchecked(self.start as usize)
- }
- MoveDirection::ForwardRight => {
- board.move_piece_forward_right_unchecked(self.start as usize)
- }
- MoveDirection::BackwardLeft => {
- board.move_piece_backward_left_unchecked(self.start as usize)
- }
- MoveDirection::BackwardRight => {
- board.move_piece_backward_right_unchecked(self.start as usize)
- }
- },
- true => match self.direction {
- MoveDirection::ForwardLeft => {
- board.jump_piece_forward_left_unchecked(self.start as usize)
- }
- MoveDirection::ForwardRight => {
- board.jump_piece_forward_right_unchecked(self.start as usize)
- }
- MoveDirection::BackwardLeft => {
- board.jump_piece_backward_left_unchecked(self.start as usize)
- }
- MoveDirection::BackwardRight => {
- board.jump_piece_backward_right_unchecked(self.start as usize)
- }
- },
- }
- }
-}
+use crate::CheckersBitBoard; + +#[derive(Copy, Clone, Eq, PartialEq)] +pub enum MoveDirection { + ForwardLeft = 0, + ForwardRight = 1, + BackwardLeft = 2, + BackwardRight = 3, +} + +/// A checkers move +#[derive(Copy, Clone)] +pub struct Move { + /// The position of the piece to move + start: u32, + + /// The direction to move to + direction: MoveDirection, + + /// Whether or not it's a jump + jump: bool, +} + +impl Move { + /// Create a new move + /// + /// # Arguments + /// + /// * `start` - The location of the piece that should move + /// * `direction` - The direction the piece should move in + /// * `jump` - Whether or not the piece should jump + pub const fn new(start: usize, direction: MoveDirection, jump: bool) -> Self { + Self { + start: start as u32, + direction, + jump, + } + } + + /// Apply the move to a board. This does not mutate the original board, + /// but instead returns a new one. + /// + /// # Arguments + /// + /// * `board` - The board to apply the move to + /// + /// # Panics + /// + /// Panics if the starting position of this move is greater than or equal to 32 + /// + /// # Safety + /// + /// Applying an illegal move to the board is undefined behavior. + /// 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 + pub const unsafe fn apply_to(self, board: CheckersBitBoard) -> CheckersBitBoard { + match self.jump { + false => match self.direction { + MoveDirection::ForwardLeft => { + board.move_piece_forward_left_unchecked(self.start as usize) + } + MoveDirection::ForwardRight => { + board.move_piece_forward_right_unchecked(self.start as usize) + } + MoveDirection::BackwardLeft => { + board.move_piece_backward_left_unchecked(self.start as usize) + } + MoveDirection::BackwardRight => { + board.move_piece_backward_right_unchecked(self.start as usize) + } + }, + true => match self.direction { + MoveDirection::ForwardLeft => { + board.jump_piece_forward_left_unchecked(self.start as usize) + } + MoveDirection::ForwardRight => { + board.jump_piece_forward_right_unchecked(self.start as usize) + } + MoveDirection::BackwardLeft => { + board.jump_piece_backward_left_unchecked(self.start as usize) + } + MoveDirection::BackwardRight => { + board.jump_piece_backward_right_unchecked(self.start as usize) + } + }, + } + } +} |
