[][src]Struct wasm_terminal_2048::game::Game

pub struct Game { /* fields omitted */ }

wasm-terminal-2048

wasm-terminal-2048 library is composed of tile module and game module. The tile has Board struct that store 16 tile values and can move tiles toward four directions. The other game struct includes Game struct that is responsible for high-level logic that is the bridge between the front-end, such as a command line terminal or a browser application, and the back-end game logic.

Game is used to control game iterations, execute user actions and update internal tile values in the board. The example is as below. The complete example can be found in cli/src/main.rs for command line terminal or wasm/src/lib.rs for webassembly

 
 // start a new game
 let mut game = Game::new();
 game.start_game();
 // call render function depends on the front-end
 render()

 // looping: process user input and execute an action 
 for c in stdin.keys() {
   match c.unwrap() {
     Key::Down |  Key::Char('j') => game.action(Direction::Down), 
     _ => continue,
   }

   // move to next iteration, and render the updated board
   game.next();
   render(&mut stdout, &game);
 }

Implementations

impl Game[src]

pub fn new() -> Self[src]

Create a new board with the default tile values 0

pub fn start_game(&mut self)[src]

This method resets the internal tile values, and start a new game

pub fn action(&mut self, dir: Direction)[src]

By a given direction, updates the internal tile values after the movement

Arguments

  • Direction The direction of tile movement

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

Get the internal Board

pub fn get_steps(&self) -> u32[src]

Get the steps of the current game

pub fn next(&mut self) -> bool[src]

Generate a new tile and increment steps. If return values is false, it means there is no empty tile for new tile generation.

Auto Trait Implementations

impl !RefUnwindSafe for Game[src]

impl !Send for Game[src]

impl !Sync for Game[src]

impl Unpin for Game[src]

impl !UnwindSafe for Game[src]

Blanket Implementations

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

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

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

impl<T> From<T> for T[src]

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

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.

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.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,