pub struct SudokuGrid { /* private fields */ }
Expand description
A Sudoku grid is composed of cells that are organized into blocks of a given width and height in a way that makes the entire grid a square. Consequently, the number of blocks in a row is equal to the block height and vice versa. Each cell may or may not be occupied by a number.
In ordinary Sudoku, the block width and height are both 3. Here, however, more exotic variants are permitted, for example 4x2 blocks, which would result in a grid like this:
╔═══╤═══╤═══╤═══╦═══╤═══╤═══╤═══╗
║ │ │ │ ║ │ │ │ ║
╟───┼───┼───┼───╫───┼───┼───┼───╢
║ │ │ │ ║ │ │ │ ║
╠═══╪═══╪═══╪═══╬═══╪═══╪═══╪═══╣
║ │ │ │ ║ │ │ │ ║
╟───┼───┼───┼───╫───┼───┼───┼───╢
║ │ │ │ ║ │ │ │ ║
╠═══╪═══╪═══╪═══╬═══╪═══╪═══╪═══╣
║ │ │ │ ║ │ │ │ ║
╟───┼───┼───┼───╫───┼───┼───┼───╢
║ │ │ │ ║ │ │ │ ║
╠═══╪═══╪═══╪═══╬═══╪═══╪═══╪═══╣
║ │ │ │ ║ │ │ │ ║
╟───┼───┼───┼───╫───┼───┼───┼───╢
║ │ │ │ ║ │ │ │ ║
╚═══╧═══╧═══╧═══╩═══╧═══╧═══╧═══╝
SudokuGrid
implements Display
, but only grids with a size (that is,
width or height) of less than or equal to 9 can be displayed with digits
1 to 9. Sudoku of all other sizes will raise an error.
Implementations§
Source§impl SudokuGrid
impl SudokuGrid
Sourcepub fn new(block_width: usize, block_height: usize) -> SudokuResult<SudokuGrid>
pub fn new(block_width: usize, block_height: usize) -> SudokuResult<SudokuGrid>
Creates a new, empty Sudoku grid where the blocks have 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.
§Errors
If block_width
or block_height
is invalid (zero).
Sourcepub fn parse(code: &str) -> SudokuParseResult<SudokuGrid>
pub fn parse(code: &str) -> SudokuParseResult<SudokuGrid>
Parses a code encoding a Sudoku grid. The code has to be of the format
<block_width>x<block_height>;<cells>
where <cells>
is a
comma-separated list of entries, which are either empty or a number.
The entries are assigned left-to-right, top-to-bottom, where each row
is completed before the next one is started. Whitespace in the entries
is ignored to allow for more intuitive formatting. The number of
entries must match the amount of cells in a grid with the given
dimensions, i.e. it must be (block_width · block_height)²
.
As an example, the code 2x2;1, ,2, , ,3, ,4, , , ,3, ,1, ,2
will
parse to the following grid:
╔═══╤═══╦═══╤═══╗
║ 1 │ ║ 2 │ ║
╟───┼───╫───┼───╢
║ │ 3 ║ │ 4 ║
╠═══╪═══╬═══╪═══╣
║ │ ║ 3 │ ║
╟───┼───╫───┼───╢
║ │ 1 ║ │ 2 ║
╚═══╧═══╩═══╧═══╝
§Errors
Any specialization of SudokuParseError
(see that documentation).
Sourcepub fn to_parseable_string(&self) -> String
pub fn to_parseable_string(&self) -> String
Converts the grid into a String
in a way that is consistent with
SudokuGrid::parse. That is, a grid that is converted
to a string and parsed again will not change, as is illustrated below.
use sudoku_variants::SudokuGrid;
let mut grid = SudokuGrid::new(3, 2).unwrap();
// Just some arbitrary changes to create some content.
grid.set_cell(1, 1, 4).unwrap();
grid.set_cell(1, 2, 5).unwrap();
let grid_str = grid.to_parseable_string();
let grid_parsed = SudokuGrid::parse(grid_str.as_str()).unwrap();
assert_eq!(grid, grid_parsed);
Sourcepub fn block_width(&self) -> usize
pub fn block_width(&self) -> usize
Gets the width (number of columns) of one sub-block of the grid. To ensure a square grid, this is also the number of blocks that compose the grid vertically.
Sourcepub fn block_height(&self) -> usize
pub fn block_height(&self) -> usize
Gets the height (number of rows) of one sub-block of the grid. To ensure a square grid, this is also the number of blocks that compose the grid horizontally.
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Gets the total size of the grid on one axis (horizontally or vertically). Since a square grid is enforced at construction time, this is guaranteed to be valid for both axes.
Sourcepub fn get_cell(&self, column: usize, row: usize) -> SudokuResult<Option<usize>>
pub fn get_cell(&self, column: usize, row: usize) -> SudokuResult<Option<usize>>
Gets the content of the cell at the specified position.
§Arguments
column
: The column (x-coordinate) of the desired cell. Must be in the range[0, size[
.row
: The row (y-coordinate) of the desired cell. Must be in the range[0, size[
.
§Errors
If either column
or row
are not in the specified range. In that
case, SudokuError::OutOfBounds
is returned.
Sourcepub fn has_number(
&self,
column: usize,
row: usize,
number: usize,
) -> SudokuResult<bool>
pub fn has_number( &self, column: usize, row: usize, number: usize, ) -> SudokuResult<bool>
Indicates whether the cell at the specified position has the given
number. This will return false
if there is a different number in that
cell or it is empty.
§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 in the specified cell. If it is not in the range[1, size]
,false
will always be returned.
§Errors
If either column
or row
are not in the specified range. In that
case, SudokuError::OutOfBounds
is returned.
Sourcepub fn set_cell(
&mut self,
column: usize,
row: usize,
number: usize,
) -> SudokuResult<()>
pub fn set_cell( &mut self, column: usize, row: usize, number: usize, ) -> SudokuResult<()>
Sets the content of the cell at the specified position to the given number. If the cell was not empty, the old number will be overwritten.
§Arguments
column
: The column (x-coordinate) of the assigned cell. Must be in the range[0, size[
.row
: The row (y-coordinate) of the assigned cell. Must be in the range[0, size[
.number
: The number to assign to the specified cell. Must be in the range[1, size]
.
§Errors
SudokuError::OutOfBounds
If eithercolumn
orrow
are not in the specified range.SudokuError::InvalidNumber
Ifnumber
is not in the specified range.
Sourcepub fn clear_cell(&mut self, column: usize, row: usize) -> SudokuResult<()>
pub fn clear_cell(&mut self, column: usize, row: usize) -> SudokuResult<()>
Clears the content of the cell at the specified position, that is, if contains a number, that number is removed. If the cell is already empty, it will be left that way.
§Arguments
column
: The column (x-coordinate) of the cleared cell. Must be in the range[0, size[
.row
: The row (y-coordinate) of the cleared cell. Must be in the range[0, size[
.
§Errors
If either column
or row
are not in the specified range. In that
case, SudokuError::OutOfBounds
is returned.
Sourcepub fn assign(&mut self, other: &SudokuGrid) -> SudokuResult<()>
pub fn assign(&mut self, other: &SudokuGrid) -> SudokuResult<()>
Assigns the content of another grid to this one, i.e., changes the
cells in this grid to the state in other
. The other grid must have
the same dimensions as this one.
§Errors
If the dimensions are not the same. In that case,
SudokuError::InvalidDimensions
is returned.
Sourcepub fn count_clues(&self) -> usize
pub fn count_clues(&self) -> usize
Counts the number of clues given by this grid. This is the number of non-empty cells. While on average Sudoku with less clues are harder, this is not a reliable measure of difficulty.
Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Indicates whether this grid is full, i.e. every cell is filled with a number. In this case, SudokuGrid::count_clues returns the square of SudokuGrid::size.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Indicates whether this grid is empty, i.e. no cell is filled with a number. In this case, SudokuGrid::count_clues returns 0.
Sourcepub fn is_subset(&self, other: &SudokuGrid) -> SudokuResult<bool>
pub fn is_subset(&self, other: &SudokuGrid) -> SudokuResult<bool>
Indicates whether this grid configuration is a subset of another one.
That is, all cells filled in this grid with some number must be filled
in other
with the same number. If this condition is met, true
is
returned, and false
otherwise.
§Errors
If the dimensions of this and the other
grid are not the same. In
that case, SudokuError::InvalidDimensions
is returned.
Sourcepub fn is_superset(&self, other: &SudokuGrid) -> SudokuResult<bool>
pub fn is_superset(&self, other: &SudokuGrid) -> SudokuResult<bool>
Indicates whether this grid configuration is a superset of another one.
That is, all cells filled in the other
grid with some number must be
filled in this one with the same number. If this condition is met,
true
is returned, and false
otherwise.
§Errors
If the dimensions of this and the other
grid are not the same. In
that case, SudokuError::InvalidDimensions
is returned.
Trait Implementations§
Source§impl Clone for SudokuGrid
impl Clone for SudokuGrid
Source§fn clone(&self) -> SudokuGrid
fn clone(&self) -> SudokuGrid
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more