possible moves works as expectedgit add .; git commit (now to filter down to actually valid moves...

bevy0.12
Elijah C. Voigt 2 years ago
parent 1eba3d962e
commit ea99af748c

@ -207,43 +207,54 @@ impl Board {
/// Returns the possible moves the piece at this tile can make. /// Returns the possible moves the piece at this tile can make.
pub(crate) fn possible_moves(&self, BoardIndex { x, y }: BoardIndex) -> HashSet<BoardIndex> { pub(crate) fn possible_moves(&self, BoardIndex { x, y }: BoardIndex) -> HashSet<BoardIndex> {
let f = |(a, b): (Option<usize>, Option<usize>)| {
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 }) { match self.at(BoardIndex { x, y }) {
// One space in any diagonal // One space in any diagonal
Some(Piece::Pawn) => (-1..=1) Some(Piece::Pawn) => std::iter::empty()
.flat_map(move |xi| { .chain(
(-1..=1).map(move |yi| BoardIndex { (-1..=1)
x: x.checked_add_signed(xi).map_or(0, |val| val).clamp(0, 7), .zip(-1..=1)
y: y.checked_add_signed(yi).map_or(0, |val| val).clamp(0, 3), .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(), .collect(),
// One or two spaces in either horizontal // One or two spaces in either horizontal
Some(Piece::Drone) => std::iter::empty() Some(Piece::Drone) => std::iter::empty()
.chain((-2..=2).map(|i| BoardIndex { .chain((-2..=2).map(|i| (x.checked_add_signed(i), Some(y))))
x: x.checked_add_signed(i).map_or(0, |val| val).clamp(0, 7), .chain((-2..=2).map(|i| (Some(x), y.checked_add_signed(i))))
y, .filter_map(f)
}))
.chain((-2..=2).map(|i| BoardIndex {
x,
y: y.checked_add_signed(i).map_or(0, |val| val).clamp(0, 3),
}))
.collect(), .collect(),
// Any distance in any straight line // Any distance in any straight line
Some(Piece::Queen) => std::iter::empty() Some(Piece::Queen) => std::iter::empty()
.chain((-7..=7).map(|i| BoardIndex { .chain((-7..=7).map(|i| (x.checked_add_signed(i), Some(y))))
x: x.checked_add_signed(i).map_or(0, |val| val).clamp(0, 7), .chain((-3..=3).map(|i| (Some(x), y.checked_add_signed(i))))
y, .chain(
})) (-3..=3)
.chain((-3..=3).map(|i| BoardIndex { .zip(-3..=3)
x, .map(move |(a, b)| (x.checked_add_signed(a), y.checked_add_signed(b))),
y: y.checked_add_signed(i).map_or(0, |val| val).clamp(0, 3), )
})) .chain(
.chain((-3..=3).flat_map(move |xi| { (-3..=3)
(-3..=3).map(move |yi| BoardIndex { .zip((-3..=3).rev())
x: x.checked_add_signed(xi).map_or(0, |val| val).clamp(0, 7), .map(move |(a, b)| (x.checked_add_signed(a), y.checked_add_signed(b))),
y: y.checked_add_signed(yi).map_or(0, |val| val).clamp(0, 3), )
}) .filter_map(f)
}))
.collect(), .collect(),
None => std::iter::empty().collect(), None => std::iter::empty().collect(),
} }
@ -468,9 +479,9 @@ fn asserts<T: Component>(
if selected_pieces.iter().len() > 2 { if selected_pieces.iter().len() > 2 {
panic!("More than two piece is selected"); panic!("More than two piece is selected");
} }
if selected_tiles.iter().len() > 1 { if selected_tiles.iter().len() > 2 {
panic!( panic!(
"More than one tile is selected {:?}", "More than two tile is selected {:?}",
selected_tiles.iter().len() selected_tiles.iter().len()
); );
} }

Loading…
Cancel
Save