add FutureMoves::set_parent_child

This commit is contained in:
Simon Gardling 2025-02-26 14:17:10 -05:00
parent ff87337512
commit d4e6bfa05c
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
2 changed files with 40 additions and 102 deletions

View File

@ -124,16 +124,7 @@ impl FutureMoves {
.what_if(i, j, new_color) .what_if(i, j, new_color)
.map(move |x| (i, j, x)) .map(move |x| (i, j, x))
}) })
.map(|(i, j, new_board)| { .map(|(i, j, new_board)| Move::new(i, j, new_board, new_color, self.agent_color))
Move::new(
i,
j,
new_board,
new_color,
self.agent_color,
Some(parent_idx),
)
})
.collect(); .collect();
let start_idx = self.arena.len(); let start_idx = self.arena.len();
@ -141,7 +132,9 @@ impl FutureMoves {
let new_indices = start_idx..self.arena.len(); 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) Some(new_indices)
} }
@ -257,7 +250,6 @@ impl FutureMoves {
board, board,
!self.agent_color, !self.agent_color,
self.agent_color, self.agent_color,
None,
)); ));
self.set_root_idx_raw(0); self.set_root_idx_raw(0);
} }
@ -298,6 +290,11 @@ impl FutureMoves {
assert_eq!(self.check_arena().join("\n"), ""); 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) /// Checks the consistancy of the Arena (parents and children)
/// returns a vector of errors ([`String`]) /// returns a vector of errors ([`String`])
pub fn check_arena(&self) -> Vec<String> { pub fn check_arena(&self) -> Vec<String> {
@ -460,7 +457,7 @@ mod tests {
board: Board::new(), board: Board::new(),
winner: Winner::None, winner: Winner::None,
parent: None, parent: None,
children: vec![1, 3, 4], children: Vec::new(),
value: None, value: None,
self_value: 0, self_value: 0,
color: Piece::Black, color: Piece::Black,
@ -470,46 +467,28 @@ mod tests {
futm.update_root_idx_raw(0); futm.update_root_idx_raw(0);
// child 1 // child 1
futm.arena.push(Move::new( futm.arena
0, .push(Move::new(0, 0, Board::new(), Piece::White, Piece::Black));
0, futm.set_parent_child(0, 1);
Board::new(),
Piece::White,
Piece::Black,
Some(0),
));
// dummy // dummy (2)
futm.arena.push(Move::new( futm.arena.push(Move::new(
1234, 1234,
1234, 1234,
Board::new(), Board::new(),
Piece::White, Piece::White,
Piece::Black, Piece::Black,
None,
)); ));
futm.arena.push(Move::new( // 3
0, futm.arena
0, .push(Move::new(0, 0, Board::new(), Piece::White, Piece::Black));
Board::new(), futm.set_parent_child(0, 3);
Piece::White,
Piece::Black,
Some(0),
));
futm.arena.push(Move::new( // 4
0, futm.arena
0, .push(Move::new(0, 0, Board::new(), Piece::White, Piece::Black));
Board::new(), futm.set_parent_child(0, 4);
Piece::White,
Piece::Black,
Some(0),
));
for i in futm.arena[0].children.clone() {
futm.arena[i].parent = Some(0);
}
assert_eq!(futm.arena_len(), 5); assert_eq!(futm.arena_len(), 5);
@ -531,7 +510,6 @@ mod tests {
Board::new().starting_pos(), Board::new().starting_pos(),
Piece::Black, Piece::Black,
Piece::Black, Piece::Black,
None,
)); ));
futm.update_root_idx_raw(0); futm.update_root_idx_raw(0);
@ -574,16 +552,9 @@ mod tests {
futm.update_root_idx_raw(0); futm.update_root_idx_raw(0);
// child 1 // child 1
futm.arena.push(Move::new( futm.arena
0, .push(Move::new(0, 0, Board::new(), Piece::White, Piece::Black));
0, futm.set_parent_child(0, 1);
Board::new(),
Piece::White,
Piece::Black,
Some(0),
));
futm.arena[1].parent = Some(0);
futm.arena[1].children = vec![3];
// dummy // dummy
futm.arena.push(Move::new( futm.arena.push(Move::new(
@ -592,28 +563,17 @@ mod tests {
Board::new(), Board::new(),
Piece::White, Piece::White,
Piece::Black, Piece::Black,
None,
)); ));
futm.arena.push(Move::new( futm.arena
0, .push(Move::new(0, 0, Board::new(), Piece::White, Piece::Black));
0,
Board::new(),
Piece::White,
Piece::Black,
Some(0),
));
futm.arena[3].parent = Some(1); futm.set_parent_child(1, 3);
futm.arena.push(Move::new( futm.arena
0, .push(Move::new(0, 0, Board::new(), Piece::White, Piece::Black));
0,
Board::new(), futm.set_parent_child(0, 4);
Piece::White,
Piece::Black,
Some(0),
));
assert_eq!(futm.depth_of(3), 2); assert_eq!(futm.depth_of(3), 2);
} }
@ -638,16 +598,9 @@ mod tests {
futm.update_root_idx_raw(0); futm.update_root_idx_raw(0);
// child 1 // child 1
futm.arena.push(Move::new( futm.arena
0, .push(Move::new(0, 0, Board::new(), Piece::White, Piece::Black));
0, futm.set_parent_child(0, 1);
Board::new(),
Piece::White,
Piece::Black,
Some(0),
));
futm.arena[1].parent = Some(0);
futm.arena[1].children = vec![3];
// dummy // dummy
futm.arena.push(Move::new( futm.arena.push(Move::new(
@ -656,19 +609,11 @@ mod tests {
Board::new(), Board::new(),
Piece::White, Piece::White,
Piece::Black, Piece::Black,
None,
)); ));
futm.arena.push(Move::new( futm.arena
0, .push(Move::new(0, 0, Board::new(), Piece::White, Piece::Black));
0, futm.set_parent_child(1, 3);
Board::new(),
Piece::White,
Piece::Black,
Some(0),
));
futm.arena[3].parent = Some(1);
assert_eq!( assert_eq!(
futm.by_depth(0..futm.arena.len()), futm.by_depth(0..futm.arena.len()),

View File

@ -40,20 +40,13 @@ lazy_static! {
} }
impl Move { impl Move {
pub fn new( pub fn new(i: usize, j: usize, board: Board, color: Piece, agent_color: Piece) -> Self {
i: usize,
j: usize,
board: Board,
color: Piece,
agent_color: Piece,
parent: Option<usize>,
) -> Self {
let mut m = Move { let mut m = Move {
i, i,
j, j,
board, board,
winner: board.game_winner(), winner: board.game_winner(),
parent, parent: None,
children: Vec::new(), children: Vec::new(),
value: None, value: None,
color, color,