Sudoku

Struct Sudoku 

Source
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>

Source

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).

Source

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).
Source

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.

Source

pub fn grid(&self) -> &SudokuGrid

Gets a reference to the SudokuGrid of this Sudoku.

Source

pub fn grid_mut(&mut self) -> &mut SudokuGrid

Gets a mutable reference to the SudokuGrid of this Sudoku.

Source

pub fn constraint(&self) -> &C

Gets a reference to the Constraint of this Sudoku.

Source

pub fn is_valid(&self) -> bool

Indicates whether the entire grid matches the constraint.

Source

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[.
Source

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 either column or row are not in the specified range.
  • SudokuError::InvalidNumber If number is not in the specified range.
Source

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.

Trait Implementations§

Source§

impl<C: Clone + Constraint + Clone> Clone for Sudoku<C>

Source§

fn clone(&self) -> Sudoku<C>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<C> Freeze for Sudoku<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for Sudoku<C>
where C: RefUnwindSafe,

§

impl<C> Send for Sudoku<C>
where C: Send,

§

impl<C> Sync for Sudoku<C>
where C: Sync,

§

impl<C> Unpin for Sudoku<C>
where C: Unpin,

§

impl<C> UnwindSafe for Sudoku<C>
where C: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V