pub struct Sudoku<C: Constraint + Clone> { /* private fields */ }
Expand description
A Sudoku represents a grid of numbers with an associated constraint. The numbers may or may not fulfill the constraint, but there is a method to check it.
There is no guarantee that the Sudoku is uniquely solveable or even solveable at all, however there are ways to check that (see the solver module).
Implementations§
Source§impl<C: Constraint + Clone> Sudoku<C>
impl<C: Constraint + Clone> Sudoku<C>
Sourcepub fn new_empty(
block_width: usize,
block_height: usize,
constraint: C,
) -> SudokuResult<Sudoku<C>>
pub fn new_empty( block_width: usize, block_height: usize, constraint: C, ) -> SudokuResult<Sudoku<C>>
Creates a new Sudoku with the provided constraint and an empty grid
of the given dimensions. The total width and height of the grid will be
equal to the product of block_width
and block_height
.
§Arguments
block_width
: The horizontal dimension of one sub-block of the grid. To ensure a square grid, this is also the number of blocks that compose the grid vertically. For an ordinary Sudoku grid, this is 3. Must be greater than 0.block_height
: The vertical dimension of one sub-block of the grid. To ensure a square grid, this is also the number of blocks that compose the grid horizontally. For an ordinary Sudoku grid, this is 3. Must be greater than 0.constraint
: The constraint which is checked by this Sudoku. Grid configurations which violate this constraint will be seen as invalid by Sudoku::is_valid().
§Errors
If block_width
or block_height
is invalid (zero).
Sourcepub fn new_with_grid(grid: SudokuGrid, constraint: C) -> Sudoku<C>
pub fn new_with_grid(grid: SudokuGrid, constraint: C) -> Sudoku<C>
Creats a new Sudoku with the provided constraint and a given grid, which may already contain some numbers. Note that it is not checked whether the given grid fulfills the constraint - it is perfectly legal to create an invalid Sudoku here.
§Arguments
grid
: The initial SudokuGrid which contains the numbers with which the Sudoku is filled.constraint
: The constraint which is checked by this Sudoku. Grid configurations which violate this constraint will be seen as invalid by Sudoku::is_valid).
Sourcepub fn parse(code: &str, constraint: C) -> SudokuParseResult<Sudoku<C>>
pub fn parse(code: &str, constraint: C) -> SudokuParseResult<Sudoku<C>>
Parses the code into a SudokuGrid using SudokuGrid::parse and wraps the result in a Sudoku with the given constraint. Note that it is not required that the code matches the constraint. It is perfectly legal to parse an invalid Sudoku.
§Arguments
code
: The code that specifies the grid. See SudokuGrid::parse for a language specification.constraint
: The constraint which is checked by this Sudoku. Grid configurations which violate this constraint will be seen as invalid by Sudoku::is_valid.
§Errors
If the parsing fails. See SudokuGrid::parse for further information.
Sourcepub fn grid(&self) -> &SudokuGrid
pub fn grid(&self) -> &SudokuGrid
Gets a reference to the SudokuGrid
of this Sudoku.
Sourcepub fn grid_mut(&mut self) -> &mut SudokuGrid
pub fn grid_mut(&mut self) -> &mut SudokuGrid
Gets a mutable reference to the SudokuGrid
of this Sudoku.
Sourcepub fn constraint(&self) -> &C
pub fn constraint(&self) -> &C
Gets a reference to the Constraint
of this Sudoku.
Sourcepub fn is_valid_cell(&self, column: usize, row: usize) -> SudokuResult<bool>
pub fn is_valid_cell(&self, column: usize, row: usize) -> SudokuResult<bool>
Indicates whether the cell at the given location matches the
constraint. That is, if the specified cell violates the constraint,
false
is returned, and true
otherwise.
§Arguments
column
: The column (x-coordinate) of the checked cell. Must be in the range[0, size[
.row
: The row (y-coordinate) of the checked cell. Must be in the range[0, size[
.
Sourcepub fn is_valid_number(
&self,
column: usize,
row: usize,
number: usize,
) -> SudokuResult<bool>
pub fn is_valid_number( &self, column: usize, row: usize, number: usize, ) -> SudokuResult<bool>
Indicates whether the given number would be valid in the cell at the
given location. That is, if the number violated the constraint, false
is returned, and true
otherwise.
§Arguments
column
: The column (x-coordinate) of the checked cell. Must be in the range[0, size[
.row
: The row (y-coordinate) of the checked cell. Must be in the range[0, size[
.number
: The number to check whether it is valid in the given cell.
§Errors
SudokuError::OutOfBounds
If eithercolumn
orrow
are not in the specified range.SudokuError::InvalidNumber
Ifnumber
is not in the specified range.
Sourcepub fn is_valid_solution(&self, solution: &SudokuGrid) -> SudokuResult<bool>
pub fn is_valid_solution(&self, solution: &SudokuGrid) -> SudokuResult<bool>
Indicates whether the given SudokuGrid is a valid solution to this
puzzle. That is the case if all digits from this Sudoku can be found in
the solution
, it matches the constraint of this Sudoku, and it is
full.
§Errors
If the dimensions of this Sudoku’s grid and the solution
grid are not
the same. In that case, SudokuError::InvalidDimensions
is returned.