Struct Board

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

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

Implementations§

Source§

impl Board

Source

pub fn new(size: Size) -> Board

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

pub fn size(&self) -> Size

Gets the size of the board.

Source

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

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

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

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

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

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

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

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§

Source§

impl Clone for Board

Source§

fn clone(&self) -> Board

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Display for Board

Source§

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

This provides simple formatted output of the board.

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

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
Source§

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

Source§

fn vzip(self) -> V