pub struct Rustoku {
pub board: Board,
pub masks: Masks,
pub candidates: Candidates,
pub techniques: TechniqueFlags,
}
Expand description
Solver primitive that uses backtracking and bitmasking for constraints.
This struct supports the ability to:
- Initialize from a 2D array, a flat byte array, or a string representation
- Solve a Sudoku puzzle using backtracking with Minimum Remaining Values (MRV)
- Generate a Sudoku puzzle with a unique solution based on the number of clues specified
- Check if a Sudoku puzzle is solved correctly
§Examples
Solve a Sudoku puzzle:
use rustoku_lib::Rustoku;
let puzzle = "530070000600195000098000060800060003400803001700020006060000280000419005000080079";
let mut rustoku = Rustoku::new_from_str(puzzle).unwrap();
assert!(rustoku.solve_any().is_some());
Generate a Sudoku puzzle:
use rustoku_lib::{Rustoku, generate_board};
let board = generate_board(30).unwrap();
let solution = Rustoku::new(board).unwrap().solve_all();
assert_eq!(solution.len(), 1);
Check if a Sudoku puzzle is solved:
use rustoku_lib::Rustoku;
let puzzle = "534678912672195348198342567859761423426853791713924856961537284287419635345286179";
let rustoku = Rustoku::new_from_str(puzzle).unwrap();
assert!(rustoku.is_solved());
Fields§
§board: Board
The current state of the Sudoku board.
masks: Masks
Bitmasks that check if a cell is safe in a row, column and box.
candidates: Candidates
Candidate cache from computing the bitmasks.
techniques: TechniqueFlags
Techniques used during the initial phase of solving.
Implementations§
Source§impl Rustoku
impl Rustoku
Sourcepub fn new(initial_board: Board) -> Result<Self, RustokuError>
pub fn new(initial_board: Board) -> Result<Self, RustokuError>
Constructs a new Rustoku
instance from an initial Board
.
Sourcepub fn new_from_str(s: &str) -> Result<Self, RustokuError>
pub fn new_from_str(s: &str) -> Result<Self, RustokuError>
Constructs a new Rustoku
instance from a string representation of the board.
Sourcepub fn with_techniques(self, techniques: TechniqueFlags) -> Self
pub fn with_techniques(self, techniques: TechniqueFlags) -> Self
Returns the existing Rustoku instance, with modified techniques.
Sourcepub fn solve_until(&mut self, bound: usize) -> Vec<Solution>
pub fn solve_until(&mut self, bound: usize) -> Vec<Solution>
Solves the Sudoku puzzle up to a certain bound, returning solutions with their solve paths.
Sourcepub fn solve_any(&mut self) -> Option<Solution>
pub fn solve_any(&mut self) -> Option<Solution>
Attempts to solve the Sudoku puzzle using backtracking with MRV (Minimum Remaining Values).