Module open_ttt_lib::game[][src]

Expand description

Provides game logic and state management.

Examples

use open_ttt_lib::game;

// Make a new game.
let mut game = game::Game::new();

// Mark a position as owned.
let p = game::Position{ row: 0, column: 0 };
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());

// Get the state of the game to see who's turn it is or if the game is over.
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."),
};

// Display or render the game's the board.
println!("{}", game.board());

// Keep doing moves until the game is over...
game.do_move(game::Position{ row: 0, column: 1 })?;

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

Re-exports

pub use crate::board::Position;

Structs

FreePositions

An iterator over free positions in a Game; that is positions without an owner.

Game

Handles management of Tic Tac Toe games.

Enums

Error

Holds all the errors that can be reported by this module.

State

Indicates the state of the game.