pub struct Board { /* private fields */ }
Expand description

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

Creates a new empty board of the specified size.

use sudokugen::{Board, BoardSize};

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

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);

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));

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));

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);

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);

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);

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.

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));

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);

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);

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

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more

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();
The associated error which can be returned from parsing.
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.