Struct tools_2048::Game

source ·
pub struct Game { /* private fields */ }
Expand description

A struct that represents the 2048 game.

Implementations§

source§

impl Game

source

pub fn new(size: usize) -> Result<Self, Error>

Creates a new game of 2048.

Arguments
  • n: The size of the board (nxn). Must be at least 4.
Returns
  • Ok(Game): The game was created successfully.
  • Err(Error): The game was not created successfully.
Errors
  • Error::InvalidSize: The size of the board is invalid (less than 4).
source

pub fn from_existing(board: &[Vec<u64>], score: u64) -> Result<Self, Error>

Creates a game of 2048 from an existing board.

Arguments
  • board: The board to use.
  • score: The score of the game.
Returns
  • Ok(Game): The game was created successfully.
  • Err(Error): The game was not created successfully.
Errors
  • Error::InvalidSize: The size of the board is invalid. Must be at least 4.
  • Error::InvalidBoard: The board is invalid. Must be quadratic.
  • Error::InvalidValue: The board contains invalid values. Must be 0 or powers of 2 (except 1).
source

pub fn board(&self) -> &Vec<Vec<u64>>

Returns the reference to the board.

Returns
  • &Vec<Vec<u64>>: The reference to the board.
source

pub fn result(&self) -> GameResult

Returns the result of the game.

Returns
  • Result::Victory: The game is won, 2048 was reached.
  • Result::Pending: The game is in progress, 2048 is not reached yet.
  • Result::Loss: The game is over, 2048 was not reached.
source

pub fn score(&self) -> u64

Returns the score of the game.

Returns
  • u64: The score of the game.
source

pub fn size(&self) -> usize

Returns the size of the board.

Returns
  • usize: The size of the board. The board is usizexusize.
source

pub fn state(&self) -> GameState

Returns the state of the game.

Returns
  • State::InProgress: The game is in progress.
  • State::GameOver: The game is over.
source

pub fn make_move(&mut self, direction: GameMove) -> bool

Make a move in the game.

Arguments
  • direction: The direction to move in.
Returns
  • true - The move was successful.
  • false - The move was invalid/impossible.
source

pub fn find_best_move(&self, depth: usize) -> Result<GameMove, Error>

Find the best move to make based on the current board state. Based on Monte Carlo algorithm (randomized guessing). Uses multiple threads to speed up the process.

Arguments
  • depth - The number of simulated games to play to determine the best move. Recommended value is 1000.
Returns
  • Ok(GameMove) - The best move to make.
  • Err(Error) - There are no valid moves left.
Errors
  • Error::NoValidMove - There are no valid moves left.

Trait Implementations§

source§

impl Debug for Game

source§

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

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

impl Default for Game

source§

fn default() -> Self

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

impl Display for Game

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Game

§

impl Send for Game

§

impl Sync for Game

§

impl Unpin for Game

§

impl UnwindSafe for Game

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§

default 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>,

§

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>,

§

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.
§

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

§

fn vzip(self) -> V