pub struct Grid { /* private fields */ }Expand description
Holds the game state.
Implementations§
Source§impl Grid
impl Grid
Sourcepub fn new(x_size: usize, y_size: usize) -> Grid
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);
Sourcepub fn from_rows(rows: Vec<Vec<u8>>) -> Grid
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);
Sourcepub fn with_pipes(
&self,
pipes: &'static Map<&'static str, &'static str>,
) -> Grid
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());
Sourcepub fn get_size(&self) -> (usize, usize)
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));
Sourcepub fn slide(&self, dir: Direction) -> Result<Grid, &'static str>
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more