summaryrefslogtreecommitdiff
path: root/src/shooting.rs
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@gmail.com>2017-05-20 22:20:47 +0200
committerJustin Worthe <justin.worthe@gmail.com>2017-05-20 22:20:47 +0200
commit6b8c71c635539a8609f82aa6eb52ea56c2c41c0c (patch)
tree23da0d8ae0d15ebc1e9b48797965f58958e9c34d /src/shooting.rs
parent973f7be695f7c3308d384e1ee30066547e4a07c7 (diff)
Finished up efficient elimination of found ships
Diffstat (limited to 'src/shooting.rs')
-rw-r--r--src/shooting.rs40
1 files changed, 33 insertions, 7 deletions
diff --git a/src/shooting.rs b/src/shooting.rs
index c2ca56e..616ebf3 100644
--- a/src/shooting.rs
+++ b/src/shooting.rs
@@ -1,16 +1,42 @@
+use rand;
+use rand::distributions::{IndependentSample, Range};
+
use actions::*;
use math::*;
-use state::*;
+use knowledge::*;
+
+pub fn shoot_smartly(knowledge: &Knowledge) -> Action {
+ let shot = if knowledge.has_unknown_hits() {
+ destroy_shoot(&knowledge)
+ }
+ else {
+ seek_shoot(&knowledge)
+ };
+
+ Action::Shoot(shot)
+}
-pub fn shoot_randomly(state: &State) -> Action {
+fn seek_shoot(knowledge: &Knowledge) -> Point {
let mut shot: Point;
while {
- shot = Point::random(state.map_size);
- let ref target = state.opponent_map.cells[shot.x as usize][shot.y as usize];
- target.damaged || target.missed
+ shot = Point::random(knowledge.map_size);
+ let ref target = knowledge.opponent_map.cells[shot.x as usize][shot.y as usize];
+ target.shot_attempted()
} {}
+ shot
+}
-
- Action::Shoot(shot)
+fn destroy_shoot(knowledge: &Knowledge) -> Point {
+ let possibilities = knowledge.get_best_adjacent_shots();
+ if possibilities.is_empty() {
+ seek_shoot(&knowledge)
+ }
+ else {
+ let mut rng = rand::thread_rng();
+ let between = Range::new(0, possibilities.len());
+ let i = between.ind_sample(&mut rng);
+
+ possibilities[i as usize]
+ }
}