[][src]Struct gameboard::game::Game

pub struct Game<R: Read, W: Write, L: InputListener<R, W>> { /* fields omitted */ }

Main game object.

All interactions with the game should be done using its API.

Methods

impl<R: Read, W: Write, L> Game<R, AlternateScreen<RawTerminal<W>>, L> where
    L: InputListener<R, AlternateScreen<RawTerminal<W>>>, 
[src]

pub fn new(input: R, output: W, listener: Rc<RefCell<L>>) -> Self[src]

Creates new game object.

Arguments

input - input stream.

output - output stream.

listener - user input listener.

Examples

use std::io::{self, Read, Write};
use std::cell::RefCell;
use std::rc::Rc;
use termion::event::Key;
use gameboard::{Game, InputListener};

struct App {}

impl<R: Read, W: Write> InputListener<R, W> for App {
    fn handle_key(&mut self, key: Key, game: &mut Game<R, W, Self>) {
        match key {
            Key::Char('q') => game.stop(),
            _ => {}
        }
    }
}

fn main() {
    let stdout = io::stdout();
    let stdout = stdout.lock();
    let stdin = io::stdin();
    let stdin = stdin.lock();

    let app = Rc::new(RefCell::new(App {}));
    let game = Rc::new(RefCell::new(Game::new(stdin, stdout, Rc::clone(&app))));
}

impl<R: Read, W: Write, L> Game<R, RawTerminal<W>, L> where
    L: InputListener<R, RawTerminal<W>>, 
[src]

pub fn new_dbg(input: R, output: W, listener: Rc<RefCell<L>>) -> Self[src]

Creates new game object.

This method is the same as new method, but for debug purposes only. The new method uses termion::screen::AlternateScreen for output, which switches to the alternate screen buffer of the terminal. When application crashes, terminal switches to the main screen buffer and all debug/crash output is wiped out. This method uses main screen buffer for output.

impl<R: Read, W: Write, L: InputListener<R, W>> Game<R, W, L>[src]

pub fn init(&mut self, board: Board, info: Option<Info>)[src]

Initializes game with board and information area (optional).

This method sets layout. Board and information will be displayed on the screen. Game state will be set to GameState::Initialized.

Panics

This method can be called in GameState::Created or GameState::Stopped states only. Panics if called in any other state.

pub fn start(&mut self)[src]

Starts listening user input.

Game state will be set to GameState::Started.

Panics

This method can be called in GameState::Initialized or GameState::Stopped states only. Panics if called in any other state. Also it panics if input listener object was dropped.

pub fn stop(&mut self)[src]

Stops listening user input.

Game state will be set to GameState::Stopped.

Panics

This method can be called in GameState::Started state only. Panics if called in any other state.

pub fn pause(&mut self, resume_key: Key)[src]

Pauses listening user input (except resume key).

Game state will be set to GameState::Paused. This method is added for convenience. The same functionality can easily be done on the client side. All key presses are ignored while in GameState::Paused state. Pressing resume key will call handle_key. User must call resume to get back to GameState::Started state.

Panics

This method can be called in GameState::Started state only. Panics if called in any other state.

pub fn resume(&mut self)[src]

Starts listening all user input.

Game state will be set to GameState::Started.

Panics

This method can be called in GameState::Paused state only. Panics if called in any other state.

pub fn get_state(&self) -> GameState[src]

Returns game state.

pub fn update_cells(&mut self, updates: CellUpdates)[src]

Updates cells content.

Panics

Panics if message dialog is open.

Examples

let mut updates = CellUpdates::with_capacity(2);
updates.push((Cell::Empty, Position(0, 1)));
updates.push((Cell::Char('x'), Position(0, 2)));
game.update_cells(updates);

pub fn update_info(&mut self, lines: &[&str])[src]

Updates information area content.

Examples

game.update_info(&[
    "This is line 1.",
    "",
    "This is line 3.",
    "This is line 4.",
]);

pub fn show_message(&mut self, lines: &[&str])[src]

Shows message dialog.

This dialog can be used to ask user a questions. This dialog is modal. You can't update board cells while it is open. It can be closed by calling hide_message.

The dialog is displayed over the board. It will be centered automatically. It can't be larger than board: last lines will be ignored, too long lines will be truncated.

lines is a list of strings to display in the dialog. If you want space between lines, add empty string to list.

Text alignment:

  • Lines are left-aligned by default
  • Lines started with |^| are centered
  • Lines started with |>| are right-aligned

Implementation note

This crate iterates Unicode strings as a set of grapheme clusters to handle characters like correctly. When we slice strings to put them inside cells or dialogs, we expect characters to have the same width. This is not always true for some Unicode symbols. Such symbols will break layout.

Examples

game.show_message(&[
    "|^|Congratulations! You win!",
    "",
    "Press 'r' to replay.",
    "Press 'q' to quit.",
]);

pub fn hide_message(&mut self)[src]

Hides message dialog.

Trait Implementations

impl<R: Read, W: Write, L: InputListener<R, W>> Drop for Game<R, W, L>[src]

Auto Trait Implementations

impl<R, W, L> !Send for Game<R, W, L>

impl<R, W, L> !Sync for Game<R, W, L>

Blanket Implementations

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

impl<T> From for T[src]

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

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