From ea99af748cc7bf84891ed0a7839efa26fd30487e Mon Sep 17 00:00:00 2001 From: "Elijah C. Voigt" Date: Wed, 8 Nov 2023 22:41:21 -0800 Subject: [PATCH] possible moves works as expectedgit add .; git commit (now to filter down to actually valid moves... --- src/game.rs | 73 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/src/game.rs b/src/game.rs index 733ed0f..6d81c65 100644 --- a/src/game.rs +++ b/src/game.rs @@ -207,43 +207,54 @@ impl Board { /// Returns the possible moves the piece at this tile can make. pub(crate) fn possible_moves(&self, BoardIndex { x, y }: BoardIndex) -> HashSet { + let f = |(a, b): (Option, Option)| { + if let (Some(x), Some(y)) = (a, b) { + if (0..=7).contains(&x) && (0..=3).contains(&y) { + Some(BoardIndex { x, y }) + } else { + None + } + } else { + None + } + }; + match self.at(BoardIndex { x, y }) { // One space in any diagonal - Some(Piece::Pawn) => (-1..=1) - .flat_map(move |xi| { - (-1..=1).map(move |yi| BoardIndex { - x: x.checked_add_signed(xi).map_or(0, |val| val).clamp(0, 7), - y: y.checked_add_signed(yi).map_or(0, |val| val).clamp(0, 3), - }) - }) + Some(Piece::Pawn) => std::iter::empty() + .chain( + (-1..=1) + .zip(-1..=1) + .map(move |(a, b)| (x.checked_add_signed(a), y.checked_add_signed(b))), + ) + .chain( + (-1..=1) + .zip((-1..=1).rev()) + .map(move |(a, b)| (x.checked_add_signed(a), y.checked_add_signed(b))), + ) + .filter_map(f) .collect(), // One or two spaces in either horizontal Some(Piece::Drone) => std::iter::empty() - .chain((-2..=2).map(|i| BoardIndex { - x: x.checked_add_signed(i).map_or(0, |val| val).clamp(0, 7), - y, - })) - .chain((-2..=2).map(|i| BoardIndex { - x, - y: y.checked_add_signed(i).map_or(0, |val| val).clamp(0, 3), - })) + .chain((-2..=2).map(|i| (x.checked_add_signed(i), Some(y)))) + .chain((-2..=2).map(|i| (Some(x), y.checked_add_signed(i)))) + .filter_map(f) .collect(), // Any distance in any straight line Some(Piece::Queen) => std::iter::empty() - .chain((-7..=7).map(|i| BoardIndex { - x: x.checked_add_signed(i).map_or(0, |val| val).clamp(0, 7), - y, - })) - .chain((-3..=3).map(|i| BoardIndex { - x, - y: y.checked_add_signed(i).map_or(0, |val| val).clamp(0, 3), - })) - .chain((-3..=3).flat_map(move |xi| { - (-3..=3).map(move |yi| BoardIndex { - x: x.checked_add_signed(xi).map_or(0, |val| val).clamp(0, 7), - y: y.checked_add_signed(yi).map_or(0, |val| val).clamp(0, 3), - }) - })) + .chain((-7..=7).map(|i| (x.checked_add_signed(i), Some(y)))) + .chain((-3..=3).map(|i| (Some(x), y.checked_add_signed(i)))) + .chain( + (-3..=3) + .zip(-3..=3) + .map(move |(a, b)| (x.checked_add_signed(a), y.checked_add_signed(b))), + ) + .chain( + (-3..=3) + .zip((-3..=3).rev()) + .map(move |(a, b)| (x.checked_add_signed(a), y.checked_add_signed(b))), + ) + .filter_map(f) .collect(), None => std::iter::empty().collect(), } @@ -468,9 +479,9 @@ fn asserts( if selected_pieces.iter().len() > 2 { panic!("More than two piece is selected"); } - if selected_tiles.iter().len() > 1 { + if selected_tiles.iter().len() > 2 { panic!( - "More than one tile is selected {:?}", + "More than two tile is selected {:?}", selected_tiles.iter().len() ); }