game-tree-search
Adversarial game-tree search in Rust for computer opponents in board, card and turn-based games: choosing a move when an opponent chooses the next one.
Who this is for
- Game developers writing a computer opponent for a two-sided, turn-based game: board games, card games, tactics and combat systems. The side to move belongs to the position, so turn orders by initiative or speed, where one side may act several times in a row, work as they are.
- Opponents that explain themselves or scale in difficulty: a search returns the chosen move with its score, the expected line of play, and the alternatives with their scores or bounds, so a game can show what its opponent considered or pick a weaker move on purpose.
- Games with a frame or time limit: a node or time budget stops iterative deepening with
the best move found so far, and a reusable searcher allocates nothing per position once
warm. The crate is
no_std, so it also runs on consoles, handhelds and in WebAssembly.
It is not for single-agent problems: for pathfinding, see heuristic-search; for puzzles, including checking that a puzzle has a unique solution and grading it, see constraint-search. Games with hidden information, such as most trick-taking card games, search sampled deals of the hidden cards, one deal at a time; constraint-search draws deals consistent with what a player has seen, and tells which cards are certain or likely.
Scope
- Minimax and negamax
- Alpha-beta pruning with move ordering, principal variation search (PVS/NegaScout) and MTD(f)
- Iterative deepening with transposition tables, aspiration windows and quiescence search
- Expectimax for games with chance nodes
- Monte Carlo tree search with UCT
Single-agent search, including iterative deepening (IDDFS, IDA*), lives in heuristic-search.
Usage
A game describes its positions, the moves available in them, which side is to move and how good a position is for the maximizing side. The side to move belongs to the position, so one side may move several times in a row. Here, one-pile Nim: players take one or two stones, and whoever cannot move has lost.
use ;
/// One-pile Nim; a position is the number of stones left and the side to move.
;
// Four stones: taking one leaves three, a lost pile for the opponent.
let result = minimax;
assert_eq!;
assert_eq!;
The crate is no_std and needs alloc. Its required dependencies are
search-core, for the shared Budget trait, and
libm, for the square roots and logarithms of Monte Carlo
tree search.
Many searches at once
Game AI often runs a search per unit or agent, every frame. Keep one Searcher (or Mcts)
per thread or agent and pass the game by reference: every search takes &G and never
mutates it, so a Sync game is shared between threads as it is, and a searcher is Send
whenever its moves and scores are. A warm searcher reuses its buffers, so a search allocates
only the result it returns. Give each agent its own transposition table, or none; with the
parallel feature, a SharedTable or a lock-free PackedTable serves several threads at once.
use ;
/// The best move from each agent's position, with one thread and one searcher per agent.
Features
hashbrown:MapTable, a transposition table backed by a hashbrown hash map. Without it, the fixed-sizeSlotTableis available.rand_core:RandCore, so Monte Carlo tree search can draw from any rand_core 0.10 generator instead of its built-in, seededSplitMix64.parallel: searches onstdthreads.Mcts::search_parallelruns root-parallel Monte Carlo tree search, one tree per thread with their root statistics added up;deepen_sharedruns Lazy SMP, several deepening threads sharing one table: aSharedTable, which takes any move and score, or a lock-freePackedTablefor moves and scores that pack into 48 bits together.
Every feature combination except parallel stays no_std; parallel needs std for its
threads.
Minimum supported Rust version
Rust 1.85 (edition 2024). The MSRV applies to the library; tests may need a newer toolchain. It may be raised in a minor release when a dependency or a language feature calls for it.
Contributing
See CONTRIBUTING.md.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.