Fix infinite loop bug, refactorings/fixes
This commit is contained in:
parent
38e7772d4f
commit
92f91672fa
2 changed files with 12 additions and 5 deletions
|
@ -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);
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue