Struct open_ttt_lib::board::Board[][src]

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

Represents the Tic Tac Toe board providing multiple ways to access individual squares.

Implementations

impl Board[src]

pub fn new(size: Size) -> Board[src]

Constructs a new board of the given size.

Panics

The minimum board size is 1x1. Panics if either the number of rows or columns is less than one.

Examples

use open_ttt_lib::board;
let size = board::Size {
    rows: 3,
    columns: 3,
};
let b = board::Board::new(size);

assert_eq!(b.size(), size);

pub fn size(&self) -> Size[src]

Gets the size of the board.

pub fn contains(&self, position: Position) -> bool[src]

Returns true if the board contains the given position.

Note positions are zero based.

Examples

use open_ttt_lib::board;

let b = board::Board::new(board::Size::from((3, 3)));

assert!(b.contains(board::Position { row: 2, column: 2 }));
// Since the positions are zero indexed, the board does not
// contain the following position.
assert!(!b.contains(board::Position { row: 3, column: 3 }));
// A negative row or column is also not contained in the board.
assert!(!b.contains(board::Position { row: -1, column: -1 }));

pub fn get(&self, position: Position) -> Option<Owner>[src]

Returns a copy of the owner at the indicated position, or None if the board does not contain the provided position.

Examples

use open_ttt_lib::board;

let b = board::Board::new(board::Size::from((3, 3)));

assert!(b.get(board::Position { row: 0, column: 0 }).is_some());
assert!(b.get(board::Position { row: -1, column: -1 }).is_none());

pub fn get_mut(&mut self, position: Position) -> Option<&mut Owner>[src]

Gets a mutable reference to the owner at the indicated position.

This allows the owner of the position to be changed. None is returned if the board does not contain the provided position.

Examples

use open_ttt_lib::board;

let mut b = board::Board::new(board::Size::from((3, 3)));
let position = board::Position { row: 2, column: 2 };

// Change the owner of the position to Player X.
if let Some(owner) = b.get_mut(position) {
    *owner = board::Owner::PlayerX;
}

assert_eq!(b.get(position), Some(board::Owner::PlayerX));

pub fn iter(&self) -> Iter<'_>

Notable traits for Iter<'_>

impl Iterator for Iter<'_> type Item = (Position, Owner);
[src]

Gets an iterator over all the positions in the board.

The iterator provides tuples containing the position and the owner of the position. The items are returned in arbitrary order.

Examples

use open_ttt_lib::board;

let b = board::Board::new(board::Size::from((3, 3)));

// Print the owner of every position.
for (position, owner) in b.iter() {
    println!("{:?} is owned by {:?}", position, owner);
}

Trait Implementations

impl Clone for Board[src]

fn clone(&self) -> Board[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Display for Board[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

This provides simple formatted output of the board.

This is suitable for use in simple console applications or debugging purposes.

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]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

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

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

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

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

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

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T> ToString for T where
    T: Display + ?Sized
[src]

pub default fn to_string(&self) -> String[src]

Converts the given value to a String. Read more

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

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

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

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

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V