summaryrefslogtreecommitdiff
path: root/src/strategy/monte_carlo.rs
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2018-06-25 19:04:29 +0200
committerJustin Worthe <justin@worthe-it.co.za>2018-06-25 19:04:29 +0200
commit7bf7d8d977733cb02258b4a79faf2417c52e9323 (patch)
tree8a4286663e07d88a20e39d0a3f8210acdde22f93 /src/strategy/monte_carlo.rs
parent9e523171aee0a6ee5a255a248ec3c1e78f2ba21e (diff)
Compilation, allowing new moves to be chosen, and missile move order
Diffstat (limited to 'src/strategy/monte_carlo.rs')
-rw-r--r--src/strategy/monte_carlo.rs33
1 files changed, 27 insertions, 6 deletions
diff --git a/src/strategy/monte_carlo.rs b/src/strategy/monte_carlo.rs
index 0d813d3..1ea18db 100644
--- a/src/strategy/monte_carlo.rs
+++ b/src/strategy/monte_carlo.rs
@@ -76,25 +76,35 @@ fn simulate_to_endstate<R: Rng>(command_score: &mut CommandScore, settings: &Gam
fn random_player_move<R: Rng>(settings: &GameSettings, state: &GameState, rng: &mut R) -> Command {
let all_buildings = state.player.sensible_buildings(settings);
- random_move(&state.unoccupied_player_cells, &all_buildings, rng)
+ random_move(&settings, &state.unoccupied_player_cells, &all_buildings, rng)
}
fn random_opponent_move<R: Rng>(settings: &GameSettings, state: &GameState, rng: &mut R) -> Command {
let all_buildings = state.opponent.sensible_buildings(settings);
- random_move(&state.unoccupied_opponent_cells, &all_buildings, rng)
+ random_move(&settings, &state.unoccupied_opponent_cells, &all_buildings, rng)
}
-fn random_move<R: Rng>(all_positions: &[Point], all_buildings: &[BuildingType], rng: &mut R) -> Command {
- let number_of_commands = all_positions.len()*all_buildings.len()+1;
+fn random_move<R: Rng>(settings: &GameSettings, all_positions: &[Point], all_buildings: &[BuildingType], rng: &mut R) -> Command {
+
+ let building_command_count = all_positions.len()*all_buildings.len();
+ let deconstruct_count = (settings.size.x as usize * settings.size.y as usize / 2) - all_positions.len();
+ let nothing_count = 1;
+
+ let number_of_commands = building_command_count + deconstruct_count + nothing_count;
+
let choice_index = rng.gen_range(0, number_of_commands);
if choice_index == number_of_commands - 1 {
Command::Nothing
- } else {
+ } else if choice_index < building_command_count {
Command::Build(
all_positions[choice_index/all_buildings.len()],
all_buildings[choice_index%all_buildings.len()]
)
+ } else {
+ Command::Deconstruct(
+ all_positions[choice_index-building_command_count]
+ )
}
}
@@ -153,7 +163,11 @@ impl CommandScore {
fn init_command_scores(settings: &GameSettings, state: &GameState) -> Vec<CommandScore> {
let all_buildings = state.player.sensible_buildings(settings);
- let mut commands = Vec::with_capacity(state.unoccupied_player_cells.len()*all_buildings.len()+1);
+ let building_command_count = state.unoccupied_player_cells.len()*all_buildings.len();
+ let deconstruct_count = (settings.size.x as usize *settings.size.y as usize / 2) - state.unoccupied_player_cells.len();
+ let nothing_count = 1;
+
+ let mut commands = Vec::with_capacity(building_command_count + deconstruct_count + nothing_count);
commands.push(CommandScore::new(Command::Nothing));
for &position in &state.unoccupied_player_cells {
@@ -162,6 +176,13 @@ impl CommandScore {
}
}
+ for building in &state.player_buildings {
+ commands.push(CommandScore::new(Command::Deconstruct(building.pos)));
+ }
+ for building in &state.player_unconstructed_buildings {
+ commands.push(CommandScore::new(Command::Deconstruct(building.pos)));
+ }
+
commands
}
}