]> git.seodisparate.com - EN605.607.81.SP22_ASDM_Project/commitdiff
Tweak Hard-level AI
authorStephen Seo <seo.disparate@gmail.com>
Mon, 7 Mar 2022 07:59:18 +0000 (16:59 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 7 Mar 2022 07:59:18 +0000 (16:59 +0900)
Shuffles the utilities-index pairs for the cases where there are
multiple pairs of the same utility value.

front_end/src/ai.rs

index 87226c25a2425a87e93e3f71bca5c09ba57952f7..967d03361c70bb8e49c2fbc3bfe35662e6da5590 100644 (file)
@@ -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;
                 }
             }