From cdf0b52ea6c464e23da52564972c859cab61cdff Mon Sep 17 00:00:00 2001 From: "Elijah C. Voigt" Date: Wed, 8 Nov 2023 20:17:22 -0800 Subject: [PATCH] no progress is made, but code has changed --- src/display2d.rs | 2 +- src/display3d.rs | 2 +- src/game.rs | 91 +++++++++++++++++++++++++++++++----------------- 3 files changed, 61 insertions(+), 34 deletions(-) diff --git a/src/display2d.rs b/src/display2d.rs index a0171da..2cdce72 100644 --- a/src/display2d.rs +++ b/src/display2d.rs @@ -159,7 +159,7 @@ fn initialize_board(board: Option>, mut commands: Commands) { // Spawn pieces board.pieces().iter().for_each(|(index, piece)| { - let side = Board::side(index).expect("Spawn valid side"); + let side = Board::side(*index).expect("Spawn valid side"); parent.spawn(( piece.clone(), diff --git a/src/display3d.rs b/src/display3d.rs index e56e0bd..e524538 100644 --- a/src/display3d.rs +++ b/src/display3d.rs @@ -156,7 +156,7 @@ fn initialize(mut commands: Commands, board: Res, assets: Res, } -#[derive(Debug, Component, PartialEq, Clone, Default)] +#[derive(Debug, Component, PartialEq, Clone, Default, Copy)] pub(crate) struct BoardIndex { pub x: usize, pub y: usize, @@ -124,8 +124,8 @@ pub(crate) enum Side { impl Board { /// Returns the piece at the given location - pub(crate) fn at(&self, BoardIndex { x, y }: &BoardIndex) -> Option { - self.inner[*y][*x].clone() + pub(crate) fn at(&self, BoardIndex { x, y }: BoardIndex) -> Option<&Piece> { + self.inner[y][x].as_ref() } /// Returns a list of all pieces on the board with their location @@ -141,54 +141,81 @@ impl Board { .collect() } + /// Moves a piece from -> to pub(crate) fn move_piece( &mut self, - from: &BoardIndex, - to: &BoardIndex, + from: BoardIndex, + to: BoardIndex, ) -> Result, GameError> { if from == to { Err(GameError::NullMove) } else { - self.at(from).map_or(Err(GameError::NullMove), |from_val| { - // The current epoch is the last epoch + 1 - let epoch = self.moves.last().unwrap_or(&Move { ..default() }).epoch + 1; - - // Local moves vec we can return - let mut moves = vec![]; + match self.at(from.clone()) { + Some(from_val) => { + // The current epoch is the last epoch + 1 + let epoch = self.moves.last().unwrap_or(&Move { ..default() }).epoch + 1; + + // 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 - if self.inner[to.y][to.x].is_some() { + // Capture the intened move in the moves ledger moves.push(Move { epoch, - from: to.clone(), - to: None, + from: from.clone(), + to: Some(to.clone()), }); - } - // Capture the intened move in the moves ledger - moves.push(Move { - 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()); - self.inner[to.y][to.x] = Some(from_val); - self.inner[from.y][from.x] = None; - - Ok(moves) - }) + Ok(moves) + } + None => Err(GameError::NullMove), + } } } - pub(crate) fn side(&BoardIndex { x, .. }: &BoardIndex) -> Result { + /// Returns the Side of a piece + pub(crate) fn side(BoardIndex { x, .. }: BoardIndex) -> Result { match x { 0..=3 => Ok(Side::A), 4..=7 => Ok(Side::B), _ => Err(GameError::InvalidIndex), } } + + /// Returns the possible moves the piece at this tile can make. + pub(crate) fn possible_moves(&self, BoardIndex { x, y }: BoardIndex) -> Vec { + match self.at(BoardIndex { x, y }) { + // One space in any diagonal + Some(Piece::Pawn) => { + self.at(BoardIndex { x: x + 1, y }); + vec![] + } + // One or two spaces in either horizontal + Some(Piece::Drone) => { + todo!("Where can a drone move?"); + vec![] + } + // Any distance in any straight line + Some(Piece::Queen) => { + todo!("Where can a queen move?"); + vec![] + } + None => Vec::new(), + } + } } impl std::fmt::Display for Board { @@ -308,7 +335,7 @@ pub(crate) fn update_board( pub(crate) fn set_side(mut events: Query<(&mut Side, &BoardIndex), Changed>) { events .iter_mut() - .for_each(|(mut side, idx)| match Board::side(idx) { + .for_each(|(mut side, idx)| match Board::side(*idx) { Ok(s) => { debug!("Set side event {:?} {:?} -> {:?}", idx, side, s); if *side != s { @@ -372,7 +399,7 @@ fn move_piece( events.iter().for_each(|(tile, to)| { selected_pieces.iter().for_each(|from| { // Move piece - match board.move_piece(from, to) { + match board.move_piece(*from, *to) { Ok(moves) => { // De-select the piece info!("Applying moves {:?}", moves);