[core, moves, position] Implement parsing long algebraic moves
UCI uses a move format it calls "long algebraic". They look like either "e2e4" for a regular move, or "h7h8q" for a promotion. Implement parsing these move strings as a two step process. First define an AlgebraicMoveComponents struct in the moves crate that implements FromStr. This struct reads out an origin square, a target square, and an optional promotion shape from a string. Then, implement a pair of methods on Position that take the move components struct and return a fully encoded Move struct with them. This process is required because the algebraic string is not enough by itself to know what kind of move was made. The current position is required to understand that. Implement Shape::is_promotable(). Add a NULL move to the Move struct. I'm not sure what this is used for yet, but the UCI spec specifically calls out a string that encodes a null move, so I added it. It may end up being unused! Do a little bit of cleanup in the core crate as well. Use deeper imports (import std::fmt instead of requring the fully qualified type path) and remove some unnecessary From implementations. This commit is also the first instance (I think) of defining an errors module in lib.rs for the core crate that holds the various error types the crate exports.
This commit is contained in:
parent
fb182e7ac0
commit
3951af76cb
8 changed files with 232 additions and 24 deletions
|
@ -11,6 +11,7 @@ use chessfriend_board::{
|
|||
};
|
||||
use chessfriend_core::{Color, Piece, Shape, Square};
|
||||
use chessfriend_moves::{
|
||||
algebraic::AlgebraicMoveComponents,
|
||||
generators::{
|
||||
AllPiecesMoveGenerator, BishopMoveGenerator, KingMoveGenerator, KnightMoveGenerator,
|
||||
PawnMoveGenerator, QueenMoveGenerator, RookMoveGenerator,
|
||||
|
@ -222,6 +223,68 @@ impl Position {
|
|||
|
||||
unmake_result
|
||||
}
|
||||
|
||||
/// Build a move given its origin, target, and possible promotion. Perform
|
||||
/// some minimal validation. If a move cannot be
|
||||
#[must_use]
|
||||
pub fn move_from_algebraic_components(
|
||||
&self,
|
||||
components: AlgebraicMoveComponents,
|
||||
) -> Option<Move> {
|
||||
match components {
|
||||
AlgebraicMoveComponents::Null => Some(Move::null()),
|
||||
AlgebraicMoveComponents::Regular {
|
||||
origin,
|
||||
target,
|
||||
promotion,
|
||||
} => self.move_from_origin_target(origin, target, promotion),
|
||||
}
|
||||
}
|
||||
|
||||
fn move_from_origin_target(
|
||||
&self,
|
||||
origin: Square,
|
||||
target: Square,
|
||||
promotion: Option<Shape>,
|
||||
) -> Option<Move> {
|
||||
let piece = self.get_piece(origin)?;
|
||||
|
||||
let color = piece.color;
|
||||
|
||||
// Pawn and King are the two most interesting shapes here, because of en
|
||||
// passant, castling and so on. So, let the move generators do their
|
||||
// thing and find the move that fits the parameters. For the rest of the
|
||||
// pieces, do something a little more streamlined.
|
||||
|
||||
match piece.shape {
|
||||
Shape::Pawn => PawnMoveGenerator::new(&self.board, None)
|
||||
.find(|ply| {
|
||||
ply.origin() == origin
|
||||
&& ply.target() == target
|
||||
&& ply.promotion_shape() == promotion
|
||||
})
|
||||
.map(std::convert::Into::into),
|
||||
Shape::King => KingMoveGenerator::new(&self.board, None)
|
||||
.find(|ply| ply.origin() == origin && ply.target() == target)
|
||||
.map(std::convert::Into::into),
|
||||
_ => {
|
||||
if color != self.board.active_color() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let target_bitboard: BitBoard = target.into();
|
||||
if !(self.movement(origin) & target_bitboard).is_populated() {
|
||||
return None;
|
||||
}
|
||||
|
||||
if self.get_piece(target).is_some() {
|
||||
return Some(Move::capture(origin, target));
|
||||
}
|
||||
|
||||
Some(Move::quiet(origin, target))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Position {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue