Trait alcibiades::MoveGenerator
[−]
[src]
pub trait MoveGenerator: Sized + Send + Clone + SetOption { type Evaluator: Evaluator; fn from_board(board: Board) -> Result<Self, IllegalBoard>; fn hash(&self) -> u64; fn board(&self) -> &Board; fn attacks_to(&self, square: Square) -> Bitboard; fn checkers(&self) -> Bitboard; fn evaluator(&self) -> &Self::Evaluator; fn generate_all<T: AddMove>(&self, moves: &mut T); fn generate_forcing<T: AddMove>(&self, generate_checks: bool, moves: &mut T); fn try_move_digest(&self, move_digest: MoveDigest) -> Option<Move>; fn null_move(&self) -> Move; fn do_move(&mut self, m: Move) -> Option<u64>; fn undo_move(&mut self, m: Move); fn evaluate_move(&self, m: Move) -> Value { ... } }
A trait for move generators.
A MoveGenerator
holds a chess position and can:
Generate all legal moves, or a subset of all legal moves in the current position.
Perform static exchange evaluation for the generated moves.
Play a selected move and take it back.
Provide a static evaluator bound to the current position.
Calculate Zobrist hashes.
Important note: MoveGenerator
is unaware of repeating
positions and rule-50.
Associated Types
Required Methods
fn from_board(board: Board) -> Result<Self, IllegalBoard>
Creates a new instance, consuming the supplied Board
instance.
Returns Err(IllegalBoard)
if the position is illegal.
fn hash(&self) -> u64
Returns the Zobrist hash value for the underlying Board
instance.
Zobrist hashing is a technique to transform a board position into a number of a fixed length, with an equal distribution over all possible numbers, invented by Albert Zobrist. The key property of this method is that two similar positions generate entirely different hash numbers.
Important note: This method will be relatively slow if the
implementation calculates the hash value "from
scratch". Inspect the implementation before using hash
in
time-critical paths. (See do_move
.)
fn board(&self) -> &Board
Returns a reference to the underlying Board
instance.
fn attacks_to(&self, square: Square) -> Bitboard
Returns a bitboard with all pieces that attack square
.
fn checkers(&self) -> Bitboard
Returns a bitboard with all enemy pieces that attack the king.
Important note: The bitboard of checkers is calculated on
the first call to checkers
, and is stored in case another
call is made before doing/undoing any moves. In that case
checkers
returns the saved bitboard instead of
re-calculating it, thus saving time.
fn evaluator(&self) -> &Self::Evaluator
Returns a reference to a static evaluator bound to the current position.
fn generate_all<T: AddMove>(&self, moves: &mut T)
Generates all legal moves, possibly including some pseudo-legal moves too.
The moves are added to moves
. All generated moves with
pieces other than the king will be legal. Some of the
generated king's moves may be illegal because the destination
square is under attack. This arrangement has two important
advantages:
do_move
can do its work without knowing the set of checkers and pinned pieces, so there is no need to keep those around.A beta cut-off may make the verification that king's destination square is not under attack unnecessary, thus saving time.
The initial move score for the generated moves is unspecified.
Note: A pseudo-legal move is a move that is otherwise legal, except it might leave the king in check.
fn generate_forcing<T: AddMove>(&self, generate_checks: bool, moves: &mut T)
Generates moves for the quiescence search.
The moves are added to moves
. This method always generates a
subset of the moves generated by generate_all
:
If the king is in check, all legal moves are included.
Captures and pawn promotions to queen are always included.
If
generate_checks
istrue
, moves that give check are included too. Discovered checks and checks given by castling can be omitted for speed.
fn try_move_digest(&self, move_digest: MoveDigest) -> Option<Move>
Checks if move_digest
represents a pseudo-legal move.
If a move m
exists that would be generated by
generate_all
if called for the current position on the
board, and for that move m.digest() == move_digest
, this
method will return Some(m)
. Otherwise it will return
None
. This is useful when playing moves from the
transposition table, without calling generate_all
.
fn null_move(&self) -> Move
Returns a null move.
"Null move" is a pseudo-move that changes only the side to move. It is sometimes useful to include a speculative null move in the search tree so as to achieve more aggressive pruning. Null moves are represented as king's moves for which the origin and destination squares are the same.
fn do_move(&mut self, m: Move) -> Option<u64>
Plays a move on the board.
It verifies if the move is legal. If the move is legal, the
board is updated and an u64
value is returned, which should
be XOR-ed with old board's hash value to obtain new board's
hash value. If the move is illegal, None
is returned without
updating the board. The move passed to this method must
have been generated by generate_all
, generate_forcing
,
try_move_digest
, or null_move
methods for the current
position on the board.
The moves generated by the null_move
method are
exceptions. For them do_move
will return None
if and only
if the king is in check.
fn undo_move(&mut self, m: Move)
Takes back last played move.
The move passed to this method must be the last move passed
to do_move
.
Provided Methods
fn evaluate_move(&self, m: Move) -> Value
Returns the likely evaluation change (material) to be lost or gained as a result of a given move.
This method performs static exchange evaluation (SEE). It examines the consequence of a series of exchanges on the destination square after a given move. A positive returned value indicates a "winning" move. For example, "PxQ" will always be a win, since the pawn side can choose to stop the exchange after its pawn is recaptured, and still be ahead. SEE is just an evaluation calculated without actually trying moves on the board, and therefore the returned value might be incorrect.
The move passed to this method must have been generated by
generate_all
, generate_forcing
, try_move_digest
, or
null_move
methods for the current position on the board.
Implementors
impl<T: Evaluator> MoveGenerator for StdMoveGenerator<T>