From: Stephen Seo Date: Mon, 7 Mar 2022 07:59:18 +0000 (+0900) Subject: Tweak Hard-level AI X-Git-Tag: sprint_02_day_1~2 X-Git-Url: https://git.seodisparate.com/stephenseo/static/git-favicon.png?a=commitdiff_plain;h=ec9aac84004088919388f16ee95168c70a985dca;p=EN605.607.81.SP22_ASDM_Project Tweak Hard-level AI Shuffles the utilities-index pairs for the cases where there are multiple pairs of the same utility value. --- 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; } }