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§
type Ply: Ply
type Resolution: Resolution
Required Methods§
Sourcefn get_ply_count(&self) -> usize
fn get_ply_count(&self) -> usize
Returns the number of plies that have passed in the game.
Sourcefn execute_ply(&mut self, ply: Option<&Self::Ply>) -> Result<(), String>
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.
Sourcefn revert_ply(&mut self, ply: Option<&Self::Ply>) -> Result<(), String>
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.
Sourcefn check_resolution(&self) -> Option<Self::Resolution>
fn check_resolution(&self) -> Option<Self::Resolution>
Returns None
if the game has not reached a conclusion.
Provided Methods§
Sourcefn null_move_allowed(&self) -> bool
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
.
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.