From 92f91672fa4a2a688c6cbcda05f7640b86a288f7 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 10 Mar 2022 17:01:01 +0900 Subject: [PATCH] Fix infinite loop bug, refactorings/fixes --- front_end/src/ai.rs | 3 +-- front_end/src/yew_components.rs | 14 +++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/front_end/src/ai.rs b/front_end/src/ai.rs index 8640f5d..6b78424 100644 --- a/front_end/src/ai.rs +++ b/front_end/src/ai.rs @@ -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); diff --git a/front_end/src/yew_components.rs b/front_end/src/yew_components.rs index 3f9a97c..d9d3e91 100644 --- a/front_end/src/yew_components.rs +++ b/front_end/src/yew_components.rs @@ -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)); }