Skip to content

Commit

Permalink
fix(push): movement points missing error
Browse files Browse the repository at this point in the history
  • Loading branch information
maxblan committed Dec 7, 2023
1 parent 366cb54 commit 0daeab5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
2 changes: 1 addition & 1 deletion python/socha/_socha.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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): ...


Expand Down
55 changes: 28 additions & 27 deletions src/plugin/actions/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,15 +23,16 @@ impl Push {
Push { direction }
}

pub fn perform(&self, state: &GameState) -> Result<Ship, PyErr> {
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();
Expand Down Expand Up @@ -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<String> {
Expand All @@ -93,21 +94,21 @@ 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::*;

fn setup(
c1: CubeCoordinates,
c2: CubeCoordinates,
fields: Vec<Vec<Field>>,
dir: CubeDirection,
dir: CubeDirection
) -> (GameState, Push) {
let segment: Vec<Segment> = vec![Segment {
direction: CubeDirection::Right,
Expand All @@ -124,7 +125,7 @@ mod tests {
None,
None,
None,
None,
None
);
team_one.speed = 5;
team_one.movement = 5;
Expand All @@ -137,7 +138,7 @@ mod tests {
None,
None,
None,
None,
None
);
team_two.speed = 5;
team_two.movement = 5;
Expand All @@ -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<Ship, PyErr> = 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]
Expand All @@ -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<Ship, PyErr> = push.perform(&state);
let result: Result<(Ship, Ship), PyErr> = push.perform(&state);

assert!(result.is_err());

Expand All @@ -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<Ship, PyErr> = push.perform(&state);
let result: Result<(Ship, Ship), PyErr> = push.perform(&state);

assert!(result.is_err());

Expand All @@ -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<Ship, PyErr> = push.perform(&state);
let result: Result<(Ship, Ship), PyErr> = push.perform(&state);

assert!(result.is_err());

Expand Down
6 changes: 4 additions & 2 deletions src/plugin/game_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl GameState {
_ => {}
}

let mut new_current_ship: Result<Ship, PyErr> = Ok(new_state.current_ship);
let new_current_ship: Result<Ship, PyErr>;
let mut new_other_ship: Result<Ship, PyErr> = Ok(new_state.other_ship);

match action {
Expand All @@ -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);
Expand Down

0 comments on commit 0daeab5

Please sign in to comment.