gameboard::board

Struct Board

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

Board structure.

Implementations§

Source§

impl Board

Source

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

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

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

Auto Trait Implementations§

§

impl Freeze for Board

§

impl RefUnwindSafe for Board

§

impl !Send for Board

§

impl !Sync for Board

§

impl Unpin for Board

§

impl UnwindSafe for Board

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.