From d4e6bfa05cc1b2a398751e5914da6c2bfc443a88 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 26 Feb 2025 14:17:10 -0500 Subject: [PATCH] add FutureMoves::set_parent_child --- src/logic/future_moves.rs | 131 +++++++++++--------------------------- src/logic/move.rs | 11 +--- 2 files changed, 40 insertions(+), 102 deletions(-) diff --git a/src/logic/future_moves.rs b/src/logic/future_moves.rs index a3a8f75..82a50ff 100644 --- a/src/logic/future_moves.rs +++ b/src/logic/future_moves.rs @@ -124,16 +124,7 @@ impl FutureMoves { .what_if(i, j, new_color) .map(move |x| (i, j, x)) }) - .map(|(i, j, new_board)| { - Move::new( - i, - j, - new_board, - new_color, - self.agent_color, - Some(parent_idx), - ) - }) + .map(|(i, j, new_board)| Move::new(i, j, new_board, new_color, self.agent_color)) .collect(); let start_idx = self.arena.len(); @@ -141,7 +132,9 @@ impl FutureMoves { let new_indices = start_idx..self.arena.len(); - self.arena[parent_idx].children.extend(new_indices.clone()); + for child_idx in new_indices.clone() { + self.set_parent_child(parent_idx, child_idx); + } Some(new_indices) } @@ -257,7 +250,6 @@ impl FutureMoves { board, !self.agent_color, self.agent_color, - None, )); self.set_root_idx_raw(0); } @@ -298,6 +290,11 @@ impl FutureMoves { assert_eq!(self.check_arena().join("\n"), ""); } + pub fn set_parent_child(&mut self, parent: usize, child: usize) { + self.arena[parent].children.push(child); + self.arena[child].parent = Some(parent); + } + /// Checks the consistancy of the Arena (parents and children) /// returns a vector of errors ([`String`]) pub fn check_arena(&self) -> Vec { @@ -460,7 +457,7 @@ mod tests { board: Board::new(), winner: Winner::None, parent: None, - children: vec![1, 3, 4], + children: Vec::new(), value: None, self_value: 0, color: Piece::Black, @@ -470,46 +467,28 @@ mod tests { futm.update_root_idx_raw(0); // child 1 - futm.arena.push(Move::new( - 0, - 0, - Board::new(), - Piece::White, - Piece::Black, - Some(0), - )); + futm.arena + .push(Move::new(0, 0, Board::new(), Piece::White, Piece::Black)); + futm.set_parent_child(0, 1); - // dummy + // dummy (2) futm.arena.push(Move::new( 1234, 1234, Board::new(), Piece::White, Piece::Black, - None, )); - futm.arena.push(Move::new( - 0, - 0, - Board::new(), - Piece::White, - Piece::Black, - Some(0), - )); + // 3 + futm.arena + .push(Move::new(0, 0, Board::new(), Piece::White, Piece::Black)); + futm.set_parent_child(0, 3); - futm.arena.push(Move::new( - 0, - 0, - Board::new(), - Piece::White, - Piece::Black, - Some(0), - )); - - for i in futm.arena[0].children.clone() { - futm.arena[i].parent = Some(0); - } + // 4 + futm.arena + .push(Move::new(0, 0, Board::new(), Piece::White, Piece::Black)); + futm.set_parent_child(0, 4); assert_eq!(futm.arena_len(), 5); @@ -531,7 +510,6 @@ mod tests { Board::new().starting_pos(), Piece::Black, Piece::Black, - None, )); futm.update_root_idx_raw(0); @@ -574,16 +552,9 @@ mod tests { futm.update_root_idx_raw(0); // child 1 - futm.arena.push(Move::new( - 0, - 0, - Board::new(), - Piece::White, - Piece::Black, - Some(0), - )); - futm.arena[1].parent = Some(0); - futm.arena[1].children = vec![3]; + futm.arena + .push(Move::new(0, 0, Board::new(), Piece::White, Piece::Black)); + futm.set_parent_child(0, 1); // dummy futm.arena.push(Move::new( @@ -592,28 +563,17 @@ mod tests { Board::new(), Piece::White, Piece::Black, - None, )); - futm.arena.push(Move::new( - 0, - 0, - Board::new(), - Piece::White, - Piece::Black, - Some(0), - )); + futm.arena + .push(Move::new(0, 0, Board::new(), Piece::White, Piece::Black)); - futm.arena[3].parent = Some(1); + futm.set_parent_child(1, 3); - futm.arena.push(Move::new( - 0, - 0, - Board::new(), - Piece::White, - Piece::Black, - Some(0), - )); + futm.arena + .push(Move::new(0, 0, Board::new(), Piece::White, Piece::Black)); + + futm.set_parent_child(0, 4); assert_eq!(futm.depth_of(3), 2); } @@ -638,16 +598,9 @@ mod tests { futm.update_root_idx_raw(0); // child 1 - futm.arena.push(Move::new( - 0, - 0, - Board::new(), - Piece::White, - Piece::Black, - Some(0), - )); - futm.arena[1].parent = Some(0); - futm.arena[1].children = vec![3]; + futm.arena + .push(Move::new(0, 0, Board::new(), Piece::White, Piece::Black)); + futm.set_parent_child(0, 1); // dummy futm.arena.push(Move::new( @@ -656,19 +609,11 @@ mod tests { Board::new(), Piece::White, Piece::Black, - None, )); - futm.arena.push(Move::new( - 0, - 0, - Board::new(), - Piece::White, - Piece::Black, - Some(0), - )); - - futm.arena[3].parent = Some(1); + futm.arena + .push(Move::new(0, 0, Board::new(), Piece::White, Piece::Black)); + futm.set_parent_child(1, 3); assert_eq!( futm.by_depth(0..futm.arena.len()), diff --git a/src/logic/move.rs b/src/logic/move.rs index d72f7af..0b3a2ef 100644 --- a/src/logic/move.rs +++ b/src/logic/move.rs @@ -40,20 +40,13 @@ lazy_static! { } impl Move { - pub fn new( - i: usize, - j: usize, - board: Board, - color: Piece, - agent_color: Piece, - parent: Option, - ) -> Self { + pub fn new(i: usize, j: usize, board: Board, color: Piece, agent_color: Piece) -> Self { let mut m = Move { i, j, board, winner: board.game_winner(), - parent, + parent: None, children: Vec::new(), value: None, color,