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

Methods

impl Game
[src]

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);

Like new but takes a closure that accepts two parameters and returns a Direction. The parameters passed to the closure:

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

The failed: &Vec<Direction> will contain at most 3 items, when the 4th item is added the game ends automatically without calling the closure again.

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

Examples

Simple example:

use tfe::{Game, Direction};

let board = 0x0000_0000_0022_1100;
let moved = Game::execute(board, &Direction::Left);

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

assert_eq!(board, 0x0000_0000_0022_1100);
assert_eq!(moved, 0x0000_0000_3000_2000);

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);

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);

// | 0 | 0 | 0 | 0 |      | 0 | 0 | 1 | 1 |
// | 0 | 0 | 0 | 0 |  =>  | 0 | 0 | 0 | 0 |
// | 0 | 0 | 0 | 0 |      | 0 | 0 | 0 | 0 |
// | 0 | 0 | 1 | 1 |      | 0 | 0 | 0 | 0 |

assert_eq!(result, 0x0011_0000_0000_0000);

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);

// | 0 | 0 | 1 | 1 |      | 0 | 0 | 0 | 0 |
// | 0 | 0 | 0 | 0 |  =>  | 0 | 0 | 0 | 0 |
// | 0 | 0 | 0 | 0 |      | 0 | 0 | 0 | 0 |
// | 0 | 0 | 1 | 1 |      | 0 | 0 | 2 | 2 |

assert_eq!(result, 0x0000_0000_0000_0022);

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);

// | 0 | 0 | 0 | 0 |      | 0 | 0 | 0 | 0 |
// | 0 | 0 | 0 | 0 |  =>  | 0 | 0 | 0 | 0 |
// | 0 | 0 | 0 | 0 |      | 0 | 0 | 0 | 0 |
// | 2 | 2 | 1 | 1 |      | 0 | 0 | 3 | 2 |

assert_eq!(result, 0x0000_0000_0000_0032);

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);

// | 0 | 0 | 0 | 0 |      | 0 | 0 | 0 | 0 |
// | 0 | 0 | 0 | 0 |  =>  | 0 | 0 | 0 | 0 |
// | 0 | 0 | 0 | 0 |      | 0 | 0 | 0 | 0 |
// | 2 | 2 | 1 | 1 |      | 3 | 2 | 0 | 0 |

assert_eq!(result, 0x0000_0000_0000_3200);

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);

Returns the sum of 4 lookups in table for each "row" in board.

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.

Returns a 2 with 90% chance and 4 with 10% chance.

Returns a 1 shifted to the position of any 0 bit in board randomly.

Auto Trait Implementations

impl Send for Game

impl Sync for Game