Skip to content

Commit

Permalink
refactor: refactor some code
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenMian committed Apr 2, 2024
1 parent 6bf7559 commit 9c12700
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ impl Level {
comments += &(trimmed_line.to_string() + "\n");
continue;
}
if trimmed_line.starts_with(';') {
comments += &(trimmed_line[1..].trim().to_string() + "\n");
if let Some(comment) = trimmed_line.strip_prefix(';') {
comments += &(comment.trim_start().to_string() + "\n");
continue;
}

Expand Down
12 changes: 6 additions & 6 deletions src/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ impl From<char> for Movement {
}
}

impl Into<char> for Movement {
fn into(self) -> char {
let c = match self.direction() {
impl From<Movement> for char {
fn from(movement: Movement) -> Self {
let char = match movement.direction() {
Direction::Up => 'u',
Direction::Down => 'd',
Direction::Left => 'l',
Direction::Right => 'r',
};
if self.is_push() {
c.to_ascii_uppercase()
if movement.is_push() {
char.to_ascii_uppercase()
} else {
c
char
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/solver/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,22 @@ impl State {
heuristic: 0,
lower_bound: OnceCell::new(),
};
debug_assert!(instance.movements.move_count() < 10000);
debug_assert!(instance.movements.push_count() < 10000);
debug_assert!(instance.lower_bound(solver) < 10000);
debug_assert!(instance.movements.move_count() < 10_000);
debug_assert!(instance.movements.push_count() < 10_000);
debug_assert!(instance.lower_bound(solver) < 10_000);
instance.heuristic = match solver.strategy() {
Strategy::Fast => {
instance.lower_bound(solver) * 10000 + instance.movements.move_count()
instance.lower_bound(solver) * 10_000 + instance.movements.move_count()
}
Strategy::Mixed => instance.lower_bound(solver) + instance.movements.move_count(),
Strategy::OptimalMovePush => {
instance.movements.move_count() * 10000_0000
+ instance.movements.push_count() * 10000
instance.movements.move_count() * 100_000_000
+ instance.movements.push_count() * 10_000
+ instance.lower_bound(solver)
}
Strategy::OptimalPushMove => {
instance.movements.push_count() * 10000_0000
+ instance.movements.move_count() * 10000
instance.movements.push_count() * 100_000_000
+ instance.movements.move_count() * 10_000
+ instance.lower_bound(solver)
}
};
Expand Down
2 changes: 2 additions & 0 deletions src/systems/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::too_many_arguments, clippy::type_complexity)]

pub mod audio;
pub mod auto_move;
pub mod auto_solve;
Expand Down
9 changes: 5 additions & 4 deletions src/systems/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::direction::Direction;
use crate::events::*;
use crate::resources::*;

use std::cmp::Ordering;
use std::collections::HashSet;
use std::time::Duration;

Expand Down Expand Up @@ -111,10 +112,10 @@ pub fn handle_player_movement(
.target_positions
.intersection(&board.level.crate_positions)
.count();
if new_occupied_targets_count > occupied_targets_count {
crate_enter_target_events.send_default();
} else if new_occupied_targets_count < occupied_targets_count {
crate_leave_target_events.send_default();
match new_occupied_targets_count.cmp(&occupied_targets_count) {
Ordering::Greater => drop(crate_enter_target_events.send_default()),
Ordering::Less => drop(crate_leave_target_events.send_default()),
_ => (),
}

player_grid_position.x += direction.to_vector().x;
Expand Down
6 changes: 3 additions & 3 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ mod tests {

#[test]
#[cfg(not(debug_assertions))]
fn solver_microban_2() {
fn solve_microban_2() {
let levels = Level::load_from_file(Path::new("assets/levels/microban_II_135.xsb")).unwrap();
assert!(
solve(
Expand All @@ -73,7 +73,7 @@ mod tests {

#[test]
#[cfg(not(debug_assertions))]
fn solver_microban() {
fn solve_microban() {
let levels = Level::load_from_file(Path::new("assets/levels/microban_155.xsb")).unwrap();
assert!(
solve(
Expand All @@ -87,7 +87,7 @@ mod tests {

#[test]
#[cfg(not(debug_assertions))]
fn solver_box_world() {
fn solve_box_world() {
let levels = Level::load_from_file(Path::new("assets/levels/box_world_100.xsb")).unwrap();
assert!(
solve(
Expand Down

0 comments on commit 9c12700

Please sign in to comment.