From ec9aac84004088919388f16ee95168c70a985dca Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 7 Mar 2022 16:59:18 +0900 Subject: [PATCH] Tweak Hard-level AI Shuffles the utilities-index pairs for the cases where there are multiple pairs of the same utility value. --- front_end/src/ai.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/front_end/src/ai.rs b/front_end/src/ai.rs index 87226c2..967d033 100644 --- a/front_end/src/ai.rs +++ b/front_end/src/ai.rs @@ -115,12 +115,19 @@ pub fn get_ai_choice( AIDifficulty::Easy => pick_some_of_choices(AI_EASY_MAX_CHOICES), AIDifficulty::Normal => pick_some_of_choices(AI_NORMAL_MAX_CHOICES), AIDifficulty::Hard => { + let mut utilities: Vec<(usize, f64)> = utilities.into_iter().enumerate().collect(); + // shuffle utilities for the cases where there are equivalent utilities + if utilities.len() > 1 { + for i in 1..utilities.len() { + utilities.swap(i, thread_rng().gen_range(0..=i)); + } + } // only pick the best option all the time - let mut max = 0.0f64; + let mut max = -1.0f64; let mut max_idx: usize = 0; - for (idx, utility) in utilities.iter().enumerate() { - if *utility > max { - max = *utility; + for (idx, utility) in utilities { + if utility > max { + max = utility; max_idx = idx; } }