diff --git a/RULES.txt b/RULES.txt index e2853ef..33c7134 100644 --- a/RULES.txt +++ b/RULES.txt @@ -1,9 +1,9 @@ - https://www.looneylabs.com/rules/martian-chess -- [ ] Movement - - [ ] Pawn movement - - [ ] Drone movement - - [ ] Queen movement -- [ ] No move reversal +- [x] Movement + - [x] Pawn movement + - [x] Drone movement + - [x] Queen movement +- [x] No move reversal - [ ] Scoring - Queen: 3 - Drone: 2 diff --git a/src/display3d.rs b/src/display3d.rs index 5955221..c8d2e68 100644 --- a/src/display3d.rs +++ b/src/display3d.rs @@ -587,7 +587,7 @@ fn put_down( mut players: Query<&mut AnimationPlayer>, ) { events.iter().for_each(|entity| { - if let Ok(piece) = query.get_mut(entity) { + if let Ok(_piece) = query.get_mut(entity) { let gltf = gltfs.get(&assets_map.models).expect("Load GLTF content"); children.iter_descendants(entity).for_each(|child| { if let Ok(mut player) = players.get_mut(child) { diff --git a/src/game.rs b/src/game.rs index 70bb1e9..da3d1da 100644 --- a/src/game.rs +++ b/src/game.rs @@ -98,6 +98,7 @@ impl std::fmt::Display for Piece { pub(crate) enum GameError { NullMove, InvalidIndex, + InvalidMove, } /// The board is setup like this: @@ -199,7 +200,7 @@ impl Board { Ok(moves) } else { - Err(GameError::InvalidIndex) + Err(GameError::InvalidMove) } } None => Err(GameError::NullMove), @@ -218,7 +219,9 @@ impl Board { /// Returns the possible moves the piece at this tile can make. /// !!TODO: exclude pieces on your own side!! - pub(crate) fn valid_moves(&self, BoardIndex { x, y }: BoardIndex) -> HashSet { + pub(crate) fn valid_moves(&self, current_board_index: BoardIndex) -> HashSet { + let BoardIndex { x, y } = current_board_index; + let f = |(a, b): (Option, Option)| { if let (Some(this_x), Some(this_y)) = (a, b) { // This has a valid x position @@ -236,14 +239,38 @@ impl Board { let valid_capture = { match self.at(this_board_index) { Some(_) => { - let same_side = Board::side(BoardIndex { x, y }); + let same_side = Board::side(current_board_index); Board::side(this_board_index) != same_side } None => true, } }; if valid_capture { - Some(this_board_index) + // You cannot move a piece from SideB->SideA when it was just moved from SideA->SideB + // Move rejection is not allowed + let rejection = { + if let Some(Move { + from: last_from, + to: Some(last_to), + .. + }) = self.moves.last() + { + // TODO: I think this is more logic than we need to express + // the sentiment... + Board::side(*last_from) == Board::side(this_board_index) + && Board::side(*last_to) == Board::side(current_board_index) + && Board::side(this_board_index) + != Board::side(current_board_index) + } else { + false + } + }; + if !rejection { + // If all tests pass, this is a valid move + Some(this_board_index) + } else { + None + } } else { None } @@ -495,8 +522,8 @@ fn handle_selection( } } Err(GameError::NullMove) => warn!("Null move!"), - Err(GameError::InvalidIndex) => { - warn!("Invalid index!"); + Err(GameError::InvalidIndex) | Err(GameError::InvalidMove) => { + warn!("Invalid index/move!"); if !(*done) { audio_event.send(AudioEvent::Invalid); *done = true; diff --git a/src/main.rs b/src/main.rs index 1cf1bc6..bc594e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ use std::time::Duration; use bevy::{ asset::{ChangeWatcher, HandleId}, - input::{keyboard::KeyboardInput, ButtonState}, + input::ButtonState, }; use crate::prelude::*;