From a310274c6d5acea8f5f83f2feb8699e7b312f8e3 Mon Sep 17 00:00:00 2001 From: Mike White Date: Fri, 27 Aug 2021 20:46:18 -0400 Subject: Highlighted possible moves when a piece is clicked --- model/src/coordinates.rs | 10 +++++++++- model/src/moves.rs | 29 ++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) (limited to 'model/src') diff --git a/model/src/coordinates.rs b/model/src/coordinates.rs index a055f46..7aea88b 100644 --- a/model/src/coordinates.rs +++ b/model/src/coordinates.rs @@ -7,7 +7,7 @@ pub struct SquareCoordinate { } impl SquareCoordinate { - pub(crate) fn new(rank: u8, file: u8) -> Self { + pub fn new(rank: u8, file: u8) -> Self { if rank > 32 { panic!("A Square cannot have a rank greater than 32. Got {}", rank) } else if file > 32 { @@ -56,6 +56,14 @@ impl SquareCoordinate { VALUE_COORDINATE_MAP[value] } + pub fn rank(self) -> u8 { + self.rank + } + + pub fn file(self) -> u8 { + self.file + } + pub fn to_value(self) -> Option { if self.rank % 2 == 0 { if self.file % 2 == 0 { diff --git a/model/src/moves.rs b/model/src/moves.rs index b8e58ee..7581b6b 100644 --- a/model/src/moves.rs +++ b/model/src/moves.rs @@ -38,6 +38,29 @@ impl Move { } } + pub const fn start(self) -> u32 { + self.start + } + + /// Calculates the value of the end position of the move + pub const fn end_position(self) -> usize { + let dest = match self.jump { + false => match self.direction { + MoveDirection::ForwardLeft => self.start + 7, + MoveDirection::ForwardRight => self.start + 1, + MoveDirection::BackwardLeft => self.start - 1, + MoveDirection::BackwardRight => self.start - 7, + }, + true => match self.direction { + MoveDirection::ForwardLeft => self.start + 14, + MoveDirection::ForwardRight => self.start + 2, + MoveDirection::BackwardLeft => self.start - 2, + MoveDirection::BackwardRight => self.start - 14, + }, + }; + dest as usize + } + /// Apply the move to a board. This does not mutate the original board, /// but instead returns a new one. /// @@ -92,10 +115,10 @@ impl Move { #[cfg(test)] mod tests { - use proptest::prelude::*; use super::*; + use proptest::prelude::*; - proptest!{ + proptest! { #[test] fn new(start in 0usize..32, jump in proptest::bool::ANY) { let direction = MoveDirection::ForwardLeft; @@ -123,4 +146,4 @@ mod tests { assert_eq!(move_test.jump, jump); } } -} \ No newline at end of file +} -- cgit v1.2.3