[][src]Struct gameboard::board::Board

pub struct Board { /* fields omitted */ }

Board structure.

Methods

impl Board[src]

pub fn new(
    width: usize,
    height: usize,
    cell_width: usize,
    cell_height: usize,
    cell_borders: bool,
    resources: Option<ResourceTable>
) -> Self
[src]

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

pub fn init_from_vec(&mut self, cells: &Vec<Cell>, cursor: Option<Cursor>)[src]

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

pub fn init_from_str(&mut self, cells: &str, cursor: Option<Cursor>)[src]

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

Auto Trait Implementations

impl !Send for Board

impl !Sync for Board

Blanket Implementations

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

impl<T> From for T[src]

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

impl<T, U> TryInto 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<T> Any for T where
    T: 'static + ?Sized
[src]