pub struct Board { /* private fields */ }
Expand description
Board structure.
Implementations§
Source§impl Board
impl Board
Sourcepub fn new(
width: usize,
height: usize,
cell_width: usize,
cell_height: usize,
cell_borders: bool,
resources: Option<ResourceTable>,
) -> Self
pub fn new( width: usize, height: usize, cell_width: usize, cell_height: usize, cell_borders: bool, resources: Option<ResourceTable>, ) -> Self
Creates new board.
§Arguments
width
- horizontal number of cells (number of columns)
height
- vertical number of cells (number of rows)
cell_width
- cell width in characters
cell_height
- cell height in characters
cell_borders
- if true
show cell borders
resources
- resource table (optional)
§Examples
A board for 3x3 tic-tac-toe game. Cell has 10x5 size to look square in terminal.
fn create_resources() -> ResourceTable {
let mut res = ResourceTable::new();
res.insert(0, String::from(" OOO O O O O O O OOO "));
res.insert(1, String::from(" X X X X X X X X X "));
res
}
let mut board = Board::new(3, 3, 10, 5, true, Some(create_resources()));
Sourcepub fn init_from_vec(&mut self, cells: &Vec<Cell>, cursor: Option<Cursor>)
pub fn init_from_vec(&mut self, cells: &Vec<Cell>, cursor: Option<Cursor>)
Initializes board with cells and cursor (optional).
The cells will be filled from vector by rows.
§Panics
Panics cells
contain wrong number of elements.
§Examples
let mut board = Board::new(2, 2, 1, 1, false, None));
board.init_from_vec(&vec![Cell::Empty, Cell::Char('x'), Cell::Empty, Cell::Char('o')],
None);
Sourcepub fn init_from_str(&mut self, cells: &str, cursor: Option<Cursor>)
pub fn init_from_str(&mut self, cells: &str, cursor: Option<Cursor>)
Initializes board with cells and cursor (optional).
The cells will be filled from string by rows.
This is an additional initialization method. It might be useful if board has 1x1 cells and all cells contain 1 character without styling.
§Implementation note
We iterate string using chars()
method, it iterates through Unicode code points. Do not
use Unicode symbols which consist of more than 1 code points.
§Panics
Panics cells
contain wrong number of elements.
Panics if cell size is not 1x1.
§Examples
let mut board = Board::new(4, 4, 1, 1, false, None));
board.init_from_str(&"x o x o", None);
The following code does the same.
let mut board = Board::new(4, 4, 1, 1, false, None));
board.init_from_vec(&vec![Cell::Char('x'), Cell::Empty, Cell::Empty, Cell::Empty,
Cell::Empty, Cell::Char('o'), Cell::Empty, Cell::Empty,
Cell::Empty, Cell::Empty, Cell::Char('x'), Cell::Empty,
Cell::Empty, Cell::Empty, Cell::Empty, Cell::Char('o')],
None);