diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/knowledge.rs | 113 | ||||
-rw-r--r-- | src/math.rs | 36 |
2 files changed, 131 insertions, 18 deletions
diff --git a/src/knowledge.rs b/src/knowledge.rs index 4ade5f5..f271dbe 100644 --- a/src/knowledge.rs +++ b/src/knowledge.rs @@ -53,21 +53,57 @@ impl Knowledge { .map(|weapon| (weapon.clone(), weapon.single_shot_rounds_to_ready(energy, state.map_size))) .collect(); - let points = match self.last_action { + let (hits, misses, _) = match self.last_action { Action::PlaceShips(_) => { - vec!() + (vec!(), vec!(), vec!()) }, Action::Shoot(Weapon::SingleShot, p) => { - vec!(p) + Knowledge::to_hits_and_misses(vec!(Some(p)), &state) }, - Action::Shoot(w, p) => { - vec!() - //TODO + Action::Shoot(Weapon::DoubleShotVertical, p) => { + Knowledge::to_hits_and_misses(vec!( + p.move_point(Direction::North, 1, state.map_size), + p.move_point(Direction::South, 1, state.map_size) + ), &state) + }, + Action::Shoot(Weapon::DoubleShotHorizontal, p) => { + Knowledge::to_hits_and_misses(vec!( + p.move_point(Direction::East, 1, state.map_size), + p.move_point(Direction::West, 1, state.map_size) + ), &state) + }, + Action::Shoot(Weapon::CornerShot, p) => { + Knowledge::to_hits_and_misses(vec!( + p.move_point(Direction::NorthEast, 1, state.map_size), + p.move_point(Direction::SouthEast, 1, state.map_size), + p.move_point(Direction::NorthWest, 1, state.map_size), + p.move_point(Direction::SouthWest, 1, state.map_size), + ), &state) + }, + Action::Shoot(Weapon::CrossShotDiagonal, p) => { + Knowledge::to_hits_and_misses(vec!( + p.move_point(Direction::NorthEast, 1, state.map_size), + p.move_point(Direction::SouthEast, 1, state.map_size), + p.move_point(Direction::NorthWest, 1, state.map_size), + p.move_point(Direction::SouthWest, 1, state.map_size), + Some(p) + ), &state) + }, + Action::Shoot(Weapon::CrossShotHorizontal, p) => { + Knowledge::to_hits_and_misses(vec!( + p.move_point(Direction::North, 1, state.map_size), + p.move_point(Direction::East, 1, state.map_size), + p.move_point(Direction::South, 1, state.map_size), + p.move_point(Direction::West, 1, state.map_size), + Some(p) + ), &state) + }, + + Action::Shoot(Weapon::SeekerMissle, p) => { + Knowledge::seeker_hits_and_misses(p, &state) } }; - - let misses = points.iter().filter(|p| state.opponent_map.cells[p.x as usize][p.y as usize].missed).cloned().collect(); - let hits = points.iter().filter(|p| !state.opponent_map.cells[p.x as usize][p.y as usize].missed).cloned().collect(); + let sunk_ships = new_knowledge.opponent_map.update_sunk_ships(&state); new_knowledge.opponent_map.update_from_shot(hits, misses, sunk_ships); @@ -75,6 +111,65 @@ impl Knowledge { new_knowledge } + fn to_hits_and_misses(points: Vec<Option<Point>>, state: &State) -> (Vec<Point>, Vec<Point>, Vec<Point>) { + let points = points.iter().filter_map(|&p| p).collect::<Vec<_>>(); + + let hits = points.iter().filter(|p| !state.opponent_map.cells[p.x as usize][p.y as usize].damaged).cloned().collect(); + let misses = points.iter().filter(|p| state.opponent_map.cells[p.x as usize][p.y as usize].missed).cloned().collect(); + let unknown = points.iter().filter(|p| !state.opponent_map.cells[p.x as usize][p.y as usize].missed && !state.opponent_map.cells[p.x as usize][p.y as usize].damaged).cloned().collect(); + + (hits, misses, unknown) + } + + fn seeker_hits_and_misses(p: Point, state: &State) -> (Vec<Point>, Vec<Point>, Vec<Point>) { + let mut misses: Vec<Point> = Vec::new(); + let mut hits: Vec<Point> = Vec::new(); + + let rings = vec!( + vec!( + //0 + Some(p) + ), + vec!( + //1 + p.move_point(Direction::North, 1, state.map_size), + p.move_point(Direction::East, 1, state.map_size), + p.move_point(Direction::South, 1, state.map_size), + p.move_point(Direction::West, 1, state.map_size), + ), + vec!( + //1.44 + p.move_point(Direction::NorthEast, 1, state.map_size), + p.move_point(Direction::SouthEast, 1, state.map_size), + p.move_point(Direction::NorthWest, 1, state.map_size), + p.move_point(Direction::SouthWest, 1, state.map_size) + ), + vec!( + //2 + p.move_point(Direction::North, 2, state.map_size), + p.move_point(Direction::East, 2, state.map_size), + p.move_point(Direction::South, 2, state.map_size), + p.move_point(Direction::West, 2, state.map_size), + ) + ); + + //start in the center. Add rings, until I find a hit + //don't add more after a hit is found + for ring in rings { + if hits.is_empty() { + let (mut new_hits, mut new_misses, mut unknown) = Knowledge::to_hits_and_misses(ring, &state); + misses.append(&mut new_misses); + if !new_hits.is_empty() { + hits.append(&mut new_hits); + } else { + misses.append(&mut unknown); + } + } + } + + (hits, misses, vec!()) + } + pub fn has_unknown_hits(&self) -> bool { self.opponent_map.cells.iter().fold(false, |acc, x| { x.iter().fold(acc, |acc, y| acc || y.unknown_hit()) diff --git a/src/math.rs b/src/math.rs index a93d03e..3187829 100644 --- a/src/math.rs +++ b/src/math.rs @@ -5,9 +5,13 @@ use rand::distributions::{IndependentSample, Range}; #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)] pub enum Direction { North, + NorthEast, East, + SouthEast, South, + SouthWest, West, + NorthWest } use Direction::*; @@ -19,7 +23,11 @@ impl fmt::Display for Direction { &North => "North", &East => "East", &South => "South", - &West => "West" + &West => "West", + &NorthEast => "NorthEast", + &SouthEast => "SouthEast", + &NorthWest => "NorthWest", + &SouthWest => "SouthWest" } ) } @@ -77,13 +85,13 @@ impl Point { pub fn move_point(&self, direction: Direction, distance: i32, map_size: u16) -> Option<Point> { let x = self.x as i32 + match direction { - West => -distance, - East => distance, + West|NorthWest|SouthWest => -distance, + East|NorthEast|SouthEast => distance, _ => 0 }; let y = self.y as i32 + match direction { - South => -distance, - North => distance, + South|SouthWest|SouthEast => -distance, + North|NorthWest|NorthEast => distance, _ => 0 }; let max = map_size as i32; @@ -114,17 +122,20 @@ impl Point { pub fn check_for_ship_collision(&self, ship_start: Point, direction: Direction, length: u16) -> bool { let reverse = match direction { West | South => true, - East | North => false + East | North => false, + _ => false //ships cannot go diagonally }; let same_cross = match direction { East | West => self.y == ship_start.y, - North | South => self.x == ship_start.x + North | South => self.x == ship_start.x, + _ => false //ships cannot go diagonally }; let (parr_self, parr_ship) = match direction { East | West => (self.x, ship_start.x), - North | South => (self.y, ship_start.y) + North | South => (self.y, ship_start.y), + _ => (self.x, self.y) //ships cannot go diagonally }; let corrected_parr_ship = match reverse { @@ -163,7 +174,14 @@ mod point_tests { } } - + #[test] + fn move_point_works_north_west() { + assert_eq!(Some(Point::new(3,7)), Point::new(5,5).move_point(NorthWest, 2, 10)); + assert_eq!(Some(Point::new(7,3)), Point::new(5,5).move_point(NorthWest, -2, 10)); + assert_eq!(None, Point::new(5,5).move_point(NorthWest, 6, 10)); + assert_eq!(None, Point::new(5,5).move_point(NorthWest, -5, 10)); + } + #[test] fn move_point_works_west() { assert_eq!(Some(Point::new(3,5)), Point::new(5,5).move_point(West, 2, 10)); |