Brought random crate back
authorJustin Worthe <justin@worthe-it.co.za>
Sat, 12 May 2018 13:14:35 +0000 (15:14 +0200)
committerJustin Worthe <justin@worthe-it.co.za>
Sat, 12 May 2018 13:14:35 +0000 (15:14 +0200)
Cargo.toml
src/lib.rs
src/strategy/sample.rs

index 7628f0f..12a5214 100644 (file)
@@ -6,3 +6,5 @@ version = "1.0.0"
 serde_derive = "1.0.43"
 serde = "1.0.43"
 serde_json = "1.0.16"
+
+rand = "0.4.2"
index 158805a..a09f999 100644 (file)
@@ -4,6 +4,8 @@ extern crate serde_json;
 #[macro_use]
 extern crate serde_derive;
 
+extern crate rand;
+
 pub mod json;
 pub mod engine;
 pub mod strategy;
index 3a311e0..2dad924 100644 (file)
@@ -1,13 +1,16 @@
 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() {
+                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 option = rng.choose(&options);
         let buildings = [BuildingType::Attack, BuildingType::Defence, BuildingType::Energy];
-        let building = buildings.first();
+        let building = rng.choose(&buildings);
         match (option, building) {
             (Some(&p), Some(&building)) => Command::Build(p, building),
             _ => Command::Nothing