Skip to content

Commit

Permalink
Refactor action perform_unchecked methods
Browse files Browse the repository at this point in the history
  • Loading branch information
maxblan committed Jan 28, 2024
1 parent 26af619 commit 51ba81d
Show file tree
Hide file tree
Showing 7 changed files with 0 additions and 135 deletions.
13 changes: 0 additions & 13 deletions python/socha/api/networking/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,6 @@ def handle_move(move_response):


def _merge_advances(actions):
"""
Merges consecutive Advance actions into a single action by adding their distances.
This is a workaround for handling multiple Advance actions in a sequence.
Args:
actions (list): A list of actions.
Returns:
list: A new list of actions where consecutive Advance actions have been merged.
Note:
This function modifies the input list 'actions' in-place.
"""
new_actions = []
for i in range(len(actions) - 1):
if isinstance(actions[i], _socha.Advance) and isinstance(actions[i + 1], _socha.Advance):
Expand Down
13 changes: 0 additions & 13 deletions src/plugin/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,4 @@ impl Action {
Action::Turn(turn) => turn.perform(game_state).map(|ship| (Some(ship), None)),
}
}

pub fn perform_unchecked(&self, game_state: &mut GameState) -> (Option<Ship>, Option<Ship>) {
match self {
Action::Accelerate(accelerate) =>
(Some(accelerate.perform_unchecked(game_state)), None),
Action::Advance(advance) => (Some(advance.perform_unchecked(game_state)), None),
Action::Push(push) => {
let (ship1, ship2) = push.perform_unchecked(game_state);
(Some(ship1), Some(ship2))
}
Action::Turn(turn) => (Some(turn.perform_unchecked(game_state)), None),
}
}
}
10 changes: 0 additions & 10 deletions src/plugin/actions/accelerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,6 @@ impl Accelerate {
return ship.clone();
}

pub fn perform_unchecked(&self, state: &GameState) -> Ship {
debug!("perform_unchecked() called with acc: {} and game state: {:?}", self.acc, state);
let mut ship: Ship = state.current_ship.clone();

let new_ship = self.accelerate_unchecked(&mut ship);

debug!("Ship accelerated successfully");
new_ship
}

fn accelerate_unchecked(&self, ship: &mut Ship) -> Ship {
debug!("accelerate_unchecked() called with ship: {:?}", ship);
let used_coal: i32 = self.acc.abs() - ship.free_acc;
Expand Down
15 changes: 0 additions & 15 deletions src/plugin/actions/advance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,6 @@ impl Advance {
Ok(true)
}

pub fn perform_unchecked(&self, state: &GameState) -> Ship {
debug!("Performing unchecked advance with distance: {}", self.distance);
let mut ship = state.current_ship.clone();

let advance_info = state.calculate_advance_info(
&ship.position,
&ship.direction,
ship.movement
);

ship.update_position(self.distance, advance_info);
debug!("Unchecked advance completed: {:?}", ship);
ship
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!("Advance({})", self.distance))
}
Expand Down
24 changes: 0 additions & 24 deletions src/plugin/actions/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,6 @@ impl Push {
Ok((current_ship, other_ship))
}

pub fn perform_unchecked(&self, state: &GameState) -> (Ship, Ship) {
debug!("Performing unchecked push with direction: {}", self.direction);
let mut current_ship: Ship = state.current_ship.clone();
let mut other_ship: Ship = state.other_ship.clone();

current_ship.movement -= 1;

let push_to: CubeCoordinates = current_ship.position + self.direction.vector();

other_ship.position = push_to;

if let Some(field) = state.board.get(&push_to) {
if field.field_type == FieldType::Sandbank {
other_ship.speed = 1;
other_ship.movement = 1;
}
}

other_ship.free_turns += 1;

debug!("Unchecked push completed and other ship status: {:?}", other_ship);
(current_ship, other_ship)
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!("Push({})", self.direction))
}
Expand Down
18 changes: 0 additions & 18 deletions src/plugin/actions/turn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,6 @@ impl Turn {
self.direction.turn_count_to(self.direction.clone()).abs().saturating_sub(ship.free_turns)
}

pub fn perform_unchecked(&self, state: &GameState) -> Ship {
debug!("Performing unchecked turn with direction: {}", self.direction);
let mut current_ship: Ship = state.current_ship.clone();

let turn_count: i32 = current_ship.direction.turn_count_to(self.direction);
let used_coal: i32 = turn_count.abs() - current_ship.free_turns;

current_ship.free_turns = std::cmp::max(current_ship.free_turns - turn_count.abs(), 0);
if used_coal > 0 {
current_ship.coal -= used_coal;
}

current_ship.direction = self.direction;

debug!("Unchecked turn completed and ship status: {:?}", current_ship);
current_ship
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!("Turn({})", self.direction))
}
Expand Down
42 changes: 0 additions & 42 deletions src/plugin/game_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,48 +272,6 @@ impl GameState {
Ok(new_state)
}

pub fn perform_move_unchecked(&self, move_: Move) -> GameState {
let mut new_state = self.clone();
debug!("Current ship before move: {:?}", new_state.current_ship);
debug!("Other ship before move: {:?}", new_state.other_ship);

for action in &move_.actions {
match action.perform_unchecked(&mut new_state) {
(Some(current_ship), Some(other_ship)) => {
new_state.current_ship = current_ship;
new_state.other_ship = other_ship;
}
(Some(current_ship), None) => {
new_state.current_ship = current_ship;
}
(None, Some(_)) => {}
(None, None) => {}
}
}

new_state.pick_up_passenger_current_ship();
new_state.current_ship.points = new_state
.ship_points(new_state.current_ship)
.expect("Could not calculate ship points");

if move_.actions.iter().any(|a| matches!(a, Action::Push(_))) {
new_state.other_ship.points = new_state
.ship_points(new_state.other_ship)
.expect("Could not calculate other ship's points");
if new_state.other_ship.speed == 1 {
new_state.pick_up_passenger_other_ship();
}
}

new_state.last_move = Some(move_);
new_state.advance_turn();

debug!("Current ship after move: {:?}", new_state.current_ship);
debug!("Other ship after move: {:?}", new_state.other_ship);

new_state
}

pub fn advance_turn(&mut self) {
let current_ship: &mut Ship = &mut self.current_ship;

Expand Down

0 comments on commit 51ba81d

Please sign in to comment.