From 0daeab51418effcc711fd5cc61b9cd222953c8ca Mon Sep 17 00:00:00 2001 From: maxblan Date: Thu, 7 Dec 2023 14:55:34 +0100 Subject: [PATCH] fix(push): movement points missing error --- python/socha/_socha.pyi | 2 +- src/plugin/actions/push.rs | 55 +++++++++++++++++++------------------- src/plugin/game_state.rs | 6 +++-- 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/python/socha/_socha.pyi b/python/socha/_socha.pyi index 11dbb63..72e3419 100644 --- a/python/socha/_socha.pyi +++ b/python/socha/_socha.pyi @@ -161,7 +161,7 @@ class Push: direction: CubeDirection def __init__(self, direction: CubeDirection) -> None: ... - def perform(self, state: GameState) -> Ship | BaseException( + def perform(self, state: GameState) -> (Ship, Ship) | BaseException( PushProblem): ... diff --git a/src/plugin/actions/push.rs b/src/plugin/actions/push.rs index d46c8ad..faba639 100644 --- a/src/plugin/actions/push.rs +++ b/src/plugin/actions/push.rs @@ -2,7 +2,7 @@ use log::debug; use pyo3::exceptions::PyBaseException; use pyo3::prelude::*; -use crate::plugin::coordinate::{CubeCoordinates, CubeDirection}; +use crate::plugin::coordinate::{ CubeCoordinates, CubeDirection }; use crate::plugin::errors::push_error::PushProblem; use crate::plugin::field::FieldType; use crate::plugin::game_state::GameState; @@ -23,15 +23,16 @@ impl Push { Push { direction } } - pub fn perform(&self, state: &GameState) -> Result { + pub fn perform(&self, state: &GameState) -> Result<(Ship, Ship), PyErr> { debug!("Performing push with direction: {}", self.direction); - let current_ship: Ship = state.current_ship.clone(); + let mut current_ship: Ship = state.current_ship.clone(); let mut other_ship: Ship = state.other_ship.clone(); if current_ship.movement == 0 { debug!("Movement points missing: {}", current_ship.movement); return Err(PyBaseException::new_err(PushProblem::MovementPointsMissing.message())); } + current_ship.movement -= 1; let push_from: CubeCoordinates = current_ship.position; let push_to: CubeCoordinates = push_from + self.direction.vector(); @@ -82,7 +83,7 @@ impl Push { other_ship.free_turns += 1; debug!("Push completed and other ship status: {:?}", other_ship); - Ok(other_ship) + Ok((current_ship, other_ship)) } fn __repr__(&self) -> PyResult { @@ -93,13 +94,13 @@ impl Push { #[cfg(test)] mod tests { use pyo3::prepare_freethreaded_python; - + use crate::plugin::board::Board; - use crate::plugin::coordinate::{CubeCoordinates, CubeDirection}; + use crate::plugin::coordinate::{ CubeCoordinates, CubeDirection }; use crate::plugin::field::Field; use crate::plugin::game_state::GameState; use crate::plugin::segment::Segment; - use crate::plugin::ship::{Ship, TeamEnum}; + use crate::plugin::ship::{ Ship, TeamEnum }; use super::*; @@ -107,7 +108,7 @@ mod tests { c1: CubeCoordinates, c2: CubeCoordinates, fields: Vec>, - dir: CubeDirection, + dir: CubeDirection ) -> (GameState, Push) { let segment: Vec = vec![Segment { direction: CubeDirection::Right, @@ -124,7 +125,7 @@ mod tests { None, None, None, - None, + None ); team_one.speed = 5; team_one.movement = 5; @@ -137,7 +138,7 @@ mod tests { None, None, None, - None, + None ); team_two.speed = 5; team_two.movement = 5; @@ -152,16 +153,16 @@ mod tests { CubeCoordinates::new(0, 0), CubeCoordinates::new(0, 0), vec![vec![Field::new(FieldType::Water, None); 4]; 5], - CubeDirection::Right, + CubeDirection::Right ); - let result: Result = push.perform(&state); + let result: Result<(Ship, Ship), PyErr> = push.perform(&state); assert!(result.is_ok()); - let new_ship: Ship = result.unwrap(); + let new_ships: (Ship, Ship) = result.unwrap(); - assert_eq!(new_ship.position, CubeCoordinates::new(1, 0)); - assert_eq!(new_ship.free_turns, 2); + assert_eq!(new_ships.1.position, CubeCoordinates::new(1, 0)); + assert_eq!(new_ships.1.free_turns, 2); } #[test] @@ -171,41 +172,41 @@ mod tests { Field::new(FieldType::Water, None), Field::new(FieldType::Water, None), Field::new(FieldType::Water, None), - Field::new(FieldType::Water, None), + Field::new(FieldType::Water, None) ], vec![ Field::new(FieldType::Water, None), Field::new(FieldType::Water, None), Field::new(FieldType::Water, None), - Field::new(FieldType::Water, None), + Field::new(FieldType::Water, None) ], vec![ Field::new(FieldType::Water, None), Field::new(FieldType::Water, None), Field::new(FieldType::Island, None), - Field::new(FieldType::Water, None), + Field::new(FieldType::Water, None) ], vec![ Field::new(FieldType::Water, None), Field::new(FieldType::Water, None), Field::new(FieldType::Water, None), - Field::new(FieldType::Water, None), + Field::new(FieldType::Water, None) ], vec![ Field::new(FieldType::Water, None), Field::new(FieldType::Water, None), Field::new(FieldType::Water, None), - Field::new(FieldType::Water, None), - ], + Field::new(FieldType::Water, None) + ] ]; let (state, push) = setup( CubeCoordinates::new(0, 0), CubeCoordinates::new(0, 0), fields, - CubeDirection::Right, + CubeDirection::Right ); - let result: Result = push.perform(&state); + let result: Result<(Ship, Ship), PyErr> = push.perform(&state); assert!(result.is_err()); @@ -224,9 +225,9 @@ mod tests { CubeCoordinates::new(0, 0), CubeCoordinates::new(1, 0), vec![vec![Field::new(FieldType::Water, None); 4]; 5], - CubeDirection::Right, + CubeDirection::Right ); - let result: Result = push.perform(&state); + let result: Result<(Ship, Ship), PyErr> = push.perform(&state); assert!(result.is_err()); @@ -245,9 +246,9 @@ mod tests { CubeCoordinates::new(0, 0), CubeCoordinates::new(0, 0), vec![vec![Field::new(FieldType::Water, None); 4]; 5], - CubeDirection::Left, + CubeDirection::Left ); - let result: Result = push.perform(&state); + let result: Result<(Ship, Ship), PyErr> = push.perform(&state); assert!(result.is_err()); diff --git a/src/plugin/game_state.rs b/src/plugin/game_state.rs index c379029..86f036d 100644 --- a/src/plugin/game_state.rs +++ b/src/plugin/game_state.rs @@ -184,7 +184,7 @@ impl GameState { _ => {} } - let mut new_current_ship: Result = Ok(new_state.current_ship); + let new_current_ship: Result; let mut new_other_ship: Result = Ok(new_state.other_ship); match action { @@ -195,7 +195,9 @@ impl GameState { new_current_ship = advance.perform(&new_state); } Action::Push(push) => { - new_other_ship = push.perform(&new_state); + let ship_tuple: (Ship, Ship) = push.perform(&new_state)?; + new_current_ship = Ok(ship_tuple.0); + new_other_ship = Ok(ship_tuple.1); } Action::Turn(turn) => { new_current_ship = turn.perform(&new_state);