Struct tfe::Game [−][src]
pub struct Game { pub board: u64, }
Struct used to play a single game of 2048.
tfe::Game
uses a single u64
as board value.
The board itself is divided into rows (x4 16 bit "row" per "board") which are
divided into tiles (4x 4 bit "nybbles" per "row").
All manipulations are done using bit-shifts and a precomputed table of moves and scores. Every move is stored as four lookups total, one for each row. The result of XOR'ing each row back into the board at the right position is the output board.
Fields
board: u64
Methods
impl Game
[src]
impl Game
pub fn new() -> Self
[src]
pub fn new() -> Self
Constructs a new tfe::Game
.
Game
stores a board internally as a u64
.
Examples
Simple example:
use tfe::Game; let mut game = Game::new();
Accessing board value:
use tfe::Game; let mut game = Game::new(); println!("{:016x}", game.board);
pub fn play<F: Fn(u64, &Vec<Direction>) -> Direction>(mv: F) -> Self
[src]
pub fn play<F: Fn(u64, &Vec<Direction>) -> Direction>(mv: F) -> Self
Like new
but takes a callback that accepts two parameters and returns
a Direction
. The parameters passed to the callback:
u64
: The current board&Vec<Direction>
: A list of attempted moves that had no effect. Gets cleared when a move succeeds.
Examples
Simple example:
use tfe::{Game, Direction}; let game = Game::play(|_board, failed| Direction::sample_without(failed));
In this example, the variable game
will have a value of a single Game
played to
completion. A game is over when it has no moves left. This is true when all possible
moves return the same resulting board as before the move was executed.
pub fn execute(board: u64, direction: &Direction) -> u64
[src]
pub fn execute(board: u64, direction: &Direction) -> u64
Returns board
moved in given direction
.
- When
Direction::Left
, return board moved left - When
Direction::Right
, return board moved right - When
Direction::Down
, return board moved down - When
Direction::Up
, return board moved up - When
Direction::None
, returns current board
Examples
Simple example:
use tfe::{Game, Direction}; let mut game = Game::new(); Game::execute(game.board, &Direction::Left);
The above Game::execute
takes the current u64
board and returns a new u64
board moved
in that direction. e.g.
// | 0 | 0 | 0 | 0 | | 0 | 0 | 0 | 0 | // | 0 | 0 | 0 | 0 | => | 0 | 0 | 0 | 0 | // | 0 | 0 | 4 | 4 | | 8 | 0 | 0 | 0 | // | 2 | 2 | 0 | 0 | | 4 | 0 | 0 | 0 |
pub fn transpose(board: u64) -> u64
[src]
pub fn transpose(board: u64) -> u64
Returns a transposed board where rows are transformed into columns and vice versa.
use tfe::Game; // | F | E | D | C | | F | B | 7 | 3 | // | B | A | 9 | 8 | => | E | A | 6 | 2 | // | 7 | 6 | 5 | 4 | | D | 9 | 5 | 1 | // | 3 | 2 | 1 | 0 | | C | 8 | 4 | 0 | assert_eq!(Game::transpose(0xFEDC_BA98_7654_3210), 0xFB73_EA62_D951_C840);
pub fn move_up(board: u64) -> u64
[src]
pub fn move_up(board: u64) -> u64
Returns a u64
board moved up.
This is the same as calling Game::execute(board, &Direction::Up)
;
Examples
use tfe::Game; let board = 0x0000_0000_0000_0011_u64; let result = Game::move_up(board); assert_eq!(result, 0x0011_0000_0000_0000);
pub fn move_down(board: u64) -> u64
[src]
pub fn move_down(board: u64) -> u64
Returns a u64
board moved down.
This is the same as calling Game::execute(board, &Direction::Down)
;
Examples
use tfe::Game; let board = 0x0011_0000_0000_0011_u64; let result = Game::move_down(board); assert_eq!(result, 0x0000_0000_0000_0022);
pub fn move_right(board: u64) -> u64
[src]
pub fn move_right(board: u64) -> u64
Returns a u64
board moved right.
This is the same as calling Game::execute(board, &Direction::Right)
;
Examples
use tfe::Game; let board = 0x0000_0000_0000_2211_u64; let result = Game::move_right(board); assert_eq!(result, 0x0000_0000_0000_0032);
pub fn move_left(board: u64) -> u64
[src]
pub fn move_left(board: u64) -> u64
Returns a u64
board moved left.
This is the same as calling Game::execute(board, &Direction::Left)
;
Examples
use tfe::Game; let board = 0x0000_0000_0000_2211_u64; let result = Game::move_left(board); assert_eq!(result, 0x0000_0000_0000_3200);
pub fn count_empty(board: u64) -> u32
[src]
pub fn count_empty(board: u64) -> u32
Returns the count of tiles with a value of 0
.
Examples
use tfe::Game; let board = 0x0000_0000_0000_2211_u64; let result = Game::count_empty(board); assert_eq!(result, 12);
pub fn table_helper(board: u64, table: &Vec<u64>) -> u64
[src]
pub fn table_helper(board: u64, table: &Vec<u64>) -> u64
Returns the result of 4 lookups in table
for each "row" in board
.
pub fn score(board: u64) -> u64
[src]
pub fn score(board: u64) -> u64
Returns the score of a given board
.
The score of a single tile is the sum of the tile value and all intermediate merged tiles.
pub fn column_from(board: u64) -> u64
[src]
pub fn column_from(board: u64) -> u64
Returns the 4th bit from each row in given board OR'd.
pub fn tile() -> u64
[src]
pub fn tile() -> u64
Returns a 2
with 90% chance and 4
with 10% chance.
pub fn spawn_tile(board: u64) -> u64
[src]
pub fn spawn_tile(board: u64) -> u64
Returns a 1
shifted to the position of any 0
bit in board
randomly.