[−][src]Struct bitboard_xo::XO
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]
pub fn play(
&mut self,
pos: XOPos
) -> Result<Option<XOTokenWinState>, XOGameError>
[src]
&mut self,
pos: XOPos
) -> Result<Option<XOTokenWinState>, XOGameError>
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 returnOption::Some
containing the type of game ending asXOTokenWinState
-
If after playing at
pos
, the game doesn't end -> it returnOption::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
- None if game didn't end yet
- Some(XOTokenWinState::X)) if X won
- Some(XOTokenWinState::O) if O won
- Some(XOTokenWinState::Stale) if game ended in stalemate
Notes
-
This function doesn't directly calculate the winner. the calculation are already done when calling
XO::play
orXOBoard::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 ofXO::play
's output. If this function outputNone
thenXO::play
will output error variant ofXOGameError::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 BoardIterpub 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]
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
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,