Trait State

Source
pub trait State:
    Clone
    + Display
    + Eq
    + Hash
    + PartialEq {
    type Ply: Ply;
    type Resolution: Resolution;

    // Required methods
    fn get_ply_count(&self) -> usize;
    fn execute_ply(&mut self, ply: Option<&Self::Ply>) -> Result<(), String>;
    fn revert_ply(&mut self, ply: Option<&Self::Ply>) -> Result<(), String>;
    fn check_resolution(&self) -> Option<Self::Resolution>;

    // Provided methods
    fn null_move_allowed(&self) -> bool { ... }
    fn execute_plies(&mut self, plies: &[Self::Ply]) -> Result<(), String> { ... }
}
Expand description

The state of the game.

This should represent everything that makes up a single moment of the game, i.e. in chess, this would be the board and all of its pieces, the turn number, etc.

However, if the implementor of this trait does store data that changes or increments every turn, like a turn number, it is recommended to implement Hash manually and to exclude that data from the hash, perhaps simplifying it into the next player to move. This is in order to allow the struct to benefit from certain search optimization techniques – primarily a transposition table.

§Example

For tic-tac-toe, we might have:

enum Mark { X, O }
struct Move { /* ... */ }
enum End { /* ... */ }

struct Board([Option<Mark>; 9], u8); // The board and the turn number

impl State for Board {
    type Ply = Move;
    type Resolution = End;

    fn get_ply_count(&self) -> usize {
        self.1 as usize
    }

    fn execute_ply(&mut self, ply: Option<&Move>) -> Result<(), String> {
        // ...
    }

    fn revert_ply(&mut self, ply: Option<&Move>) -> Result<(), String> {
        // ...
    }

    fn check_resolution(&self) -> Option<End> {
        // ...
    }
}

impl Hash for Board {
    fn hash<H>(&self, state: &mut H) where H: Hasher {
        self.0.hash(state);
        if self.1 % 2 == 0 {
            Mark::X.hash(state);
        } else {
            Mark::O.hash(state);
        }
    }
}

Required Associated Types§

Required Methods§

Source

fn get_ply_count(&self) -> usize

Returns the number of plies that have passed in the game.

Source

fn execute_ply(&mut self, ply: Option<&Self::Ply>) -> Result<(), String>

Executes the given ply on this state. Pass None to execute a null move.

Source

fn revert_ply(&mut self, ply: Option<&Self::Ply>) -> Result<(), String>

Reverts the given ply from the state. Pass None to revert a null move.

Source

fn check_resolution(&self) -> Option<Self::Resolution>

Returns None if the game has not reached a conclusion.

Provided Methods§

Source

fn null_move_allowed(&self) -> bool

Returns true if the state is in a good place to allow the null move search optimization. This is optional to implement, returning a default of false.

Source

fn execute_plies(&mut self, plies: &[Self::Ply]) -> Result<(), String>

Executes each ply in plies on the result of the previous ply.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§