Grid

Struct Grid 

Source
pub struct Grid { /* private fields */ }
Expand description

Holds the game state.

Implementations§

Source§

impl Grid

Source

pub fn new(x_size: usize, y_size: usize) -> Grid

Creates a new grid with the given size and adds two numbers on random positions. The size must be greater than 0 and greater than 1 in at least one dimension.

§Examples
use cli_2048::Grid;
 
//Create a 4x4 grid
let grid = Grid::new(4, 4);
Source

pub fn from_rows(rows: Vec<Vec<u8>>) -> Grid

Creates a new grid from a predefined grid.

§Examples
use cli_2048::Grid;
 
//Create a 4x4 vector
let rows = vec![vec![0; 4]; 4];
//Create a grid from the vector
let grid = Grid::from_rows(rows);
Source

pub fn with_pipes( &self, pipes: &'static Map<&'static str, &'static str>, ) -> Grid

Returns a new instance of the grid, with a custom pipe-map for the borders.

§Examples
use cli_2048::Grid;
use cli_2048::PIPEMAPS;
 
//Create a 4x4 grid
let grid = Grid::new(4, 4);
//Create a grid with a custom pipe-map
let grid = grid.with_pipes(PIPEMAPS.get("Medium").unwrap());
 
//Or like this
let grid = Grid::new(4, 4).with_pipes(PIPEMAPS.get("Medium").unwrap());
 
Source

pub fn get_size(&self) -> (usize, usize)

Gets the size of the grid in characters and with borders.

§Examples
use cli_2048::Grid;
 
//Create a 4x4 grid
let grid = Grid::new(4, 4);
 
let x_size = 4*2 + (4-1) + 2;
let y_size = 4 + (4-1) + 2;

assert_eq!(grid.get_size(), (x_size, y_size));
 
Source

pub fn slide(&self, dir: Direction) -> Result<Grid, &'static str>

Returns a new Grid from the previous. Slides and combines the grid in the given direction. If the tiles change a new tile will be added at a random empty position. If the grid is full the game is over (Err(“no more options”)).

§Examples
use cli_2048::Grid;
use cli_2048::Direction;
 
//Create a 4x4 grid
let grid = Grid::new(4, 4);
let grid = grid.slide(Direction::Up);
let grid = grid.slide(Direction::Down);
 

Trait Implementations§

Source§

impl Debug for Grid

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Grid

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Grid

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Grid

§

impl RefUnwindSafe for Grid

§

impl Send for Grid

§

impl Sync for Grid

§

impl Unpin for Grid

§

impl UnwindSafe for Grid

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

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

Source§

fn vzip(self) -> V