evaluate stop condition at every simulation step

This commit is contained in:
Max Känner 2024-10-06 21:55:15 +02:00
parent d8452daddf
commit 6cb0fe1550

View File

@ -217,11 +217,16 @@ impl Node {
board: &mut simulation::Board,
deadline: &Instant,
) -> Result<Option<SnakeToken>, DeadlineError> {
let winner = if self.statistic.played == 0 {
let stop_condition =
|board: &simulation::Board| board.alive_snakes() <= 1 || Instant::now() >= *deadline;
let winner = if stop_condition(board) {
if Instant::now() >= *deadline {
return Err(DeadlineError);
}
board.snakes().next()
} else if self.statistic.played == 0 {
// We didn't simulate a game for this node yet. Do that
board.simulate_until(&mut thread_rng(), |board| {
board.alive_snakes() <= 1 || Instant::now() >= *deadline
});
board.simulate_until(&mut thread_rng(), stop_condition);
if Instant::now() >= *deadline {
return Err(DeadlineError);
}
@ -252,9 +257,6 @@ impl Node {
})
.collect();
if Instant::now() >= *deadline {
return Err(DeadlineError);
}
board.simulate_actions(&actions, &mut thread_rng());
let winner = self
.childs