From efd762e5d204c545862e664c003d6fa62024f6c6 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Thu, 6 Mar 2025 16:23:49 -0500 Subject: [PATCH] FutureMoves: initial move to not storing board --- src/logic/future_moves.rs | 19 +++++++++++-------- src/logic/move.rs | 10 +++------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/logic/future_moves.rs b/src/logic/future_moves.rs index d5103d1..c4dc283 100644 --- a/src/logic/future_moves.rs +++ b/src/logic/future_moves.rs @@ -19,6 +19,8 @@ pub struct FutureMoves { agent_color: Piece, config: FutureMoveConfig, + + board: Board, } #[derive(Copy, Clone)] @@ -87,6 +89,7 @@ impl FutureMoves { current_depth: 0, agent_color, config, + board: Board::new(), } } @@ -196,13 +199,15 @@ impl FutureMoves { let parent = &self.arena[parent_idx]; let new_color = !parent.color; + let parent_board = self + .get_board_from_idx(parent_idx) + .expect("unable to get board"); // use [`Board::all_positions`] here instead of [`Board::possible_moves`] // because we use [`Board::what_if`] later and we want to reduce calls to [`Board::propegate_from_dry`] let mut new: Vec = Board::all_positions() .flat_map(|coord| { - parent - .board + parent_board .what_if(coord, new_color) .map(move |x| (coord, x)) }) @@ -212,7 +217,7 @@ impl FutureMoves { .collect(); if new.is_empty() { - new.push(Move::new(None, parent.board, new_color, self.agent_color)); + new.push(Move::new(None, parent_board, new_color, self.agent_color)); } let start_idx = self.arena.len(); @@ -321,7 +326,7 @@ impl FutureMoves { 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; + let mut board = self.board; for (m, c) in hist { if let Some(m) = m { board.place(m, c).expect("move would not propegate"); @@ -361,7 +366,8 @@ impl FutureMoves { // match the agent_color usually root or great-grand child .filter(|&idx| self.depth_of(idx) % 2 == 0) .find(|&idx| { - &self.arena[idx].board == board && self.arena[idx].color == !self.agent_color + self.get_board_from_idx(idx).as_ref() == Some(board) + && self.arena[idx].color == !self.agent_color }); if let Some(curr_board_idx) = curr_board { @@ -577,7 +583,6 @@ mod tests { futm.arena.push(Move { coord: None, - board: Board::new(), winner: Winner::None, parent: None, children: Vec::new(), @@ -664,7 +669,6 @@ mod tests { futm.arena.push(Move { coord: None, - board: Board::new(), winner: Winner::None, parent: None, children: vec![], @@ -705,7 +709,6 @@ mod tests { futm.arena.push(Move { coord: None, - board: Board::new(), winner: Winner::None, parent: None, children: vec![1], diff --git a/src/logic/move.rs b/src/logic/move.rs index c5ec2b9..f014948 100644 --- a/src/logic/move.rs +++ b/src/logic/move.rs @@ -7,9 +7,6 @@ pub struct Move { /// Coordinates (i, j) of the move (if it exists) pub coord: Option, - /// [`Board`] state after move is made - pub board: Board, - /// Current winner of the match pub winner: Winner, @@ -41,7 +38,6 @@ impl Move { pub fn new(coord: Option, board: Board, color: Piece, agent_color: Piece) -> Self { let mut m = Move { coord, - board, winner: board.game_winner(), parent: None, children: Vec::new(), @@ -51,11 +47,11 @@ impl Move { self_value: 0, tried_children: false, }; - m.self_value = m.compute_self_value(agent_color); + m.self_value = m.compute_self_value(agent_color, &board); m } - fn compute_self_value(&self, agent_color: Piece) -> i16 { + fn compute_self_value(&self, agent_color: Piece, board: &Board) -> i16 { if self.winner == Winner::Player(!agent_color) { // if this board results in the opponent winning, MAJORLY negatively weigh this move // NOTE! this branch isn't completely deleted because if so, the bot wouldn't make a move. @@ -72,7 +68,7 @@ impl Move { // I guess ignore Ties here, don't give them an explicit value, // because even in the case of ties, we want to have a higher score - BVM.board_value(&self.board, agent_color) + BVM.board_value(board, agent_color) } /// Sort children of the [`Move`] by their self_value in `arena`