cache winner in Move

This commit is contained in:
2025-02-08 23:13:12 -05:00
parent 2302f3be35
commit f5d44ca9f5
4 changed files with 22 additions and 16 deletions

View File

@@ -13,6 +13,7 @@ struct Move {
color: Piece,
board: Board,
next_move: Vec<Move>,
winner: Option<Piece>,
}
impl Move {
@@ -21,22 +22,28 @@ impl Move {
return;
}
if self.next_move.is_empty() {
self.next_move = problem_space(&self.board, !self.color).collect();
// only compute next move if this move doesn't end the game
if self.winner.is_none() {
if self.next_move.is_empty() {
self.next_move = problem_space(&self.board, !self.color).collect();
}
self.next_move
.iter_mut()
.for_each(|x| x.populate_next_moves(i - 1));
}
self.next_move
.iter_mut()
.for_each(|x| x.populate_next_moves(i - 1));
}
fn value(&self, agent_color: Piece, depth: usize) -> i64 {
let mut self_value = self.captured as i64;
if self.board.game_winner(agent_color) != Some(!agent_color) {
if self.winner == Some(!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.
// We shouldn't prune branches because we still need to always react to the opponent's moves
self_value = i64::MIN;
} else if self.winner == Some(agent_color) {
// this move results in a
self_value = i64::MAX;
} else if agent_color != self.color {
self_value = -self_value;
}
@@ -81,7 +88,9 @@ impl Move {
color: !color,
board: *board,
next_move: problem_space(board, color).collect(),
winner: board.game_winner(!color),
}],
winner: board.game_winner(color),
}
}
}
@@ -100,6 +109,7 @@ fn problem_space(board: &Board, color: Piece) -> Box<dyn Iterator<Item = Move> +
color,
board,
next_move: Vec::new(),
winner: board.game_winner(color),
}),
)
}