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]
impl Game
[src]pub fn new() -> Self
[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]
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]
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]
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]
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]
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]
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
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> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
[src]type Owned = T
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
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]
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<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,