From 15581d7fd7f5bf359d62bab00267569802e26bd9 Mon Sep 17 00:00:00 2001 From: Erik Karlgren Domercq Date: Thu, 22 Aug 2024 20:47:36 +0200 Subject: [PATCH] Refactor: Move solver logic to separate module This commit refactors the Klotski solver logic by moving it from `main.rs` to a new `solver.rs` module. The changes include: 1. Create a new `solver.rs` file containing the `solve_klotski` function and its associated test. 2. Update `main.rs` to use the new `solver` module and remove the moved code. 3. Adjust imports in `main.rs` to reflect the new structure. 4. Fix a documentation comment in `coordinates.rs` by changing the code block type to "test". These changes improve code organization and modularity, making the project structure cleaner and more maintainable. *Commit message written with Claude 3.5 Sonnet.* --- src/coordinates.rs | 2 +- src/main.rs | 42 +++--------------------------------------- src/solver.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 40 deletions(-) create mode 100644 src/solver.rs diff --git a/src/coordinates.rs b/src/coordinates.rs index 7a1b655..76bbba6 100644 --- a/src/coordinates.rs +++ b/src/coordinates.rs @@ -22,7 +22,7 @@ impl Coor { /// /// This is how the coordinate system looks like: /// - /// ``` + /// ```test /// y=0 y=1 y=2 y=3 y=4 ... /// x=0 /// x=1 diff --git a/src/main.rs b/src/main.rs index 847cca6..7c03620 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,10 +3,10 @@ mod coordinates; mod direction; mod errors; mod piece; +mod solver; -use board::{Board, SOLUTION}; -use coordinates::Coor; -use pathfinding::directed::astar; +use board::Board; +use solver::solve_klotski; fn main() { let state = Board::new(); @@ -22,39 +22,3 @@ fn main() { println!("No solution?"); } } - -/// Solves a given Klotski board state using an A* algorithm -fn solve_klotski(state: Board) -> Option<(Vec, i32)> { - astar::astar( - &state, - |p| p.next_states(), - |s| { - // Manhattan distance - let Coor { - row: target_row, - col: target_col, - } = s.target_piece().coor; - - let Coor { - row: sol_row, - col: sol_col, - } = SOLUTION; - - (target_row as i32 - sol_row as i32).abs() + (target_col as i32 - sol_col as i32).abs() - }, - |s| s.is_solution(), - ) -} - -#[cfg(test)] -mod tests { - use crate::{board::Board, solve_klotski}; - - #[test] - fn can_solve_default_klotski() { - // This test may take a few seconds to complete - let board = Board::new(); - let finished = solve_klotski(board); - assert!(finished.is_some()); - } -} diff --git a/src/solver.rs b/src/solver.rs new file mode 100644 index 0000000..e77f062 --- /dev/null +++ b/src/solver.rs @@ -0,0 +1,41 @@ +use crate::{ + board::{Board, SOLUTION}, + coordinates::Coor, +}; +use pathfinding::directed::astar; + +/// Solves a given Klotski board state using an A* algorithm +pub fn solve_klotski(state: Board) -> Option<(Vec, i32)> { + astar::astar( + &state, + |p| p.next_states(), + |s| { + // Manhattan distance + let Coor { + row: target_row, + col: target_col, + } = s.target_piece().coor; + + let Coor { + row: sol_row, + col: sol_col, + } = SOLUTION; + + (target_row as i32 - sol_row as i32).abs() + (target_col as i32 - sol_col as i32).abs() + }, + |s| s.is_solution(), + ) +} + +#[cfg(test)] +mod tests { + use crate::{board::Board, solve_klotski}; + + #[test] + fn can_solve_default_klotski() { + // This test may take a few seconds to complete + let board = Board::new(); + let finished = solve_klotski(board); + assert!(finished.is_some()); + } +}