]> git.seodisparate.com - EN605.607.81.SP22_ASDM_Project/commitdiff
Fix infinite loop bug, refactorings/fixes
authorStephen Seo <seo.disparate@gmail.com>
Thu, 10 Mar 2022 08:01:01 +0000 (17:01 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 10 Mar 2022 08:01:34 +0000 (17:01 +0900)
front_end/src/ai.rs
front_end/src/yew_components.rs

index 8640f5d3d4132fa599819e4eabd59f49b6ea3406..6b78424509e54b0441153af355da9ab0dccbed09 100644 (file)
@@ -72,7 +72,7 @@ pub fn get_ai_choice(
             return Err("Internal error: get_ai_choice() iterated to SlotChoice::Invalid".into());
         }
         if let Some(utility) = get_utility_for_slot(player, slot, board) {
-            utilities.push(utility);
+            utilities.push((i, utility));
         }
     }
 
@@ -81,7 +81,6 @@ pub fn get_ai_choice(
     }
 
     // shuffle utilities for the cases where there are equivalent utilities
-    let mut utilities: Vec<(usize, f64)> = utilities.into_iter().enumerate().collect();
     if utilities.len() > 1 {
         for i in 1..utilities.len() {
             utilities.swap(i, rng.rand_range(0..((i + 1) as u32)) as usize);
index 3f9a97c34497de8305b74eaf9ab51116f9e07aea..d9d3e91f307b519fc74bdc20a3c6f8d98a9a663f 100644 (file)
@@ -351,6 +351,9 @@ impl Component for Wrapper {
                         if let Err(e) = text_append_result {
                             log::warn!("ERROR: text append to info_text0 failed: {}", e);
                         }
+                        shared
+                            .game_state
+                            .replace(GameState::PostGameResults(BoardState::Empty));
                     } else {
                         // a player won
                         let turn = Turn::from(endgame_state);
@@ -366,6 +369,10 @@ impl Component for Wrapper {
                             log::warn!("ERROR: text append to info_text0 failed: {}", e);
                         }
 
+                        shared
+                            .game_state
+                            .replace(GameState::PostGameResults(turn.into()));
+
                         match win_type {
                             WinType::Horizontal(idx) => {
                                 let placed_class_erase_result = element_remove_class(
@@ -663,7 +670,7 @@ impl Component for Wrapper {
                                     shared.board[idx + 3 + 3 * (COLS as usize)].get().into_win(),
                                 );
                             }
-                            WinType::None => todo!(),
+                            WinType::None => unreachable!("WinType should never be None on win"),
                         }
                     }
 
@@ -719,8 +726,9 @@ impl Component for Wrapper {
                 {
                     if shared.turn.get() != player_type {
                         // get AI's choice
-                        let choice = get_ai_choice(ai_difficulty, Turn::CyanPlayer, &shared.board)
-                            .expect("AI should have an available choice");
+                        let choice =
+                            get_ai_choice(ai_difficulty, player_type.get_opposite(), &shared.board)
+                                .expect("AI should have an available choice");
                         ctx.link()
                             .send_message(WrapperMsg::Pressed(usize::from(choice) as u8));
                     }