[−][src]Struct sudokugen::board::Board
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 base size of the board.
use sudokugen::board::Board; let board: Board = Board::new(3);
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(base_size: usize) -> Self
[src]
Creates a new empty board. The base_size
parameter represents the size
of the board, base size is the square root of the the width of the board.
so for instance a 9x9 board has a base size of 3, a 16x16 board has a base size
of 4, etc.
use sudokugen::board::Board; let board: Board = Board::new(3);
pub fn get_base_size(&self) -> usize
[src]
Returns the base size of this board, check the documentation of new
for an
explanation of base size.
use sudokugen::board::Board; let board: Board = Board::new(3); assert_eq!(board.get_base_size(), 3);
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::Board; use sudokugen::board::CellLoc; let mut board = Board::new(3); let cell = CellLoc::at(0, 0, 3); 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::Board; let mut board = Board::new(3); 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::Board; use sudokugen::board::CellLoc; let mut board: Board = "1... .... .... ....".parse().unwrap(); let cell = CellLoc::at(0, 0, 2); 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::Board; use sudokugen::board::CellLoc; use std::collections::BTreeSet; let board = Board::new(2); assert_eq!( board.iter_cells().collect::<BTreeSet<CellLoc>>(), (0..4).flat_map(|line| (0..4).map(move |col| CellLoc::at(line.clone(), col, 2))).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::Board; let board = Board::new(3); 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);
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]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,