pub struct Board { /* private fields */ }
Expand description
Represents the Tic Tac Toe board providing multiple ways to access individual squares.
Implementations§
Source§impl Board
impl Board
Sourcepub fn contains(&self, position: Position) -> bool
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 }));
Sourcepub fn get(&self, position: Position) -> Option<Owner>
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());
Sourcepub fn get_mut(&mut self, position: Position) -> Option<&mut Owner>
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));
Sourcepub fn iter(&self) -> Iter<'_> ⓘ
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more