Struct takuzu::Grid
[−]
[src]
pub struct Grid { /* fields omitted */ }
An opaque container for takuzu grid manipulation.
It provides the internal logic and other convenience functions.
To create a Grid
you can:
- create an empty one yourself with
Grid::new(size)
. - use the
FromStr
trait, e.g. by calling.parse()
on a string. - use the
Source
trait, i.e. by calling.source()
on anyRead
implementor.
The Grid
type does not maintain any internal invariant. That is,
you can modify the grid as you like and break the rules.
Such grids will not be solved, though.
Methods
impl Grid
[src]
fn new(size: usize) -> Result<Grid, GridSizeError>
Creates an new empty grid of a given size.
Errors
Returns an error if the size is an odd number or 0.
fn size(&self) -> usize
Returns the number of rows/columns of the array.
fn as_slice(&self) -> &[Cell]
Extracts a slice containing the entire underlying array.
fn as_mut_slice(&mut self) -> &mut [Cell]
Extracts a mutable slice of the entire underlying array.
fn is_filled(&self) -> bool
Returns true
if the grid contains no Empty
cell.
fn is_legal(&self) -> bool
Verifies that the grid does not currently violate any of the rules.
Returns true
if the grid is legal.
fn is_cell_legal(&self, row: usize, col: usize) -> bool
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 solve(&self) -> Result<Vec<Grid>, GridError>
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.
Errors
Returns an error before any attempt at solving if the grid breaks any of the rules.
Use the is_legal()
method to know if the grid will trigger an Err
.
fn to_string_diff(&self, reference: &Grid) -> String
Suitable for printing in terminals.
Encodes the grid as a printable string containing ANSI escape codes. The grid is compared to a reference grid. The cells that differ from the reference will be displayed in color.
Warning
A red-colored cell signals that a 0
or a 1
from the reference grid
was overwritten. (Which, if reference
is the original grid
and self
is a solution, should never happen.)
Trait Implementations
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 Debug for Grid
[src]
impl Eq for Grid
[src]
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 Display for Grid
[src]
fn fmt(&self, f: &mut Formatter) -> FmtResult
Formats the value using the given formatter. Read more
impl Index<(usize, usize)> for Grid
[src]
type Output = Cell
The returned type after indexing
fn index(&self, (i, j): (usize, usize)) -> &Self::Output
The method for the indexing (container[index]
) operation
impl IndexMut<(usize, usize)> for Grid
[src]
fn index_mut(&mut self, (i, j): (usize, usize)) -> &mut Self::Output
The method for the mutable indexing (container[index]
) operation