Added profiling target with perf
[entelect-challenge-tower-defence.git] / src / strategy / sample.rs
index bd23916..2dad924 100644 (file)
@@ -1,14 +1,17 @@
 use engine;
 use engine::command::*;
 
+use rand::{thread_rng, Rng};
 
 pub fn choose_move(settings: &engine::settings::GameSettings, state: &engine::GameState) -> Command {
+    let mut rng = thread_rng();
+    
     if state.player.can_afford_defence_buildings(settings) {
         for y in 0..settings.size.y {
             if is_under_attack(state, y) {
                 let p_options = state.unoccupied_player_cells_in_row(settings, y);
-                if let Some(&p) = p_options.first() {
-                    return Command::Build(p, BuildingType::Defense);
+                if let Some(&p) = rng.choose(&p_options) {
+                    return Command::Build(p, BuildingType::Defence);
                 }
             }
         }
@@ -16,9 +19,9 @@ pub fn choose_move(settings: &engine::settings::GameSettings, state: &engine::Ga
 
     if state.player.can_afford_all_buildings(settings) {
         let options = state.unoccupied_player_cells(settings);
-        let option = options.first();
-        let buildings = [BuildingType::Attack, BuildingType::Defense, BuildingType::Energy];
-        let building = buildings.first();
+        let option = rng.choose(&options);
+        let buildings = [BuildingType::Attack, BuildingType::Defence, BuildingType::Energy];
+        let building = rng.choose(&buildings);
         match (option, building) {
             (Some(&p), Some(&building)) => Command::Build(p, building),
             _ => Command::Nothing