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());
|
return Err("Internal error: get_ai_choice() iterated to SlotChoice::Invalid".into());
|
||||||
}
|
}
|
||||||
if let Some(utility) = get_utility_for_slot(player, slot, board) {
|
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
|
// 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 {
|
if utilities.len() > 1 {
|
||||||
for i in 1..utilities.len() {
|
for i in 1..utilities.len() {
|
||||||
utilities.swap(i, rng.rand_range(0..((i + 1) as u32)) as usize);
|
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 {
|
if let Err(e) = text_append_result {
|
||||||
log::warn!("ERROR: text append to info_text0 failed: {}", e);
|
log::warn!("ERROR: text append to info_text0 failed: {}", e);
|
||||||
}
|
}
|
||||||
|
shared
|
||||||
|
.game_state
|
||||||
|
.replace(GameState::PostGameResults(BoardState::Empty));
|
||||||
} else {
|
} else {
|
||||||
// a player won
|
// a player won
|
||||||
let turn = Turn::from(endgame_state);
|
let turn = Turn::from(endgame_state);
|
||||||
|
@ -366,6 +369,10 @@ impl Component for Wrapper {
|
||||||
log::warn!("ERROR: text append to info_text0 failed: {}", e);
|
log::warn!("ERROR: text append to info_text0 failed: {}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shared
|
||||||
|
.game_state
|
||||||
|
.replace(GameState::PostGameResults(turn.into()));
|
||||||
|
|
||||||
match win_type {
|
match win_type {
|
||||||
WinType::Horizontal(idx) => {
|
WinType::Horizontal(idx) => {
|
||||||
let placed_class_erase_result = element_remove_class(
|
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(),
|
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 {
|
if shared.turn.get() != player_type {
|
||||||
// get AI's choice
|
// get AI's choice
|
||||||
let choice = get_ai_choice(ai_difficulty, Turn::CyanPlayer, &shared.board)
|
let choice =
|
||||||
.expect("AI should have an available choice");
|
get_ai_choice(ai_difficulty, player_type.get_opposite(), &shared.board)
|
||||||
|
.expect("AI should have an available choice");
|
||||||
ctx.link()
|
ctx.link()
|
||||||
.send_message(WrapperMsg::Pressed(usize::from(choice) as u8));
|
.send_message(WrapperMsg::Pressed(usize::from(choice) as u8));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue