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.
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 }) {
// 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<T: Component>(
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()
);
}

Loading…
Cancel
Save