Skip to content

Commit

Permalink
fix(socha): bugs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxblan committed Nov 10, 2023
1 parent d2b0efc commit d8db930
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
extension-module = ["pyo3/extension-module"]
31 changes: 19 additions & 12 deletions src/plugin/actions/accelerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -109,7 +113,8 @@ mod tests {
None,
None,
None,
None);
None
);
let team_two: Ship = Ship::new(
CubeCoordinates::new(-1, 1),
TeamEnum::Two,
Expand All @@ -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)
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/actions/advance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ship, PyErr> = advance.perform(&state);
Expand Down
42 changes: 16 additions & 26 deletions src/plugin/game_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -709,8 +709,8 @@ mod tests {
);

let accelerations: Vec<Accelerate> = 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]
Expand Down Expand Up @@ -865,8 +865,8 @@ mod tests {
);

let advances: Vec<Advance> = 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]
Expand Down Expand Up @@ -948,7 +948,7 @@ mod tests {
}

#[test]
fn test_performance_move() {
fn test_performe_move() {
let segment: Vec<Segment> = vec![
Segment {
direction: CubeDirection::Right,
Expand Down Expand Up @@ -1058,36 +1058,26 @@ 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);
assert_eq!(game_state.current_ship.position, CubeCoordinates::new(-1, -1));

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]
Expand Down

0 comments on commit d8db930

Please sign in to comment.