feat: unmake move
This commit is contained in:
parent
c29a7ff789
commit
ca0c17cbbe
@ -10,7 +10,7 @@ pub trait FromFen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait ToFen {
|
pub trait ToFen {
|
||||||
fn to_fen(self) -> String;
|
fn to_fen(&self) -> String;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// FEN parsing error, with index of issue if applicable.
|
/// FEN parsing error, with index of issue if applicable.
|
||||||
@ -234,7 +234,7 @@ impl FromFen for BoardState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ToFen for BoardState {
|
impl ToFen for BoardState {
|
||||||
fn to_fen(self) -> String {
|
fn to_fen(&self) -> String {
|
||||||
let pieces_str = (0..BOARD_HEIGHT)
|
let pieces_str = (0..BOARD_HEIGHT)
|
||||||
.rev()
|
.rev()
|
||||||
.map(|row| {
|
.map(|row| {
|
||||||
|
@ -5,6 +5,7 @@ use crate::{BoardState, Color, Square, BOARD_WIDTH};
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
/// Game tree node.
|
/// Game tree node.
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
struct Node {
|
struct Node {
|
||||||
/// Immutable position data.
|
/// Immutable position data.
|
||||||
pos: BoardState,
|
pos: BoardState,
|
||||||
@ -113,7 +114,7 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::fen::START_POSITION;
|
use crate::fen::START_POSITION;
|
||||||
|
|
||||||
/// Test that make move works for regular piece pushes and captures.
|
/// Test that make move and unmake move for simple piece pushes/captures.
|
||||||
///
|
///
|
||||||
/// Also tests en passant target square.
|
/// Also tests en passant target square.
|
||||||
#[test]
|
#[test]
|
||||||
@ -176,8 +177,8 @@ mod tests {
|
|||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// make move
|
||||||
let mut node = Node::default();
|
let mut node = Node::default();
|
||||||
|
|
||||||
for (src, dest, expect_fen) in moves {
|
for (src, dest, expect_fen) in moves {
|
||||||
let idx_src = Square::from_algebraic(src.to_string()).unwrap();
|
let idx_src = Square::from_algebraic(src.to_string()).unwrap();
|
||||||
let idx_dest = Square::from_algebraic(dest.to_string()).unwrap();
|
let idx_dest = Square::from_algebraic(dest.to_string()).unwrap();
|
||||||
@ -188,5 +189,15 @@ mod tests {
|
|||||||
node = mv.make(node);
|
node = mv.make(node);
|
||||||
assert_eq!(node.pos.to_fen(), expect_fen.to_string())
|
assert_eq!(node.pos.to_fen(), expect_fen.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// unmake move
|
||||||
|
let mut cur_node = Rc::new(node.clone());
|
||||||
|
for (_, _, expect_fen) in moves.iter().rev().chain([("", "", START_POSITION)].iter()) {
|
||||||
|
println!("{}", expect_fen);
|
||||||
|
assert_eq!(*cur_node.pos.to_fen(), expect_fen.to_string());
|
||||||
|
if *expect_fen != START_POSITION {
|
||||||
|
cur_node = cur_node.prev.clone().unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user