[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
|
@ -12,3 +12,8 @@ pub use colors::Color;
|
|||
pub use coordinates::{Direction, File, Rank, Square, Wing};
|
||||
pub use pieces::Piece;
|
||||
pub use shapes::{Shape, Slider};
|
||||
|
||||
pub mod errors {
|
||||
pub use crate::coordinates::ParseSquareError;
|
||||
pub use crate::shapes::ParseShapeError;
|
||||
}
|
||||
|
|
|
@ -99,9 +99,8 @@ mod tests {
|
|||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn shape_try_from() {
|
||||
assert_eq!(Shape::try_from('p'), Ok(Shape::Pawn));
|
||||
assert_eq!(Shape::try_from("p"), Ok(Shape::Pawn));
|
||||
fn parse_shape() {
|
||||
assert_eq!("p".parse(), Ok(Shape::Pawn));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
use std::{array, slice};
|
||||
use std::{array, fmt, slice, str::FromStr};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
||||
|
@ -27,6 +27,8 @@ impl Shape {
|
|||
Shape::King,
|
||||
];
|
||||
|
||||
const PROMOTABLE_SHAPES: [Shape; 4] = [Shape::Queen, Shape::Rook, Shape::Bishop, Shape::Knight];
|
||||
|
||||
pub fn iter() -> slice::Iter<'static, Self> {
|
||||
Shape::ALL.iter()
|
||||
}
|
||||
|
@ -38,10 +40,7 @@ impl Shape {
|
|||
|
||||
/// An iterator over the shapes that a pawn can promote to
|
||||
pub fn promotable() -> slice::Iter<'static, Shape> {
|
||||
const PROMOTABLE_SHAPES: [Shape; 4] =
|
||||
[Shape::Queen, Shape::Rook, Shape::Bishop, Shape::Knight];
|
||||
|
||||
PROMOTABLE_SHAPES.iter()
|
||||
Self::PROMOTABLE_SHAPES.iter()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
|
@ -67,6 +66,11 @@ impl Shape {
|
|||
Shape::King => "king",
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_promotable(&self) -> bool {
|
||||
Self::PROMOTABLE_SHAPES.contains(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
||||
|
@ -121,32 +125,24 @@ impl TryFrom<char> for Shape {
|
|||
|
||||
#[derive(Clone, Copy, Debug, Error, Eq, PartialEq)]
|
||||
#[error("no matching piece shape for string")]
|
||||
pub struct ShapeFromStrError;
|
||||
pub struct ParseShapeError;
|
||||
|
||||
impl TryFrom<&str> for Shape {
|
||||
type Error = ShapeFromStrError;
|
||||
impl FromStr for Shape {
|
||||
type Err = ParseShapeError;
|
||||
|
||||
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
||||
match value.to_lowercase().as_str() {
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.to_lowercase().as_str() {
|
||||
"p" | "pawn" => Ok(Shape::Pawn),
|
||||
"n" | "knight" => Ok(Shape::Knight),
|
||||
"b" | "bishop" => Ok(Shape::Bishop),
|
||||
"r" | "rook" => Ok(Shape::Rook),
|
||||
"q" | "queen" => Ok(Shape::Queen),
|
||||
"k" | "king" => Ok(Shape::King),
|
||||
_ => Err(ShapeFromStrError),
|
||||
_ => Err(ParseShapeError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for Shape {
|
||||
type Err = ShapeFromStrError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Self::try_from(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Shape> for char {
|
||||
fn from(shape: &Shape) -> char {
|
||||
char::from(*shape)
|
||||
|
@ -159,7 +155,7 @@ impl From<Shape> for char {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Shape {
|
||||
impl fmt::Display for Shape {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let self_char: char = self.into();
|
||||
write!(f, "{self_char}")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue