pub struct Solver { /* private fields */ }Expand description
A Sudoku solver using logical strategies and optional backtracking.
Implementations§
Source§impl Solver
impl Solver
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new solver with default strategies.
Examples found in repository?
examples/generate.rs (line 4)
3fn main() {
4 let mut solver = Solver::new();
5
6 for difficulty in [
7 Difficulty::Easy,
8 Difficulty::Medium,
9 Difficulty::Hard,
10 Difficulty::Expert,
11 ] {
12 let puzzle = solver.generate(9, difficulty).unwrap();
13 println!("{difficulty:?}:\n{puzzle}");
14 }
15}More examples
examples/solve.rs (line 12)
3fn main() {
4 let puzzle = Sudoku::from_string(
5 "530070000600195000098000060800060003400803001700020006060000280000419005000080079",
6 9,
7 )
8 .unwrap();
9
10 println!("Puzzle:\n{puzzle}");
11
12 let mut solver = Solver::new();
13 let (solution, stats) = solver.solve_with_stats(puzzle).unwrap();
14
15 println!("Solution:\n{solution}");
16 println!(
17 "Stats: {} iterations, {} backtracks",
18 stats.iterations, stats.backtracks
19 );
20}examples/basic.rs (line 29)
3fn main() {
4 // Create empty 9x9 sudoku
5 let mut sudoku = Sudoku::new(9);
6
7 // Set some values
8 sudoku.set(0, 0, 5).unwrap();
9 sudoku.set(0, 1, 3).unwrap();
10 sudoku.set(0, 4, 7).unwrap();
11
12 // Check validity
13 println!("Valid: {}", sudoku.is_valid());
14 println!("Complete: {}", sudoku.is_complete());
15
16 // Get candidates for a cell
17 let candidates = sudoku.candidates(0, 2);
18 println!("Candidates at (0,2): {:?}", candidates);
19
20 // Check cell state
21 match sudoku.get(0, 0) {
22 Some(Cell::Filled(v)) => println!("Cell (0,0) = {v}"),
23 Some(Cell::Given(v)) => println!("Cell (0,0) = {v} (given)"),
24 Some(Cell::Empty) => println!("Cell (0,0) is empty"),
25 None => println!("Out of bounds"),
26 }
27
28 // Get hint
29 let solver = Solver::new();
30 if let Some((r, c, val)) = solver.hint(&sudoku) {
31 println!("Hint: Place {val} at ({r}, {c})");
32 }
33}Sourcepub fn with_strategies(strategies: Vec<Box<dyn Strategy>>) -> Self
pub fn with_strategies(strategies: Vec<Box<dyn Strategy>>) -> Self
Creates a solver with custom strategies.
Sourcepub fn max_iterations(self, n: usize) -> Self
pub fn max_iterations(self, n: usize) -> Self
Sets maximum iterations for logical solving.
Sourcepub fn use_backtracking(self, enabled: bool) -> Self
pub fn use_backtracking(self, enabled: bool) -> Self
Enables or disables backtracking.
Sourcepub fn solve(&mut self, sudoku: Sudoku) -> Result<Sudoku, String>
pub fn solve(&mut self, sudoku: Sudoku) -> Result<Sudoku, String>
Solves the puzzle, returning the solution.
Sourcepub fn solve_with_stats(
&mut self,
sudoku: Sudoku,
) -> Result<(Sudoku, Stats), String>
pub fn solve_with_stats( &mut self, sudoku: Sudoku, ) -> Result<(Sudoku, Stats), String>
Solves the puzzle, returning solution and statistics.
Examples found in repository?
examples/solve.rs (line 13)
3fn main() {
4 let puzzle = Sudoku::from_string(
5 "530070000600195000098000060800060003400803001700020006060000280000419005000080079",
6 9,
7 )
8 .unwrap();
9
10 println!("Puzzle:\n{puzzle}");
11
12 let mut solver = Solver::new();
13 let (solution, stats) = solver.solve_with_stats(puzzle).unwrap();
14
15 println!("Solution:\n{solution}");
16 println!(
17 "Stats: {} iterations, {} backtracks",
18 stats.iterations, stats.backtracks
19 );
20}Sourcepub fn hint(&self, sudoku: &Sudoku) -> Option<(usize, usize, u8)>
pub fn hint(&self, sudoku: &Sudoku) -> Option<(usize, usize, u8)>
Returns a hint: (row, col, value) for the next logical move.
Examples found in repository?
examples/basic.rs (line 30)
3fn main() {
4 // Create empty 9x9 sudoku
5 let mut sudoku = Sudoku::new(9);
6
7 // Set some values
8 sudoku.set(0, 0, 5).unwrap();
9 sudoku.set(0, 1, 3).unwrap();
10 sudoku.set(0, 4, 7).unwrap();
11
12 // Check validity
13 println!("Valid: {}", sudoku.is_valid());
14 println!("Complete: {}", sudoku.is_complete());
15
16 // Get candidates for a cell
17 let candidates = sudoku.candidates(0, 2);
18 println!("Candidates at (0,2): {:?}", candidates);
19
20 // Check cell state
21 match sudoku.get(0, 0) {
22 Some(Cell::Filled(v)) => println!("Cell (0,0) = {v}"),
23 Some(Cell::Given(v)) => println!("Cell (0,0) = {v} (given)"),
24 Some(Cell::Empty) => println!("Cell (0,0) is empty"),
25 None => println!("Out of bounds"),
26 }
27
28 // Get hint
29 let solver = Solver::new();
30 if let Some((r, c, val)) = solver.hint(&sudoku) {
31 println!("Hint: Place {val} at ({r}, {c})");
32 }
33}Sourcepub fn count_solutions(&self, sudoku: Sudoku, max: usize) -> usize
pub fn count_solutions(&self, sudoku: Sudoku, max: usize) -> usize
Counts solutions up to a maximum.
Sourcepub fn generate(
&mut self,
size: usize,
difficulty: Difficulty,
) -> Result<Sudoku, String>
pub fn generate( &mut self, size: usize, difficulty: Difficulty, ) -> Result<Sudoku, String>
Generates a puzzle of the given size and difficulty.
Examples found in repository?
examples/generate.rs (line 12)
3fn main() {
4 let mut solver = Solver::new();
5
6 for difficulty in [
7 Difficulty::Easy,
8 Difficulty::Medium,
9 Difficulty::Hard,
10 Difficulty::Expert,
11 ] {
12 let puzzle = solver.generate(9, difficulty).unwrap();
13 println!("{difficulty:?}:\n{puzzle}");
14 }
15}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Solver
impl !RefUnwindSafe for Solver
impl Send for Solver
impl Sync for Solver
impl Unpin for Solver
impl !UnwindSafe for Solver
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more