#game-ai #algorithm #open-source #board-game #game-logic

tools-2048

A Rust crate that provides the core logic of the popular game 2048, along with a basic AI to play the game

7 releases

0.3.2 Jan 10, 2024
0.3.1 Jan 10, 2024
0.2.0 May 13, 2023
0.1.2 Mar 25, 2023

#134 in Games

Download history 4/week @ 2023-12-28 19/week @ 2024-01-04 19/week @ 2024-01-11 18/week @ 2024-02-22 6/week @ 2024-02-29 2/week @ 2024-03-07 8/week @ 2024-03-14 4/week @ 2024-03-28 4/week @ 2024-04-04 58/week @ 2024-04-11

66 downloads per month

MIT license

26KB
440 lines

tools-2048-rs

A Rust crate that provides the core logic of the popular game 2048, along with a basic AI to play the game. Arbitrary board sizes are supported with the minimum being 4x4.


Documentation

Crate

Example

use tools_2048::{Game, GameMove, GameState, GameResult};

// create a new game
let mut game = Game::new(4).unwrap();

// make a move
game.make_move(GameMove::Left);
game.make_move(GameMove::Right);
game.make_move(GameMove::Up);
game.make_move(GameMove::Down);

// find the best move and make it
game.make_move(game.find_best_move(10_000).unwrap());

assert_eq!(game.state(), GameState::InProgress);  // the game should still be in progress
assert_eq!(game.result(), GameResult::Pending);  // the result shouldn't be decided yet

The AI is based on the Monte Carlo algorithm, and uses parallelism to speed up the process. At depth of 10 000, AI achieves 1024 tile ~100% of the time, 2048 tile ~96% of the time, and 4096 tile ~65% of the time.

Dependencies

~335KB