From 6cb0fe155082a55c71b4f1203096223cce38ee31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20K=C3=A4nner?= Date: Sun, 6 Oct 2024 21:55:15 +0200 Subject: [PATCH] evaluate stop condition at every simulation step --- battlesnake/src/logic.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/battlesnake/src/logic.rs b/battlesnake/src/logic.rs index f2caf36..324f79c 100644 --- a/battlesnake/src/logic.rs +++ b/battlesnake/src/logic.rs @@ -217,11 +217,16 @@ impl Node { board: &mut simulation::Board, deadline: &Instant, ) -> Result, 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