From d6bec472109806eea2ad86741edd3eb1571b872c Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sat, 12 May 2018 15:14:35 +0200 Subject: Brought random crate back --- src/lib.rs | 2 ++ src/strategy/sample.rs | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 158805a..a09f999 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/strategy/sample.rs b/src/strategy/sample.rs index 3a311e0..2dad924 100644 --- a/src/strategy/sample.rs +++ b/src/strategy/sample.rs @@ -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 -- cgit v1.2.3