summaryrefslogtreecommitdiff
path: root/model/src
diff options
context:
space:
mode:
authorMike White <botahamec@outlook.com>2021-08-27 20:46:18 -0400
committerMike White <botahamec@outlook.com>2021-08-27 20:46:18 -0400
commita310274c6d5acea8f5f83f2feb8699e7b312f8e3 (patch)
tree9424e380512df43e64a50ad0824375d7f23b3c25 /model/src
parent68014734561c3a8d8712916bdfa58b8347131501 (diff)
Highlighted possible moves when a piece is clicked
Diffstat (limited to 'model/src')
-rw-r--r--model/src/coordinates.rs10
-rw-r--r--model/src/moves.rs29
2 files changed, 35 insertions, 4 deletions
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<usize> {
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
+}