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)
.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<String> {
@ -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()),

View File

@ -40,20 +40,13 @@ lazy_static! {
}
impl Move {
pub fn new(
i: usize,
j: usize,
board: Board,
color: Piece,
agent_color: Piece,
parent: Option<usize>,
) -> 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,