Struct open_ttt_lib::game::Game[][src]

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

Handles management of Tic Tac Toe games.

This structure is one of the central types provided by the library. It contains the state machine logic, holds the underlying game board, and enforces the rules of Tic Tac Toe.

Examples

use open_ttt_lib::game;

// Make a new game.
let mut game = game::Game::new();
assert!(!game.state().is_game_over());

// Mark a position as owned.
let p = game::Position{ row: 0, column: 0 };
assert!(game.can_move(p));
game.do_move(p)?;

// Once a position is owned, its owner cannot be changed.
assert!(!game.can_move(p));
// Trying to move into that position causes an error to be returned.
assert!(game.do_move(p).is_err());

// To ensure each player gets to take the first turn use
// start_next_game() instead of making a new game.
game.start_next_game();

Implementations

impl Game[src]

pub fn new() -> Self[src]

Creates a new Tic Tac Toe game structure.

Note: use start_next_game() for playing consecutive games to ensure each player gets to start the game.

Examples

use open_ttt_lib::game;

let mut game = game::Game::new();

pub fn board(&self) -> &Board[src]

Gets the board associated with the game.

Access to the board is useful for providing to your display or render function.

Examples

use open_ttt_lib::{board, game};

let mut game = game::Game::new();

display(game.board());

fn display(board: &board::Board) {
    // Display or render a representation of the board.
    println!("{}", board);
}

pub fn state(&self) -> State[src]

Gets the current state of the game.

Example

use open_ttt_lib::game;

let mut game = game::Game::new();

match game.state() {
    game::State::PlayerXMove => println!("X's turn."),
    game::State::PlayerOMove => println!("O's turn."),
    game::State::PlayerXWin(_) => println!("Game Over: X wins!"),
    game::State::PlayerOWin(_) => println!("Game Over: O wins!"),
    game::State::CatsGame => println!("Game Over: cat's game."),
};

pub fn free_positions(&self) -> FreePositions<'_>

Notable traits for FreePositions<'_>

impl Iterator for FreePositions<'_> type Item = Position;
[src]

Gets an iterator over the free positions that do not have an owner and thus can be provided to do_move().

When the game is over there are no free positions.

Examples

use open_ttt_lib::game;

let mut game = game::Game::new();

let num_free_positions = game.free_positions().count();
println!("There are {} available positions.", num_free_positions);

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

Indicates if the square at the indicated position can be marked as owned.

That is, if can_move() returns true then do_move() is guaranteed to not return an error. False is returned if the position is owned, if the game is over, or if the position is outside the area of the board.

Examples

use open_ttt_lib::game;

let mut game = game::Game::new();
let p = game::Position{ row: 0, column: 0 };

// For a new game all of the positions are available.
assert!(game.can_move(p));

// Once a position is owned, its owner cannot be changed.
game.do_move(p)?;
assert!(!game.can_move(p));

pub fn do_move(&mut self, position: Position) -> Result<State, Error>[src]

Marks the indicated square as being owned by the current player.

The state of the game is updated as a side effect of do_move(). The new state is returned if the move was successful.

Errors

An error is returned if the indicated position is already owned or if the game is over.

Examples

use open_ttt_lib::game;

let mut game = game::Game::new();

match game.do_move(game::Position{ row: 0, column: 0 }) {
    Ok(new_state) => assert_eq!(game.state(), new_state),
    Err(error) => println!("{}", error),
};

pub fn start_next_game(&mut self) -> State[src]

Starts the next game by resetting the state machine ensuring the player who went second last game goes first next game.

Use of this function is preferred over making a new game with new().

Examples

use open_ttt_lib::game;

let mut game = game::Game::new();
let first_player_last_game = game.state();

// Play partial or complete game...

// Start the next game.
game.start_next_game();

// The player to start the next game is not the
// player that started last game.
assert_ne!(game.state(), first_player_last_game);
assert!(!game.state().is_game_over());

Trait Implementations

impl Clone for Game[src]

fn clone(&self) -> Game[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 Default for Game[src]

fn default() -> Self[src]

Provides a default game.

Auto Trait Implementations

impl RefUnwindSafe for Game

impl Send for Game

impl Sync for Game

impl Unpin for Game

impl UnwindSafe for Game

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, 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