Game

Struct Game 

Source
pub struct Game<const SIZE: usize> { /* private fields */ }
Expand description

A struct that represents the 2048 game.

Implementations§

Source§

impl<const SIZE: usize> Game<SIZE>

Source

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

Creates a new game of 2048.

§Returns
  • Ok(Game): The game was created successfully.
  • Err(Error): The game was not created successfully.
§Errors
  • Error::InvalidSize: The SIZE is invalid. Must be at least 4.
Source

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

Creates a game of 2048 from an existing board. The board must be a square matrix filled with 0 for empty tiles and powers of 2 for filled tiles.

§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 is invalid. Must be at least 4.
  • Error::InvalidValue: The board contains invalid value. Must be 0 or a power of 2, starting from 2.
Source

pub fn board(&self) -> &[[u64; SIZE]; SIZE]

Returns the reference to the board. The board is a square matrix filled with 0 for empty tiles and powers of 2 for filled tiles.

§Returns
  • &[[u64; SIZE]; SIZE]: 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<const SIZE: usize> Debug for Game<SIZE>

Source§

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

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

impl<const SIZE: usize> Display for Game<SIZE>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<const SIZE: usize> Freeze for Game<SIZE>

§

impl<const SIZE: usize> RefUnwindSafe for Game<SIZE>

§

impl<const SIZE: usize> Send for Game<SIZE>

§

impl<const SIZE: usize> Sync for Game<SIZE>

§

impl<const SIZE: usize> Unpin for Game<SIZE>

§

impl<const SIZE: usize> UnwindSafe for Game<SIZE>

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