pub trait GameState<P: Player, A: Action>: Copy + Debug + Display {
    fn actions<F>(&self, f: &mut F)
    where
        F: FnMut(A)
; fn make(&self, action: A) -> Self; fn gameover(&self) -> Option<GameResult>; fn player(&self) -> P; fn hash(&self) -> u64 { ... } fn custom_evaluation(&self) -> f32 { ... } }
Expand description

This trait describes the current state of the game from which to begin searching for the best move.

Required Methods

Iterate a list of legal actions for the current game state. Implementation should call “f” for each action.

Provide the next game state for the given action.

Indicate whether the current game state is in a game over condition. Return None when the game is still in play. Otherwise, return the result of the game from the current players perspective.

Indicate the side to play for the current game state (e.g. white, black).

Provided Methods

Optional: Provide a hash for the current game state. The hash is used to detect transpositions between game state when the “transposition” feature is activated. It must be sufficiently unique to avoid hash collisions with other game states. It is possible to have completely unique hashes for simple games like tic tac toe. An incremental hash may be a good approach in games with more complicated states like chess or checkers (see zobrist hashing).

Transposition is experimental. Care should be taken for games that prohibit move cycles like chess.

Optional: Override this method to provide a custom method for evaluating leaf nodes. The default algorithm for evaluating leaf nodes performs a random playout of the current game state using the GameState trait methods. This is a good starting point, but it should be possible to make a more efficient random playout function using the internals of the type that implements GameState.

Overriding this method allows for other ways of evaluating leaf nodes. The evaluation must provide an estimate of the win probability for the player of the current game state. The value returned should be a random variable between 0 and 1 that is correlated with the probablity the current player will win the game.

Use the “with_custom_evaluation” method in the MCTS builder to enable this feature.

Implementors