feat: rook slider movegen
This commit is contained in:
parent
23fec6541b
commit
2316e23530
25
src/lib.rs
25
src/lib.rs
@ -1,7 +1,7 @@
|
|||||||
#![deny(rust_2018_idioms)]
|
#![deny(rust_2018_idioms)]
|
||||||
|
|
||||||
use std::str::FromStr;
|
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
pub mod fen;
|
pub mod fen;
|
||||||
pub mod movegen;
|
pub mod movegen;
|
||||||
@ -157,15 +157,25 @@ impl From<Square> for usize {
|
|||||||
value.0
|
value.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! from_row_col_generic {
|
||||||
|
($T: ty, $r: ident, $c: ident) => {
|
||||||
|
if !(0..(BOARD_HEIGHT as $T)).contains(&$r) || !(0..(BOARD_WIDTH as $T)).contains(&$c) {
|
||||||
|
Err(SquareError::OutOfBounds)
|
||||||
|
} else {
|
||||||
|
let ret = (BOARD_WIDTH as $T) * $r + $c;
|
||||||
|
ret.try_into()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
impl Square {
|
impl Square {
|
||||||
fn from_row_col(r: usize, c: usize) -> Result<Self, SquareError> {
|
fn from_row_col(r: usize, c: usize) -> Result<Self, SquareError> {
|
||||||
//! Get index of square based on row and column.
|
//! Get index of square based on row and column.
|
||||||
let ret = BOARD_WIDTH * r + c;
|
from_row_col_generic!(usize, r, c)
|
||||||
ret.try_into()
|
|
||||||
}
|
}
|
||||||
fn from_row_col_signed(r: isize, c: isize) -> Result<Self, SquareError> {
|
fn from_row_col_signed(r: isize, c: isize) -> Result<Self, SquareError> {
|
||||||
let ret = (BOARD_WIDTH as isize) * r + c;
|
from_row_col_generic!(isize, r, c)
|
||||||
ret.try_into()
|
|
||||||
}
|
}
|
||||||
fn to_row_col(self) -> (usize, usize) {
|
fn to_row_col(self) -> (usize, usize) {
|
||||||
//! Get row, column from index
|
//! Get row, column from index
|
||||||
@ -469,7 +479,10 @@ mod tests {
|
|||||||
macro_rules! try_type {
|
macro_rules! try_type {
|
||||||
($T: ty) => {
|
($T: ty) => {
|
||||||
if let Ok(conv) = <$T>::try_from(tc) {
|
if let Ok(conv) = <$T>::try_from(tc) {
|
||||||
assert!(matches!(Square::try_from(conv), Err(SquareError::OutOfBounds)))
|
assert!(matches!(
|
||||||
|
Square::try_from(conv),
|
||||||
|
Err(SquareError::OutOfBounds)
|
||||||
|
))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ impl From<PromotePiece> for Piece {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
|
||||||
enum MoveType {
|
enum MoveType {
|
||||||
/// Pawn promotes to another piece.
|
/// Pawn promotes to another piece.
|
||||||
Promotion(PromotePiece),
|
Promotion(PromotePiece),
|
||||||
@ -54,7 +54,7 @@ enum MoveType {
|
|||||||
/// Pseudo-legal move.
|
/// Pseudo-legal move.
|
||||||
///
|
///
|
||||||
/// No checking is done when constructing this.
|
/// No checking is done when constructing this.
|
||||||
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
|
||||||
pub struct Move {
|
pub struct Move {
|
||||||
src: Square,
|
src: Square,
|
||||||
dest: Square,
|
dest: Square,
|
||||||
@ -397,20 +397,33 @@ mod tests {
|
|||||||
/// Test that slider pieces can move and capture.
|
/// Test that slider pieces can move and capture.
|
||||||
#[test]
|
#[test]
|
||||||
fn test_slider_movegen() {
|
fn test_slider_movegen() {
|
||||||
let test_cases = [(
|
let test_cases = [
|
||||||
// start position
|
(
|
||||||
"8/8/8/8/8/8/8/R7 w - - 0 1",
|
// start position
|
||||||
// expected moves
|
"8/8/8/8/8/8/8/R7 w - - 0 1",
|
||||||
vec![(
|
// expected moves
|
||||||
// source piece
|
vec![(
|
||||||
"a1",
|
// source piece
|
||||||
// destination squares
|
"a1",
|
||||||
vec![
|
// destination squares
|
||||||
"a2", "a3", "a4", "a5", "a6", "a7", "a8", "b1", "c1", "d1", "e1", "f1", "g1",
|
vec![
|
||||||
"h1",
|
"a2", "a3", "a4", "a5", "a6", "a7", "a8", "b1", "c1", "d1", "e1", "f1",
|
||||||
],
|
"g1", "h1",
|
||||||
)],
|
],
|
||||||
)];
|
MoveType::Normal,
|
||||||
|
)],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"8/1p6/8/1R2p3/1p6/8/8/8 w - - 0 1",
|
||||||
|
vec![(
|
||||||
|
"b5",
|
||||||
|
vec![
|
||||||
|
"b6", "b7", "b4", "a5", "c5", "d5", "e5",
|
||||||
|
],
|
||||||
|
MoveType::Normal,
|
||||||
|
)],
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
for (fen, expected) in test_cases {
|
for (fen, expected) in test_cases {
|
||||||
let board = BoardState::from_fen(fen).unwrap();
|
let board = BoardState::from_fen(fen).unwrap();
|
||||||
@ -419,7 +432,27 @@ mod tests {
|
|||||||
moves.sort_unstable();
|
moves.sort_unstable();
|
||||||
let moves = moves;
|
let moves = moves;
|
||||||
|
|
||||||
let expected_moves = expected.iter().map(|(src, dests)| {});
|
let mut expected_moves = expected
|
||||||
|
.iter()
|
||||||
|
.map(|(src, dests, move_type)| {
|
||||||
|
let src = src.parse::<Square>().unwrap();
|
||||||
|
let dests = dests
|
||||||
|
.iter()
|
||||||
|
.map(|x| x.parse::<Square>())
|
||||||
|
.map(|x| x.unwrap());
|
||||||
|
dests.map(move |dest| Move {
|
||||||
|
src,
|
||||||
|
dest,
|
||||||
|
move_type: *move_type,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.flatten()
|
||||||
|
.collect::<Vec<Move>>();
|
||||||
|
|
||||||
|
expected_moves.sort_unstable();
|
||||||
|
let expected_moves = expected_moves;
|
||||||
|
|
||||||
|
assert_eq!(moves, expected_moves);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user