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>
impl<const SIZE: usize> Game<SIZE>
Sourcepub fn from_existing(
board: &[[u64; SIZE]; SIZE],
score: u64,
) -> Result<Self, Error>
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.
Sourcepub fn board(&self) -> &[[u64; SIZE]; SIZE]
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.
Sourcepub fn result(&self) -> GameResult
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.
Sourcepub fn state(&self) -> GameState
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.
Sourcepub fn find_best_move(&self, depth: usize) -> Result<GameMove, Error>
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§
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> 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