diff --git a/src/display3d.rs b/src/display3d.rs index 6b603a3..5955221 100644 --- a/src/display3d.rs +++ b/src/display3d.rs @@ -539,7 +539,7 @@ fn moves_gizmo( ) { selected.iter().for_each(|idx| { board - .possible_moves(*idx) + .valid_moves(*idx) .iter() .map(|i| Transform::from_translation(board_translation(i))) .for_each(|t| gizmos.cuboid(t, Color::WHITE)) diff --git a/src/game.rs b/src/game.rs index 70249f2..70bb1e9 100644 --- a/src/game.rs +++ b/src/game.rs @@ -94,7 +94,7 @@ impl std::fmt::Display for Piece { } } -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub(crate) enum GameError { NullMove, InvalidIndex, @@ -169,7 +169,7 @@ impl Board { match self.at(from.clone()) { Some(from_piece) => { // Check if this is a valid move for this piece - if self.possible_moves(from).contains(&to) { + if self.valid_moves(from).contains(&to) { // The current epoch is the last epoch + 1 let epoch = self.current_epoch(); @@ -218,11 +218,38 @@ 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 { + pub(crate) fn valid_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 }) + if let (Some(this_x), Some(this_y)) = (a, b) { + // This has a valid x position + let valid_x = (0..=7).contains(&this_x); + if valid_x { + // It has a valid y position + let valid_y = (0..=3).contains(&this_y); + if valid_y { + // The checked board index + let this_board_index = BoardIndex { + x: this_x, + y: this_y, + }; + // Only propose tiles that are empty or capture a piece on the other side + let valid_capture = { + match self.at(this_board_index) { + Some(_) => { + let same_side = Board::side(BoardIndex { x, y }); + Board::side(this_board_index) != same_side + } + None => true, + } + }; + if valid_capture { + Some(this_board_index) + } else { + None + } + } else { + None + } } else { None }