Struct sudoku::Sudoku [−][src]
pub struct Sudoku(_);
The main structure exposing all the functionality of the library
Sudoku
s can generated, constructed from arrays or parsed from &str
s
in either the line or block format.
Methods
impl Sudoku
[src]
impl Sudoku
pub fn generate_filled() -> Self
[src]
pub fn generate_filled() -> Self
Generate a random, solved sudoku
pub fn generate_unique() -> Self
[src]
pub fn generate_unique() -> Self
Generate a random, uniquely solvable sudoku The puzzles are minimal in that no cell can be removed without losing uniquess of solution Most puzzles generated by this are easy
pub fn generate_unique_from(sudoku: Sudoku) -> Self
[src]
pub fn generate_unique_from(sudoku: Sudoku) -> Self
Generate a random, uniqely solvable sudoku
that has the same solution as the given sudoku
by removing the contents of some of its cells.
The puzzles are minimal in that no cell can be removed without losing uniquess of solution.
Most puzzles generated by this from solved sudokus are easy.
If the source sudoku
is invalid or has multiple solutions, it will be returned as is.
pub fn from_bytes_slice(bytes: &[u8]) -> Result<Sudoku, ()>
[src]
pub fn from_bytes_slice(bytes: &[u8]) -> Result<Sudoku, ()>
Creates a sudoku from a byte slice. All numbers must be below 10. Empty cells are denoted by 0, clues by the numbers 1-9. The slice must be of length 81.
pub fn from_bytes(bytes: [u8; 81]) -> Result<Sudoku, ()>
[src]
pub fn from_bytes(bytes: [u8; 81]) -> Result<Sudoku, ()>
Creates a sudoku from a byte array. All numbers must be below 10. Empty cells are denoted by 0, clues by the numbers 1-9.
pub fn from_str_line(s: &str) -> Result<Sudoku, LineParseError>
[src]
pub fn from_str_line(s: &str) -> Result<Sudoku, LineParseError>
Reads a sudoku in the line format.
This is a concatenation of the digits in each cell, line by line from top to bottom.
Digits must be in range of 1-9.
'_'
, '.'
and '0'
are accepted interchangeably as empty cells
An optional comment is allowed after the sudoku, separated by ASCII whitespace, commas or semicolons, that is, any of ' ', '\t', '\n', '\r', ',', ';'
Example:
..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3.. optional comment
Stops parsing after the first sudoku
pub fn from_str_block(s: &str) -> Result<Sudoku, BlockParseError>
[src]
pub fn from_str_block(s: &str) -> Result<Sudoku, BlockParseError>
Reads a sudoku in the block format with or without field delimiters
Digits must be in range of 1-9.
'_'
, '.'
and '0'
are accepted interchangeably as empty cells
Optional comments are accepted after each line. They must be delimited by ' ' or '\t', i.e. a space or a tab character.
__3_2_6__ optional comment
9__3_5__1 another comment
__18_64__
__81_29__
7_______8
__67_82__
__26_95__
8__2_3__9
__5_1_3__
alternatively also with field delimiters
__3|_2_|6__ optional comment
9__|3_5|__1 another comment
__1|8_6|4__
---+---+--- comment: "-----------", i.e. '-' 11 times is also allowed
__8|1_2|9__ delimiters have to be consistent across the entire
7__|___|__8 grid
__6|7_8|2__
---+---+---
__2|6_9|5__
8__|2_3|__9
__5|_1_|3__
Stops parsing after the first sudoku
pub fn from_str_block_permissive(s: &str) -> Result<Sudoku, NotEnoughRows>
[src]
pub fn from_str_block_permissive(s: &str) -> Result<Sudoku, NotEnoughRows>
Reads a sudoku in a variety of block formats with very few constraints.
'_', '.' and '0' are treated as empty cells. '1' to '9' as clues. Each line needs to have 9 valid cells. Lines that don't contain 9 valid entries are ignored.
Stops parsing after the first sudoku.
Due to the lax format rules, the only failure that can occur is that there are not enough rows.
pub fn solve_one(self) -> Option<Sudoku>
[src]
pub fn solve_one(self) -> Option<Sudoku>
Find a solution to the sudoku. If multiple solutions exist, it will not find them and just stop at the first.
Return None
if no solution exists.
pub fn solve_unique(self) -> Option<Sudoku>
[src]
pub fn solve_unique(self) -> Option<Sudoku>
Solve sudoku and return solution if solution is unique.
pub fn count_at_most(self, limit: usize) -> usize
[src]
pub fn count_at_most(self, limit: usize) -> usize
Counts number of solutions to sudoku up to limit
This solves the sudoku but does not return the solutions which allows for slightly faster execution.
pub fn is_uniquely_solvable(self) -> bool
[src]
pub fn is_uniquely_solvable(self) -> bool
Checks whether sudoku has one and only one solution. This solves the sudoku but does not return the solution which allows for slightly faster execution.
pub fn solve_at_most(self, limit: usize) -> Vec<Sudoku>
[src]
pub fn solve_at_most(self, limit: usize) -> Vec<Sudoku>
Solve sudoku and return the first limit
solutions it finds. If less solutions exist, return only those. Return None
if no solution exists.
No specific ordering of solutions is promised. It can change across versions.
pub fn solve_at_most_buffer(
self,
target: &mut [[u8; 81]],
limit: usize
) -> usize
[src]
pub fn solve_at_most_buffer(
self,
target: &mut [[u8; 81]],
limit: usize
) -> usize
Counts number of solutions to sudoku up to limit
and writes any solution found into target
up to its capacity. Additional solutions will be counted but not saved.
No specific ordering of solutions is promised. It can change across versions.
This is primarily meant for C FFI.
pub fn is_solved(&self) -> bool
[src]
pub fn is_solved(&self) -> bool
Check whether the sudoku is solved.
pub fn n_clues(&self) -> u8
[src]
pub fn n_clues(&self) -> u8
Returns number of filled cells
pub fn shuffle(&mut self)
[src]
pub fn shuffle(&mut self)
Perform various transformations that create a different but equivalent sudoku. The transformations preserve the sudoku's validity and the amount of solutions as well a the applicability of solution strategies. Shuffling can be used to quickly generate sudokus of the same difficulty as a given sudoku.
Transformations that are applied:
- Relabel numbers, e.g. swap all 1s and all 3s (9! permutations)
- Permute rows within their band and columns within their stack (3!
3 * 2 permutations) - Permute stacks and bands (3!
2 permutations) - Transpose the board, i.e. mirror it along the diagonal (2 permutations) The remaining rotations as well as mirrorings can be produced by a combination with the other transformations
This results in a total of up to 2 * 9! * 3!
pub fn iter(&self) -> Map<Iter<'a, u8>, fn(_: &u8) -> Option<u8>>
[src]
pub fn iter(&self) -> Map<Iter<'a, u8>, fn(_: &u8) -> Option<u8>>
Returns an Iterator over sudoku, going from left to right, top to bottom
pub fn to_bytes(self) -> [u8; 81]
[src]
pub fn to_bytes(self) -> [u8; 81]
Returns a byte array for the sudoku. Empty cells are denoted by 0, clues by the numbers 1-9.
pub fn to_str_line(&self) -> SudokuLine
[src]
pub fn to_str_line(&self) -> SudokuLine
Returns a representation of the sudoku in line format that can be printed and which derefs into a &str
use sudoku::Sudoku; let mut grid = [0; 81]; grid[3] = 5; let sudoku = Sudoku::from_bytes(grid).unwrap(); let line = sudoku.to_str_line(); // :SudokuLine println!("{}", line); let line_str: &str = &line; assert_eq!( "...5.............................................................................", line_str );
pub fn display_block(&self) -> SudokuBlock
[src]
pub fn display_block(&self) -> SudokuBlock
Returns a value that, prints a block representation of the sudoku
when formatted via the Display
trait.
use sudoku::Sudoku; let mut grid = [0; 81]; grid[3] = 5; grid[36..45].copy_from_slice(&[1, 2, 3, 4, 5, 6, 7, 8, 9]); let sudoku = Sudoku::from_bytes(grid).unwrap(); let block = sudoku.display_block(); // :SudokuBlock let block_string = format!("{}", block); assert_eq!( &block_string, " ___ 5__ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ 123 456 789 ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___" );
Trait Implementations
impl Copy for Sudoku
[src]
impl Copy for Sudoku
impl Clone for Sudoku
[src]
impl Clone for Sudoku
fn clone(&self) -> Sudoku
[src]
fn clone(&self) -> Sudoku
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl PartialEq for Sudoku
[src]
impl PartialEq for Sudoku
fn eq(&self, other: &Sudoku) -> bool
[src]
fn eq(&self, other: &Sudoku) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
fn ne(&self, other: &Rhs) -> bool
This method tests for !=
.
impl PartialOrd for Sudoku
[src]
impl PartialOrd for Sudoku
The ordering is lexicographical in the cells of the sudoku going from left to right, top to bottom
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
fn gt(&self, other: &Rhs) -> bool
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
fn ge(&self, other: &Rhs) -> bool
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl Ord for Sudoku
[src]
impl Ord for Sudoku
fn cmp(&self, other: &Self) -> Ordering
[src]
fn cmp(&self, other: &Self) -> Ordering
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self
1.21.0[src]
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.21.0[src]
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
impl Hash for Sudoku
[src]
impl Hash for Sudoku
fn hash<H>(&self, state: &mut H) where
H: Hasher,
[src]
fn hash<H>(&self, state: &mut H) where
H: Hasher,
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl Eq for Sudoku
[src]
impl Eq for Sudoku
impl Debug for Sudoku
[src]
impl Debug for Sudoku
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error>
[src]
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error>
Formats the value using the given formatter. Read more
impl Display for Sudoku
[src]
impl Display for Sudoku