Skip to content

Commit

Permalink
Refactor: Move solver logic to separate module
Browse files Browse the repository at this point in the history
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.*
  • Loading branch information
ErikKarlgren committed Aug 22, 2024
1 parent 55f91e5 commit 15581d7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/coordinates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 3 additions & 39 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<Board>, 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());
}
}
41 changes: 41 additions & 0 deletions src/solver.rs
Original file line number Diff line number Diff line change
@@ -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<Board>, 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());
}
}

0 comments on commit 15581d7

Please sign in to comment.