Constraint

Trait Constraint 

Source
pub trait Constraint {
    // Required methods
    fn check_number(
        &self,
        grid: &SudokuGrid,
        column: usize,
        row: usize,
        number: usize,
    ) -> bool;
    fn get_groups(&self, grid: &SudokuGrid) -> Vec<Group> ;

    // Provided methods
    fn check(&self, grid: &SudokuGrid) -> bool { ... }
    fn check_cell(&self, grid: &SudokuGrid, column: usize, row: usize) -> bool { ... }
}
Expand description

A constraint defines some property on a Sudoku grid. These are essentially the rules of the Sudoku. In standard Sudoku these are “No duplicates in a row” (RowConstraint), “No duplicates in a column” (ColumnConstraint), and “No duplicates in a block” (BlockConstraint). Here, however, the design is more flexible to allow for custom constraints.

By default, implementors of this trait only need to implement the check_number associated function, which verifies a proposed number for a specified cell. check_cell and check are implemented by default based on it, however check in particular may be very inefficient compared to a specialized implementation (it checks every cell using check_number).

Note regarding cloning: To enable wrapping constraints in a trait object, the Clone trait must not be required here. However, it is necessary later to create a Sudoku with this constraint. Implementing the Clone trait also automatically gives the CloneConstraint trait via a blanket implementation, so it is recommended to derive Clone and not worry about CloneConstraint.

Required Methods§

Source

fn check_number( &self, grid: &SudokuGrid, column: usize, row: usize, number: usize, ) -> bool

Checks whether the given number would fit into the cell specified by column and row into the grid without violating this constraint. This function does not have to check whether number is actually a valid number for this grid (i.e. in the interval [1, size]). If you require this guarantee, use Sudoku::is_valid_number instead.

For some constraints, it may be difficult to decide whether a number could actually fill the cell without making the puzzle impossible. It is therefore explicitly not required for this function to check whether the actual solution could contain that number, however it must guarantee that an error in a full grid (where all numbers are filled in) is detected. Still, it should detect errors way before that to improve the performance of solvers.

Source

fn get_groups(&self, grid: &SudokuGrid) -> Vec<Group>

Gets a vector of all groups that are defined by this constraint. A group is a set of cells which may not contain repeated numbers. As an example, the BlockConstraint defines each block as a group. Some constraints, such as the KingsMoveConstraint, do not have groups. In this particular case, a cell removed by a kings-move to the top-left may be the same as one to the bottom-right, so the cells removed by a kings-move from any particular cell cannot form a group. Such constraints should return an empty vector here.

Arguing about groups is necessary for some strategies. While it is possible to solve Sudoku with constraints which do not implement this method, getting this implementation will enable some strategies as well as improve the performance of strategic backtracking.

Provided Methods§

Source

fn check(&self, grid: &SudokuGrid) -> bool

Checks whether the given SudokuGrid matches this constraint, that is, every cell matches this constraint. By default, this runs check_cell on every cell of the grid, which may be inefficient, so custom implementations may be advantageous.

Source

fn check_cell(&self, grid: &SudokuGrid, column: usize, row: usize) -> bool

Checks whether the cell at the given position in the SudokuGrid fulfills the constraint. This is the same as calling check_number with the same coordinates and the number which is actually filled in that cell. If the cell is empty, this function always returns true.

Implementors§