From d8db930bdaf96d61c2101648656873a804f944d2 Mon Sep 17 00:00:00 2001 From: maxblan Date: Fri, 10 Nov 2023 15:54:14 +0100 Subject: [PATCH] fix(socha): bugs and tests --- Cargo.toml | 4 +-- src/plugin/actions/accelerate.rs | 31 ++++++++++++++--------- src/plugin/actions/advance.rs | 2 +- src/plugin/game_state.rs | 42 ++++++++++++-------------------- 4 files changed, 38 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 60d86a4..4e1a146 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,9 +13,9 @@ libmath = "0.2.1" regex = "1.7.1" yaserde = "0.8.0" yaserde_derive = "0.8.0" -pyo3 = { version = "0.20.0"} +pyo3 = { version = "0.20.0", features = ["auto-initialize"] } pyo3-log = "0.9.0" log = "0.4.20" [features] -extension-module = ["pyo3/extension-module"] \ No newline at end of file +extension-module = ["pyo3/extension-module"] diff --git a/src/plugin/actions/accelerate.rs b/src/plugin/actions/accelerate.rs index 329a52e..fac638a 100644 --- a/src/plugin/actions/accelerate.rs +++ b/src/plugin/actions/accelerate.rs @@ -41,14 +41,18 @@ impl Accelerate { speed += self.acc; match () { - _ if self.acc == 0 => - Err(PyBaseException::new_err(AccelerationProblem::ZeroAcc.message())), - _ if speed > 6 => - Err(PyBaseException::new_err(AccelerationProblem::AboveMaxSpeed.message())), - _ if speed < 1 => - Err(PyBaseException::new_err(AccelerationProblem::BelowMinSpeed.message())), - _ if state.board.get(&ship.position).unwrap().field_type == FieldType::Sandbank => - Err(PyBaseException::new_err(AccelerationProblem::OnSandbank.message())), + _ if self.acc == 0 => { + return Err(PyBaseException::new_err(AccelerationProblem::ZeroAcc.message())); + } + _ if speed > 6 => { + return Err(PyBaseException::new_err(AccelerationProblem::AboveMaxSpeed.message())); + } + _ if speed < 1 => { + return Err(PyBaseException::new_err(AccelerationProblem::BelowMinSpeed.message())); + } + _ if state.board.get(&ship.position).unwrap().field_type == FieldType::Sandbank => { + return Err(PyBaseException::new_err(AccelerationProblem::OnSandbank.message())); + } _ => { let new_ship: Ship = self.accelerate(&mut ship); if new_ship.coal < 0 { @@ -60,7 +64,7 @@ impl Accelerate { } } - fn accelerate(&self, ship: &mut Ship) -> Ship { + fn accelerate(&self, ship: &mut Ship) -> Ship { let used_coal: i32 = self.acc.abs() - ship.free_acc; ship.coal -= used_coal.max(0); ship.free_acc = (-used_coal).max(0); @@ -109,7 +113,8 @@ mod tests { None, None, None, - None); + None + ); let team_two: Ship = Ship::new( CubeCoordinates::new(-1, 1), TeamEnum::Two, @@ -119,7 +124,8 @@ mod tests { None, None, None, - None); + None + ); let game_state: GameState = GameState::new(board, 0, team_one.clone(), team_two, None); (accelerate, game_state) } @@ -199,7 +205,8 @@ mod tests { None, None, None, - None); + None + ); assert_eq!(ship.speed, PluginConstants::MIN_SPEED); assert_eq!(ship.coal, PluginConstants::START_COAL); diff --git a/src/plugin/actions/advance.rs b/src/plugin/actions/advance.rs index 60a68fe..a427e59 100644 --- a/src/plugin/actions/advance.rs +++ b/src/plugin/actions/advance.rs @@ -162,7 +162,7 @@ mod tests { #[test] fn test_advance_perform_invalid_distance() { - let advance: Advance = Advance::new(4); + let advance: Advance = Advance::new(-2); let state: GameState = setup(); let result: Result = advance.perform(&state); diff --git a/src/plugin/game_state.rs b/src/plugin/game_state.rs index 22e212b..e5beb7e 100644 --- a/src/plugin/game_state.rs +++ b/src/plugin/game_state.rs @@ -381,9 +381,9 @@ impl GameState { .flat_map(|i| [i, -i]) .filter(|&i| ( if i > 0 { - PluginConstants::MAX_SPEED >= ship.speed + i + PluginConstants::MAX_SPEED <= ship.speed + i } else { - PluginConstants::MIN_SPEED <= ship.speed - i + PluginConstants::MIN_SPEED >= ship.speed - i } )) .map(Accelerate::new) @@ -595,7 +595,7 @@ mod tests { }]; let board: Board = Board::new(segment, CubeDirection::Right); let team_one: &mut Ship = &mut Ship::new( - CubeCoordinates::new(0, -1), + CubeCoordinates::new(0, 0), TeamEnum::One, None, None, @@ -630,9 +630,9 @@ mod tests { let advances: AdvanceInfo = game_state.check_ship_advance_limit(&team_one); - assert_eq!(advances.costs, vec![1, 2, 3, 4, 5]); - assert_eq!(advances.problem, AdvanceProblem::MovementPointsMissing); - assert_eq!(advances.distance(), 5); + assert_eq!(advances.costs, vec![2, 3, 4]); + assert_eq!(advances.problem, AdvanceProblem::FieldIsBlocked); + assert_eq!(advances.distance(), 3); } #[test] @@ -709,8 +709,8 @@ mod tests { ); let accelerations: Vec = game_state.possible_accelerations(); - assert_eq!(accelerations.len(), 5); - assert_eq!(accelerations[4].acc, -4); + assert_eq!(accelerations.len(), 7); + assert_eq!(accelerations[4].acc, 5); } #[test] @@ -865,8 +865,8 @@ mod tests { ); let advances: Vec = game_state.possible_advances(); - assert_eq!(advances.len(), 3); - assert_eq!(advances[2].distance, 2); + assert_eq!(advances.len(), 2); + assert_eq!(advances[1].distance, 2); } #[test] @@ -948,7 +948,7 @@ mod tests { } #[test] - fn test_performance_move() { + fn test_performe_move() { let segment: Vec = vec![ Segment { direction: CubeDirection::Right, @@ -1058,12 +1058,7 @@ mod tests { ); let move_: Move = Move::new( - vec![ - Action::Accelerate(Accelerate::new(1)), - Action::Advance(Advance::new(1)), - Action::Turn(Turn::new(CubeDirection::UpRight)), - Action::Advance(Advance::new(1)) - ] + vec![Action::Accelerate(Accelerate::new(1)), Action::Advance(Advance::new(2))] ); assert_eq!(game_state.current_ship.team, TeamEnum::One); @@ -1071,23 +1066,18 @@ mod tests { let new_state: GameState = game_state.perform_move(move_).unwrap(); assert_eq!(new_state.other_ship.team, TeamEnum::One); - assert_eq!(new_state.other_ship.position, CubeCoordinates::new(1, -2)); + assert_eq!(new_state.other_ship.position, CubeCoordinates::new(1, -1)); assert_eq!(new_state.current_ship.team, TeamEnum::Two); assert_eq!(new_state.current_ship.position, CubeCoordinates::new(-2, 1)); let second_move_: Move = Move::new( - vec![ - Action::Accelerate(Accelerate::new(1)), - Action::Advance(Advance::new(1)), - Action::Turn(Turn::new(CubeDirection::DownRight)), - Action::Advance(Advance::new(1)) - ] + vec![Action::Accelerate(Accelerate::new(1)), Action::Advance(Advance::new(2))] ); let second_new_state: GameState = new_state.perform_move(second_move_).unwrap(); - assert_eq!(second_new_state.other_ship.team, TeamEnum::Two); - assert_eq!(second_new_state.other_ship.position, CubeCoordinates::new(-1, 2)); + assert_eq!(second_new_state.other_ship.team, TeamEnum::One); + assert_eq!(second_new_state.other_ship.position, CubeCoordinates::new(1, -1)); } #[test]