diff --git a/src/fen.rs b/src/fen.rs index 9b8d6af..6742317 100644 --- a/src/fen.rs +++ b/src/fen.rs @@ -10,7 +10,7 @@ pub trait FromFen { } pub trait ToFen { - fn to_fen(self) -> String; + fn to_fen(&self) -> String; } /// FEN parsing error, with index of issue if applicable. @@ -234,7 +234,7 @@ impl FromFen for BoardState { } impl ToFen for BoardState { - fn to_fen(self) -> String { + fn to_fen(&self) -> String { let pieces_str = (0..BOARD_HEIGHT) .rev() .map(|row| { diff --git a/src/movegen.rs b/src/movegen.rs index 95fd0ed..924d6e8 100644 --- a/src/movegen.rs +++ b/src/movegen.rs @@ -5,6 +5,7 @@ use crate::{BoardState, Color, Square, BOARD_WIDTH}; use std::rc::Rc; /// Game tree node. +#[derive(Clone, Debug)] struct Node { /// Immutable position data. pos: BoardState, @@ -113,7 +114,7 @@ mod tests { use super::*; 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. #[test] @@ -176,8 +177,8 @@ mod tests { ), ]; + // make move let mut node = Node::default(); - for (src, dest, expect_fen) in moves { let idx_src = Square::from_algebraic(src.to_string()).unwrap(); let idx_dest = Square::from_algebraic(dest.to_string()).unwrap(); @@ -188,5 +189,15 @@ mod tests { node = mv.make(node); 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(); + } + } } }