Changed invalid move checking to be a debug assertion
authorJustin Worthe <justin@worthe-it.co.za>
Mon, 14 May 2018 20:18:52 +0000 (22:18 +0200)
committerJustin Worthe <justin@worthe-it.co.za>
Mon, 14 May 2018 20:18:52 +0000 (22:18 +0200)
src/bin/perf-test.rs
src/engine/mod.rs
src/strategy/monte_carlo.rs

index 1397cc3..62c323c 100644 (file)
@@ -16,7 +16,7 @@ use std::io::prelude::*;
 use std::process;
 
 fn choose_move(settings: &engine::settings::GameSettings, state: &engine::GameState, start_time: &PreciseTime) -> Command {
-    let max_time = Duration::milliseconds(9950);
+    let max_time = Duration::milliseconds(1950);
     strategy::monte_carlo::choose_move(settings, state, start_time, max_time)
 }
 
index 446d3bf..090d9af 100644 (file)
@@ -25,8 +25,7 @@ pub enum GameStatus {
     Continue,
     PlayerWon,
     OpponentWon,
-    Draw,
-    InvalidMove
+    Draw
 }
 
 #[derive(Debug, Clone)]
@@ -66,13 +65,8 @@ impl GameState {
             return;
         }
 
-        let player_valid = GameState::perform_command(&mut self.player_buildings, &mut self.player, settings, player_command, &settings.size);
-        let opponent_valid = GameState::perform_command(&mut self.opponent_buildings, &mut self.opponent, settings, opponent_command, &settings.size);
-
-        if !player_valid || !opponent_valid {
-            self.status = GameStatus::InvalidMove;
-            return;
-        }
+        GameState::perform_command(&mut self.player_buildings, &mut self.player, settings, player_command, &settings.size);
+        GameState::perform_command(&mut self.opponent_buildings, &mut self.opponent, settings, opponent_command, &settings.size);
 
         GameState::update_construction(&mut self.player_buildings);
         GameState::update_construction(&mut self.opponent_buildings);
@@ -91,22 +85,20 @@ impl GameState {
         GameState::update_status(self);
     }
 
-    fn perform_command(buildings: &mut Vec<Building>, player: &mut Player, settings: &GameSettings, command: Command, size: &Point) -> bool {
+    fn perform_command(buildings: &mut Vec<Building>, player: &mut Player, settings: &GameSettings, command: Command, size: &Point) {
         match command {
-            Command::Nothing => { true },
+            Command::Nothing => { },
             Command::Build(p, b) => {
                 let blueprint = settings.building_settings(b);
-                
-                let occupied = buildings.iter().any(|b| b.pos == p);
-                let in_range = p.x < size.x && p.y < size.y;
-                let has_energy = player.energy >= blueprint.price;
-
-                let valid = !occupied && in_range && has_energy;
-                if valid {
-                    player.energy -= blueprint.price;
-                    buildings.push(Building::new(p, blueprint));
-                }
-                valid                
+
+                // This is used internally. I should not be making
+                // invalid moves!
+                debug_assert!(!buildings.iter().any(|b| b.pos == p));
+                debug_assert!(p.x < size.x && p.y < size.y);
+                debug_assert!(player.energy >= blueprint.price);
+
+                player.energy -= blueprint.price;
+                buildings.push(Building::new(p, blueprint));
             },
         }
     }
@@ -144,7 +136,7 @@ impl GameState {
                     },
                     Some(point) => {
                         missile.pos = point;
-                        for hit in opponent_buildings.iter_mut().filter(|b| b.is_constructed() && b.pos == point && b.health > 0) {
+                        for hit in opponent_buildings.iter_mut().filter(|b| b.is_constructed() && b.pos == point/* && b.health > 0*/) { //TODO surely this health>0 belongs? Not what the real game engine is doing unfortunately
                             let damage = cmp::min(missile.damage, hit.health);
                             hit.health -= damage;
                             missile.speed = 0;                    
index 83627b0..59e86a6 100644 (file)
@@ -4,7 +4,6 @@ use engine::geometry::*;
 use engine::{GameState, GameStatus};
 
 use rand::{thread_rng, Rng};
-use std::process;
 const MAX_MOVES: u16 = 400;
 
 use time::{Duration, PreciseTime};
@@ -56,11 +55,7 @@ fn simulate_to_endstate<R: Rng>(command_score: &mut CommandScore, settings: &Gam
         GameStatus::PlayerWon => command_score.add_victory(),
         GameStatus::OpponentWon => command_score.add_defeat(),
         GameStatus::Continue => command_score.add_stalemate(),
-        GameStatus::Draw => command_score.add_draw(),
-        GameStatus::InvalidMove => {
-            println!("Invalid move made while performing simulation");
-            process::exit(0);
-        }
+        GameStatus::Draw => command_score.add_draw()
     }
 }