Struct sudokugen::board::Board[][src]

pub struct Board { /* fields omitted */ }

Represents a sudoku board.

This is usually the entry point to use any of the functionality in this library. You can create a new board by simply calling new and specifying the size of the board.

use sudokugen::{Board, BoardSize};
let board: Board = Board::new(BoardSize::NineByNine);

Or you can parse an existing representation of a board using the from_str method of the FromStr trait.

use sudokugen::board::Board;

let board: Board = "
1 . . | . . . | . . .
. 2 . | . . . | . . .
. . 3 | . . . | . . .
---------------------
. . . | 4 . . | . . .
. . . | . 5 . | . . .
. . . | . . 6 | . . .
---------------------
. . . | . . . | 7 . .
. . . | . . . | . 8 .
. . . | . . . | . . 9
".parse().unwrap();

Implementations

impl Board[src]

#[must_use]pub fn new(board_size: BoardSize) -> Self[src]

Creates a new empty board of the specified size.

use sudokugen::{Board, BoardSize};

let board: Board = Board::new(BoardSize::NineByNine);

pub fn board_size(&self) -> BoardSize[src]

Returns the board size of this board..

use sudokugen::{Board, BoardSize};
let board: Board = Board::new(BoardSize::NineByNine);

assert_eq!(board.board_size(), BoardSize::NineByNine);

pub fn set(&mut self, loc: &CellLoc, value: u8) -> Option<u8>[src]

Sets the value of a cell in the board using the CellLoc structure abstraction. Returns the previous value in this location.

use sudokugen::{Board, BoardSize};
use sudokugen::board::CellLoc;

let mut board = Board::new(BoardSize::NineByNine);
let cell = CellLoc::at(0, 0, BoardSize::NineByNine);
board.set(&cell, 1);

assert_eq!(board.get(&cell), Some(1));

pub fn set_at(&mut self, l: usize, c: usize, value: u8) -> Option<u8>[src]

Convenience method to set a value in the board using line and column indexing. Returns the previous value in the board.

use sudokugen::{Board, BoardSize};

let mut board = Board::new(BoardSize::NineByNine);
board.set_at(0, 0, 1);

assert_eq!(board.get_at(0, 0), Some(1));

pub fn unset(&mut self, loc: &CellLoc) -> Option<u8>[src]

Remove a value from the board at this cell and return the previously saved value.

use sudokugen::{Board, BoardSize};    
use sudokugen::board::CellLoc;

let mut board: Board = "1... .... .... ....".parse().unwrap();
let cell = CellLoc::at(0, 0, BoardSize::FourByFour);

let old_value = board.unset(&cell);

assert_eq!(old_value, Some(1));
assert_eq!(board.get(&cell), None);

#[must_use]pub fn get(&self, cell: &CellLoc) -> Option<u8>[src]

Returns the value at a cell if there is any or None otherwise.

use sudokugen::board::Board;

let mut board: Board = "1... .... .... ....".parse().unwrap();

assert_eq!(board.get(&board.cell_at(0, 0)), Some(1));
assert_eq!(board.get(&board.cell_at(0, 1)), None);

pub fn get_at(&self, l: usize, c: usize) -> Option<u8>[src]

Same as get but more ergonomic for manual usage. Returns the value at that position or None if no value is set. See the method CellLoc::at for an explanation on the arrangement of lines and columns.

use sudokugen::board::Board;

let mut board: Board = "1... .... .... ....".parse().unwrap();

assert_eq!(board.get_at(0, 0), Some(1));
assert_eq!(board.get_at(0, 1), None);

pub fn iter_cells(&self) -> impl Iterator<Item = CellLoc>[src]

Return an iterator over all cells in the board.

use sudokugen::{Board, BoardSize};
use sudokugen::board::CellLoc;
use std::collections::BTreeSet;

let board = Board::new(BoardSize::FourByFour);

assert_eq!(
    board.iter_cells().collect::<BTreeSet<CellLoc>>(),
    (0..4).flat_map(|line| (0..4).map(move |col| {
        CellLoc::at(line.clone(), col, BoardSize::FourByFour)
    }))
        .collect::<BTreeSet<CellLoc>>()
);

