Trait rival::game::Game [−][src]
pub trait Game<const N: usize> { type Move; const DEPTH: u32; fn turn(&self) -> usize; fn value(&self) -> [i32; N]; fn moves(&self) -> Vec<Self::Move>; fn perform(&mut self, m: &Self::Move); fn revert(&mut self, m: &Self::Move); fn best_move(&mut self) -> Option<Self::Move> { ... } fn quiet(&self) -> bool { ... } }
Expand description
This trait can be implemented on any type that represents the state of a game. Doing so requires
implementations that inform the trait of which moves each player can Game::perform
and how
well each player is doing in any particular state. In return, this trait will provide a method
that calculates the Game::best_move
the current player can make.
The const generic in Game<N>
represents the number of players in this game.
Associated Types
The type of the moves that players can Game::perform
.
Associated Constants
The number of moves to look ahead when searching the Game::best_move
.
Required methods
Evaluates the game’s state and returns the relative value for each of the players,
corresponding to their index according to Game::turn
.
For two-player games, the sum of the values is usually 0: if the second player of a game of tic-tac-toe is the winner, the result of this method could be [-1, 1].
Returns a Vec
of moves that the current player (see Game::turn
) can
Game::perform
.
Performs the given Game::Move
, changing the game’s state.
Reverts the given Game::Move
. This is the dual of Game::perform
: Game::revert
after Game::perform
with identical moves should have no effect on game’s state.
Provided methods
Returns the best Game::Move
for the current player (see Game::turn
), according to
the calculated Game::value
within the next Game::DEPTH
moves, or None
if the
current player cannot perform any move.
Examples
let mut game = TicTacToe::new(); // Play the entire game while !game.moves().is_empty() { let m = game.best_move()?; game.perform(&m); } // Neither of the players won assert_eq!(game.value(), [0, 0]);
Indicates whether the current evaluation of the game state will not change much within subsequent moves.
If the balance of the game is likely to shift drastically within the next move – such as
when a queen in chess is under attack – this method should return false
, which will
trigger the algorithm to continue searching even if the search Game::DEPTH
is exceeded.
This can improve the performance of the computer player, but it is not necessary to
implement this method.
Termination
Keep in mind that searching the Game::best_move
might take forever if this function
returns false
too often.