From 856a6a17aa927e95e33cffcbd178efe86ce14262 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Thu, 9 Aug 2018 22:17:02 +0200 Subject: Rearranged random move selection to avoid expensive division --- src/engine/constants.rs | 2 +- src/strategy/monte_carlo.rs | 15 +++++---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/engine/constants.rs b/src/engine/constants.rs index 2e80988..458251f 100644 --- a/src/engine/constants.rs +++ b/src/engine/constants.rs @@ -28,7 +28,7 @@ pub const ENERGY_CONSTRUCTION_TIME: u8 = 1; pub const DECONSTRUCT_ENERGY: u16 = 5; -pub const MAX_CONCURRENT_CONSTRUCTION: usize = 5; //2 teslas, and 3 of anything else +pub const MAX_CONCURRENT_CONSTRUCTION: usize = 6; //2 teslas, and 3 of anything else, 1 extra because it's push here then update construction times #[cfg(not(feature = "reduced-time"))] diff --git a/src/strategy/monte_carlo.rs b/src/strategy/monte_carlo.rs index 25b5a66..0844402 100644 --- a/src/strategy/monte_carlo.rs +++ b/src/strategy/monte_carlo.rs @@ -126,21 +126,16 @@ fn random_opponent_move(state: &BitwiseGameState, rng: &mut R) -> Comman // TODO: Given enough energy, most opponents won't do nothing fn random_movePoint>(all_buildings: &[BuildingType], rng: &mut R, free_positions_count: usize, get_point: F) -> Command { - let building_command_count = free_positions_count*all_buildings.len(); let nothing_count = 1; - - let number_of_commands = building_command_count + nothing_count; + let building_choice_index = rng.gen_range(0, nothing_count + all_buildings.len()); - let choice_index = rng.gen_range(0, number_of_commands); - - // TODO: Remove the divide here? - - if choice_index == number_of_commands - 1 { + if building_choice_index == all_buildings.len() { Command::Nothing } else { + let position_choice = rng.gen_range(0, free_positions_count); Command::Build( - get_point(choice_index/all_buildings.len()), - all_buildings[choice_index%all_buildings.len()] + get_point(position_choice), + all_buildings[building_choice_index] ) } } -- cgit v1.2.3