diff options
Diffstat (limited to 'src/math.rs')
-rw-r--r-- | src/math.rs | 36 |
1 files changed, 27 insertions, 9 deletions
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)); |