Struct takuzu::Grid [] [src]

pub struct Grid(_);

A container for takuzu grid manipulation.

It provides the internal logic and other convenience functions. To create a Grid you can:

  • create an Array yourself and use Grid::new(array).
  • use the FromStr trait, e.g. by calling .parse() on a string.
  • use the Source trait, i.e. by calling .source() on any Read implementor.

Methods

impl Grid
[src]

fn new(array: Array) -> Result<Grid(GridError, Array)>

Creates a Grid from a preexisting array.

Failure

Returns an error enum and the invalid array if the grid is not a square of non-nul, even size or if the grid is illegal.

fn size(&self) -> usize

Returns the size of the grid.

fn into_inner(self) -> Array

Consumes a Grid and returns the underlying array.

fn is_filled(&self) -> bool

Returns true if the grid contains no empty cell.

Verifies that the grid does not currently violate any of the rules. Returns true if the grid is legal.

Verifies that a certain cell does not violate any of the rules. Returns true if the value is legal.

fn next_empty(&self) -> Option<(usize, usize)>

Returns the index of the first empty cell or None if the grid is filled.

fn apply_rules(&mut self) -> bool

Skims through the grid once, filling in the blanks where the value is unambiguous according to one of the rules, then returns if the grid was modified or repeats the operation for the next rule. Each rule is applied once at the most.

Returns true if the grid was modified.

Warning

Does not guarantee the legality of the modifications. For performance reasons, deductions made from a rule are not checked for legality against the other rules. This can result in grids with no legal solution being filled illegally. Grids with one or more legal solution(s) are not affected.

Examples

let mut grid = io::stdin().source().unwrap();
while grid.apply_rules() {}
println!("{}", grid);

fn solve(&self) -> Vec<Grid>

Solves the grid using both rules logic and a backtracking algorithm, and returns an array containing the solution(s). If no solution exists, an empty array is returned.

fn to_string_diff(&self, grid_ref: &Grid) -> String

Suitable for terminals.

Converts the grid to a printable string (containing escape characters). The grid is compared to a reference grid. The cells that differ from the reference will be displayed in color.

A red-colored cell signals that a 0 or a 1 from the reference grid was overwritten. (Which, if grid_ref is the original grid and self is a solution, should never happen.)

Trait Implementations

impl PartialEq for Grid
[src]

fn eq(&self, __arg_0: &Grid) -> bool

This method tests for self and other values to be equal, and is used by ==. Read more

fn ne(&self, __arg_0: &Grid) -> bool

This method tests for !=.

impl Hash for Grid
[src]

fn hash<__H: Hasher>(&self, __arg_0: &mut __H)

Feeds this value into the state given, updating the hasher as necessary.

fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher
1.3.0

Feeds a slice of this type into the state provided.

impl Eq for Grid
[src]

impl Debug for Grid
[src]

fn fmt(&self, __arg_0: &mut Formatter) -> Result

Formats the value using the given formatter.

impl Clone for Grid
[src]

fn clone(&self) -> Grid

Returns a copy of the value. Read more

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

Performs copy-assignment from source. Read more

impl Display for Grid
[src]

fn fmt(&self, f: &mut Formatter) -> Result

Formats the value using the given formatter.

impl FromStr for Grid
[src]

type Err = GridParseError

The associated error which can be returned from parsing.

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more

impl Index<(usize, usize)> for Grid
[src]

type Output = Option<bool>

The returned type after indexing

fn index(&self, index: (usize, usize)) -> &Self::Output

The method for the indexing (Foo[Bar]) operation

impl IndexMut<(usize, usize)> for Grid
[src]

fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output

The method for the indexing (Foo[Bar]) operation