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,35 +167,40 @@ impl Board {
Err(GameError::NullMove) Err(GameError::NullMove)
} else { } else {
match self.at(from.clone()) { match self.at(from.clone()) {
Some(from_val) => { Some(from_piece) => {
// The current epoch is the last epoch + 1 // Check if this is a valid move for this piece
let epoch = self.current_epoch(); if self.possible_moves(from).contains(&to) {
// The current epoch is the last epoch + 1
// Local moves vec we can return let epoch = self.current_epoch();
let mut moves = vec![];
// Local moves vec we can return
let mut moves = vec![];
// If the position we are moving to is occupied, capture the removal in the ledger
if self.inner[to.y][to.x].is_some() {
moves.push(Move {
epoch,
from: to.clone(),
to: None,
});
}
// If the position we are moving to is occupied, capture the removal in the ledger // Capture the intened move in the moves ledger
if self.inner[to.y][to.x].is_some() {
moves.push(Move { moves.push(Move {
epoch, epoch,
from: to.clone(), from: from.clone(),
to: None, to: Some(to.clone()),
}); });
}
// Capture the intened move in the moves ledger self.inner[to.y][to.x] = Some(*from_piece);
moves.push(Move { self.inner[from.y][from.x] = None;
epoch,
from: from.clone(),
to: Some(to.clone()),
});
self.inner[to.y][to.x] = Some(*from_val);
self.inner[from.y][from.x] = None;
self.moves.extend(moves.clone()); self.moves.extend(moves.clone());
Ok(moves) Ok(moves)
} else {
Err(GameError::InvalidIndex)
}
} }
None => Err(GameError::NullMove), None => Err(GameError::NullMove),
} }
@ -212,6 +217,7 @@ impl Board {
} }
/// Returns the possible moves the piece at this tile can make. /// 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> { pub(crate) fn possible_moves(&self, BoardIndex { x, y }: BoardIndex) -> HashSet<BoardIndex> {
let f = |(a, b): (Option<usize>, Option<usize>)| { let f = |(a, b): (Option<usize>, Option<usize>)| {
if let (Some(x), Some(y)) = (a, b) { if let (Some(x), Some(y)) = (a, b) {

Loading…
Cancel
Save