From 33f37d57c4552e68f1af04a2f64cf4d54aed77b5 Mon Sep 17 00:00:00 2001 From: "Elijah C. Voigt" Date: Wed, 1 May 2024 21:23:34 -0700 Subject: [PATCH] I think it's working again *wipes brow* thank god. --- src/game.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/game.rs b/src/game.rs index bb2f754..147e311 100644 --- a/src/game.rs +++ b/src/game.rs @@ -119,9 +119,11 @@ impl Piece { } } - fn moves_at(&self, from: &BoardIndex) -> Vec { - self.moves().map(|(x, y)| { - *from + (*x, *y) + fn moves_at(&self, from: &BoardIndex) -> HashSet { + self.moves().filter_map(|(x, y)| { + let bi = *from + (*x, *y); + // Check if this goes out of bounds, if so exclude from the list of possible moves + (bi.x <= 7 && bi.y <= 3).then_some(bi) }) .collect() } @@ -287,8 +289,8 @@ impl std::ops::Add<(isize, isize)> for BoardIndex { fn add(self, (a, b): (isize, isize)) -> Self { BoardIndex { - x: self.x.saturating_add_signed(a).max(7), - y: self.y.saturating_add_signed(b).max(3), + x: self.x.saturating_add_signed(a), + y: self.y.saturating_add_signed(b), } } } @@ -298,8 +300,8 @@ impl std::ops::Add<(usize, usize)> for BoardIndex { fn add(self, (a, b): (usize, usize)) -> Self { BoardIndex { - x: self.x.saturating_add(a).max(7), - y: self.y.saturating_add(b).max(3), + x: self.x.saturating_add(a), + y: self.y.saturating_add(b), } } } @@ -538,7 +540,7 @@ impl Board { .moves_at(&from) .iter() // Find if the given `to` move is one of those - .find(|BoardIndex { x, y }| to == from + (*x, *y)) + .find(|idx| to == **idx) // Determine if this is valid/legal in this situation .and_then(|_| { let dest_at = self.at(to); @@ -798,6 +800,16 @@ mod test { ); } } + + #[test] + fn test_piece_moves_at() { + let given = Piece::Drone.moves_at(&(4, 1).into()); + let expected: HashSet = HashSet::from([ + (4, 0).into(), (2, 1).into(), (3, 1).into(), (5, 1).into(), (6, 1).into(), (4, 2).into(), (4, 3).into(), + ]); + + assert_eq!(expected, given, "Drone moves at"); + } } impl std::fmt::Display for Board {