Keep in mind that this iterates only over the cell location not the cell value, in order to access/modify the current value you’ll need to use the get and set methods of this board.

#[must_use]pub fn cell_at(&self, l: usize, c: usize) -> CellLoc[src]

Convenience method to return a CellLoc at this position that is compatible with this board (has the same base_size). See more about referencing cells by line and column using the at method

use sudokugen::{Board, BoardSize};

let board = Board::new(BoardSize::NineByNine);
let cell = board.cell_at(1, 1);

assert_eq!((cell.line(), cell.col()), (1, 1));

pub fn rotated(&self) -> Self[src]

Returns a new sudoku Board rotated clockwise by 90deg.

Valid sudoku puzzles are also valid if rotated 90deg, 180deg and 270deg, they are the same puzzle, however must people would have trouble realizing that they are doing the same puzzle. This function provides a cheap way to turn 1 valid puzzle into 4.

use sudokugen::board::Board;

let board: Board = "
1 2 | . .
3 4 | . .
---------
. . | . .
. . | . .
".parse().unwrap();

let rotated_board: Board = "
. . | 3 1
. . | 4 2
---------
. . | . .
. . | . .
".parse().unwrap();

assert_eq!(board.rotated(), rotated_board);

impl Board[src]

pub fn generate(board_size: BoardSize) -> Self[src]

Generate a new sudoku board with a unique solution.

This a utility function for generating a new puzzle when you don’t care about the details, it returns a new random board. It’s equivalent to calling Puzzle::generate(base_size).board().clone();.

use sudokugen::{Board, BoardSize};

let board = Board::generate(BoardSize::NineByNine);

println!("{}", board);

impl Board[src]

pub fn solve(&mut self) -> Result<(), UnsolvableError>[src]

Solves the sudoku puzzle.

Updates the current board with the solution to that sudoku puzzle.

use sudokugen::board::Board;

let mut board: Board =
    ". . . | 4 . . | 8 7 .
     4 . 3 | . . . | . . .
     2 . . | . . 3 | . . 9
     ---------------------
     . . 6 | 2 . . | . . 7
     . . . | 9 . 6 | . . .
     3 . 9 | . 8 . | . . .
     ---------------------
     . . . | . . . | . 4 .
     8 7 2 | 5 . . | . . .
     . . . | 7 2 . | 6 . .
    "
       .parse()
       .unwrap();

board.solve().unwrap();

assert_eq!(
    board,
    "695412873413879526287653419146235987728946135359187264561398742872564391934721658"
    .parse()
    .unwrap()
);

If the puzzle has no possible solutions, this function returns UnsolvableError.

let mut board: Board = "123. ...4 .... ....".parse().unwrap();
assert!(matches!(board.solve(), Err(UnsolvableError)));

Trait Implementations

impl Clone for Board[src]

impl Debug for Board[src]

impl Display for Board[src]

impl FromStr for Board[src]

type Err = MalformedBoardError

The associated error which can be returned from parsing.

fn from_str(board_as_string: &str) -> Result<Self, Self::Err>[src]

Parses a board from a string. A board will be parsed from a string with each digit representing a value in the board. Separator characters like space (‘ ’), newline (‘\n’), underscore (‘_’), dash (‘-’), and pipe (‘|’) are ignored to allow a more friendly formatting.

use sudokugen::board::Board;
let board: Board = "
1 . . | . . . | . . .
. 2 . | . . . | . . .
. . 3 | . . . | . . .
---------------------
. . . | 4 . . | . . .
. . . | . 5 . | . . .
. . . | . . 6 | . . .
---------------------
. . . | . . . | 7 . .
. . . | . . . | . 8 .
. . . | . . . | . . 9
".parse().unwrap();

Alternatively a more streamelined format can be used, which is the same but without any formatting characters.

use sudokugen::board::Board;
let board: Board = "123456789........................................................................".parse().unwrap();

impl PartialEq<Board> for Board[src]

Auto Trait Implementations

impl RefUnwindSafe for Board

impl Send for Board

impl Sync for Board

impl Unpin for Board

impl UnwindSafe for Board

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Pointable for T

type Init = T

The type for initializers.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,