Rule: No move rejections (sideA->sideB->sideA)

bevy0.12
Elijah Voigt 2 years ago
parent d737e9c10d
commit cc0299a65a

@ -1,9 +1,9 @@
- https://www.looneylabs.com/rules/martian-chess - https://www.looneylabs.com/rules/martian-chess
- [ ] Movement - [x] Movement
- [ ] Pawn movement - [x] Pawn movement
- [ ] Drone movement - [x] Drone movement
- [ ] Queen movement - [x] Queen movement
- [ ] No move reversal - [x] No move reversal
- [ ] Scoring - [ ] Scoring
- Queen: 3 - Queen: 3
- Drone: 2 - Drone: 2

@ -587,7 +587,7 @@ fn put_down(
mut players: Query<&mut AnimationPlayer>, mut players: Query<&mut AnimationPlayer>,
) { ) {
events.iter().for_each(|entity| { 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"); let gltf = gltfs.get(&assets_map.models).expect("Load GLTF content");
children.iter_descendants(entity).for_each(|child| { children.iter_descendants(entity).for_each(|child| {
if let Ok(mut player) = players.get_mut(child) { if let Ok(mut player) = players.get_mut(child) {

@ -98,6 +98,7 @@ impl std::fmt::Display for Piece {
pub(crate) enum GameError { pub(crate) enum GameError {
NullMove, NullMove,
InvalidIndex, InvalidIndex,
InvalidMove,
} }
/// The board is setup like this: /// The board is setup like this:
@ -199,7 +200,7 @@ impl Board {
Ok(moves) Ok(moves)
} else { } else {
Err(GameError::InvalidIndex) Err(GameError::InvalidMove)
} }
} }
None => Err(GameError::NullMove), None => Err(GameError::NullMove),
@ -218,7 +219,9 @@ 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!! /// !!TODO: exclude pieces on your own side!!
pub(crate) fn valid_moves(&self, BoardIndex { x, y }: BoardIndex) -> HashSet<BoardIndex> { pub(crate) fn valid_moves(&self, current_board_index: BoardIndex) -> HashSet<BoardIndex> {
let BoardIndex { x, y } = current_board_index;
let f = |(a, b): (Option<usize>, Option<usize>)| { let f = |(a, b): (Option<usize>, Option<usize>)| {
if let (Some(this_x), Some(this_y)) = (a, b) { if let (Some(this_x), Some(this_y)) = (a, b) {
// This has a valid x position // This has a valid x position
@ -236,14 +239,38 @@ impl Board {
let valid_capture = { let valid_capture = {
match self.at(this_board_index) { match self.at(this_board_index) {
Some(_) => { Some(_) => {
let same_side = Board::side(BoardIndex { x, y }); let same_side = Board::side(current_board_index);
Board::side(this_board_index) != same_side Board::side(this_board_index) != same_side
} }
None => true, None => true,
} }
}; };
if valid_capture { 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 { } else {
None None
} }
@ -495,8 +522,8 @@ fn handle_selection(
} }
} }
Err(GameError::NullMove) => warn!("Null move!"), Err(GameError::NullMove) => warn!("Null move!"),
Err(GameError::InvalidIndex) => { Err(GameError::InvalidIndex) | Err(GameError::InvalidMove) => {
warn!("Invalid index!"); warn!("Invalid index/move!");
if !(*done) { if !(*done) {
audio_event.send(AudioEvent::Invalid); audio_event.send(AudioEvent::Invalid);
*done = true; *done = true;

@ -17,7 +17,7 @@ use std::time::Duration;
use bevy::{ use bevy::{
asset::{ChangeWatcher, HandleId}, asset::{ChangeWatcher, HandleId},
input::{keyboard::KeyboardInput, ButtonState}, input::ButtonState,
}; };
use crate::prelude::*; use crate::prelude::*;

Loading…
Cancel
Save