summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2018-05-14 22:18:52 +0200
committerJustin Worthe <justin@worthe-it.co.za>2018-05-14 22:18:52 +0200
commite762eda31ffa8e722737eba2583154ec038a068f (patch)
tree93b05c6b8b916c8cf20bcd4ddf3a8fd2a8c93fd3
parentb0455769297964f0b82ef0df78dad8da74e9bca9 (diff)
Changed invalid move checking to be a debug assertion
-rw-r--r--src/bin/perf-test.rs2
-rw-r--r--src/engine/mod.rs38
-rw-r--r--src/strategy/monte_carlo.rs7
3 files changed, 17 insertions, 30 deletions
diff --git a/src/bin/perf-test.rs b/src/bin/perf-test.rs
index 1397cc3..62c323c 100644
--- a/src/bin/perf-test.rs
+++ b/src/bin/perf-test.rs
@@ -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)
}
diff --git a/src/engine/mod.rs b/src/engine/mod.rs
index 446d3bf..090d9af 100644
--- a/src/engine/mod.rs
+++ b/src/engine/mod.rs
@@ -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;
diff --git a/src/strategy/monte_carlo.rs b/src/strategy/monte_carlo.rs
index 83627b0..59e86a6 100644
--- a/src/strategy/monte_carlo.rs
+++ b/src/strategy/monte_carlo.rs
@@ -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()
}
}