[][src]Struct bitboard_xo::XO

pub struct XO { /* fields omitted */ }

Representing xo game. can be see as mutating version of XOBoard.

It's implemented as wrapper of XOBoard

Methods

impl XO[src]

pub fn new() -> Self[src]

Construct a new game with empty board and X as starting player.

Internally crate board with XOBoard::empty and wrap it.

use bitboard_xo::{XO, XOBoard, XOToken};
let game = XO::new();
assert_eq!(game.board(), XOBoard::empty());
assert_eq!(game.turn(), XOToken::X);

pub fn from_board(board: XOBoard) -> Self[src]

Construct a game from XOBoard.

this function only wrap XOBoard without modifying anything in it

pub fn play(
    &mut self,
    pos: XOPos
) -> Result<Option<XOTokenWinState>, XOGameError>
[src]

Play the game! This turn's player will try to play at position pos

Assuming no error occured:

  • If after playing at pos, the game ended -> it return Option::Some containing the type of game ending as XOTokenWinState

  • If after playing at pos, the game doesn't end -> it return Option::None

(the same as XO::win_state)

Error

May return Err variant XOGameError::AlreadyPlayedError if position pos isn't empty (most likely because this function get called with the same pos before)

May return Err variant XOGameError::GameEndedError if game has already ended (X win, O win, or stalemate) before calling this function

Example

use bitboard_xo::{XO, XOPos, XOGameError, XOBoard, XOTokenWinState};
use bitboard_xo::XOToken::*;

/* create game with this configuration:
X X .
. O .
. . .
*/
let mut game = XO::from_board(XOBoard::from_maybe_token_array([
    Some(X), Some(X), None,
    None   , Some(O), None,
    None   , None   , None,
]).swap_turn());

// try playing at occupied position
assert_eq!(
    game.play(XOPos::row_col(0, 0)?),
    Err(XOGameError::AlreadyPlayedError {index: 0})
);

// o play and game continue
assert_eq!(game.turn(), O);
assert_eq!(
    game.play(XOPos::row_col(1, 0)?),
    Ok(None)
);

// x play and win
assert_eq!(game.turn(), X);
assert_eq!(
    game.play(XOPos::row_col(0, 2)?),
    Ok(Some(XOTokenWinState::X))
);

// try to play after ended
assert_eq!(
    game.play(XOPos::row_col(2, 2)?),
    Err(XOGameError::GameEndedError)
);

pub fn swap_turn(&mut self)[src]

Swap current turn's play (player who going to play) (X -> O, O -> X)

use bitboard_xo::{XO, XOToken};

let mut game = XO::new();
assert_eq!(game.turn(), XOToken::X);

game.swap_turn();
assert_eq!(game.turn(), XOToken::O);

pub fn turn(self) -> XOToken[src]

get current turn's play (player who going to play)

use bitboard_xo::{XO, XOToken, XOPos};

let mut game = XO::new();
assert_eq!(game.turn(), XOToken::X);

game.play(XOPos::row_col(1, 1)?);
assert_eq!(game.turn(), XOToken::O);

pub fn win_state(self) -> Option<XOTokenWinState>[src]

Get information about who (if any) won the game

Return

Notes

  • This function doesn't directly calculate the winner. the calculation are already done when calling XO::play or XOBoard::play, this function only retrieve the calculated information so it can be called repeatably without much performance penalty.

  • This function output are the same as in Ok variant of XO::play's output. If this function output None then XO::play will output error variant of XOGameError::AlreadyPlayedError

pub fn reset(&mut self)[src]

Reset the board to empty default board

use bitboard_xo::{XO, XOBoard, XOPos};

let mut game = XO::new();

game.play(XOPos::row_col(1, 1)?);
assert_ne!(game, XO::new());
assert_ne!(game.board(), XOBoard::empty());

game.reset();
assert_eq!(game, XO::new());
assert_eq!(game.board(), XOBoard::empty());

Important traits for BoardIter
pub fn iter(self) -> BoardIter[src]

Return immutable iterator through the board

pub fn board(self) -> XOBoard[src]

Retrieve the wrapped XOBoard.

This method copy XOBoard out (since XOBoard implement Copy) so modifying the returned board will not affect the wrapped board. If you want to modify the board, see XO::board_mut

pub fn board_mut(&mut self) -> &mut XOBoard[src]

Retrieve a mutable reference to wrapped XOBoard.

This method allowed modification of wrapped XO::board but might be less efficient than XO::board which use copying (usually, XO::board's < pointer's size) but unable modify the wrapped board

Trait Implementations

impl Clone for XO[src]

impl Copy for XO[src]

impl Debug for XO[src]

impl Default for XO[src]

impl Display for XO[src]

impl Eq for XO[src]

impl From<XOBoard> for XO[src]

impl PartialEq<XO> for XO[src]

impl StructuralEq for XO[src]

impl StructuralPartialEq for XO[src]

Auto Trait Implementations

impl RefUnwindSafe for XO

impl Send for XO

impl Sync for XO

impl Unpin for XO

impl UnwindSafe for XO

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

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

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

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

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

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

type Owned = T

The resulting type after obtaining ownership.

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

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.

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.