Restrict movement to valid moves

Now need to fix valid moves to be accurate
bevy0.12
Elijah Voigt 2 years ago
parent a58151ee52
commit 1fcd30b026

@ -167,7 +167,9 @@ impl Board {
Err(GameError::NullMove)
} else {
match self.at(from.clone()) {
Some(from_val) => {
Some(from_piece) => {
// Check if this is a valid move for this piece
if self.possible_moves(from).contains(&to) {
// The current epoch is the last epoch + 1
let epoch = self.current_epoch();
@ -190,12 +192,15 @@ impl Board {
to: Some(to.clone()),
});
self.inner[to.y][to.x] = Some(*from_val);
self.inner[to.y][to.x] = Some(*from_piece);
self.inner[from.y][from.x] = None;
self.moves.extend(moves.clone());
Ok(moves)
} else {
Err(GameError::InvalidIndex)
}
}
None => Err(GameError::NullMove),
}
@ -212,6 +217,7 @@ impl Board {
}
/// Returns the possible moves the piece at this tile can make.
/// !!TODO: exclude pieces on your own side!!
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) {

Loading…
Cancel
Save