diff --git a/src/logic/future_moves.rs b/src/logic/future_moves.rs index 7a81a78..d5103d1 100644 --- a/src/logic/future_moves.rs +++ b/src/logic/future_moves.rs @@ -293,7 +293,7 @@ impl FutureMoves { } } - fn get_board_from_idx(&self, idx: usize) -> Option { + fn move_history(&self, idx: usize) -> Option, Piece)>> { if let Some(root) = self.current_root { let mut hist = Vec::new(); @@ -307,11 +307,24 @@ impl FutureMoves { hist.push((n.coord, n.color)); current = n.parent; } + hist.reverse(); - let mut board = self.arena[root].board; - for (m, c) in hist.into_iter().rev() { + if current != self.current_root { + return None; + } + + Some(hist) + } else { + None + } + } + + fn get_board_from_idx(&self, idx: usize) -> Option { + if let Some(hist) = self.move_history(idx) { + let mut board = self.arena[self.current_root.unwrap()].board; + for (m, c) in hist { if let Some(m) = m { - let _ = board.place(m, c); + board.place(m, c).expect("move would not propegate"); } } Some(board) @@ -792,14 +805,45 @@ mod tests { let mut futm = FutureMoves::new(Piece::White, FUTURE_MOVES_CONFIG); let mut b = Board::new().starting_pos(); - let _ = b.place((2, 3).into(), Piece::Black); + futm.arena + .push(Move::new(None, b, Piece::Black, Piece::White)); + futm.update_root_idx_raw(0); + + b.place((4, 2).into(), Piece::White).unwrap(); futm.arena.push(Move::new( - Some((2, 3).into()), + Some((4, 2).into()), + b, + Piece::White, + Piece::White, + )); + + futm.set_parent_child(0, 1); + + assert_eq!( + futm.move_history(1), + Some(vec![(Some((4, 2).into()), Piece::White)]), + ); + assert_eq!(futm.get_board_from_idx(1), Some(b)); + + b.place((5, 4).into(), Piece::Black).unwrap(); + + futm.arena.push(Move::new( + Some((5, 4).into()), b, Piece::Black, Piece::White, )); + + futm.set_parent_child(1, 2); + assert_eq!( + futm.move_history(2), + Some(vec![ + (Some((4, 2).into()), Piece::White), + (Some((5, 4).into()), Piece::Black) + ]), + ); + assert_eq!(futm.get_board_from_idx(2), Some(b)); } // I can't actually reproduce the issue I got, this is my best attempt diff --git a/src/main.rs b/src/main.rs index 17317ad..6a71603 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,8 +12,8 @@ pub mod repr; // TODO! make this agent configuration a config option via `clap-rs` fn main() { - elo::run(); - return; + // elo::run(); + // return; let player1 = complexagent::ComplexAgent::new( Piece::Black, FutureMoveConfig {