tfe/
lib.rs

1//! A 2048 implementation that uses bit-shifting and a pre-computed move table
2//! this implementation is designed to provide low overhead when testing an algorithm on a large
3//! amount of games. On a mid-2015 MBP Retina (2.5GHz i7) 10,000,000 games take about 80 seconds to
4//! complete running on 8 threads (1,250,000 games per thread) by executing random moves, avg score ~2k.
5//!
6//! The board itself is encoded as a `u64`. This means that each tile has 4 bits (64 / 16 = 4) to store its
7//! value. Since the maximum value of setting all four bits to 1 is `15` we cannot use it to
8//! display the value directly. Instead we use these 4 bits as the power value: `2 << 15 = 65536`, `2
9//! << 14 = 32768`, `2 << 13 = 16384`, `2 << 12 = 8192`, etc...
10//!
11//! A simple way to play the game automatically is to use `tfe::Game::play`:
12//!
13//! ```
14//! extern crate tfe;
15//! use tfe::{Game, Direction};
16//!
17//! let game = Game::play(|board, failed| Direction::sample_without(failed));
18//! println!("score: {:<6} board hex: {:016x}", Game::score(game.board), game.board);
19//! ```
20//!
21//! The `play` method takes a closure that accepts a `board: u64` and `failed: &Vec<Direction>` as
22//! parameters and returns a `Direction` to move in.
23//!
24//! The game will terminate automatically if each distinct move has been attempted and failed
25//! without any successfull move in between the failed moves.
26//!
27//! ---
28//!
29//! references:
30//!  - https://github.com/nneonneo/2048-ai/blob/master/2048.h
31//!  - https://github.com/nneonneo/2048-ai/blob/master/2048.cpp
32//!  - https://stackoverflow.com/questions/22342854/what-is-the-optimal-algorithm-for-the-game-2048
33
34#[allow(dead_code)]
35
36#[macro_use]
37extern crate lazy_static;
38extern crate rand;
39
40mod direction;
41mod game;
42
43pub use direction::Direction;
44pub use game::Game;
45