Trait zero_sum::State
[−]
[src]
pub trait State<P, R>: Clone + Display + Eq + Hash + PartialEq where P: Ply, R: Resolution { fn execute_ply_preallocated(&self, ply: &P, next: &mut Self) -> Result<(), String>; fn check_resolution(&self) -> Option<R>; fn execute_ply(&self, ply: &P) -> Result<Self, String> { ... } fn execute_plies(&self, plies: &[P]) -> Result<Self, String> { ... } }
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<Move, End> for Board { fn execute_ply_preallocated(&self, ply: &Move, next: &mut Board) -> 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 Methods
fn execute_ply_preallocated(&self, ply: &P, next: &mut Self) -> Result<(), String>
Executes a ply on the state, storing the resultant state in the preallocated next
.
It is recommended to implement Clone
on the State
implementor manually,
to take advantage of Clone
's clone_from
method in order to avoid costly
allocations during a speed-critical search.
fn check_resolution(&self) -> Option<R>
Returns None
if the game has not reached a conclusion.
Provided Methods
fn execute_ply(&self, ply: &P) -> Result<Self, String>
Clones the state and then calls execute_ply_preallocated
.
fn execute_plies(&self, plies: &[P]) -> Result<Self, String>
Executes each ply in plies
on the result of the previous ply.