-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpenguin_restrictions.rs
69 lines (59 loc) · 2.48 KB
/
penguin_restrictions.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use rostware23_lib::game::board::Board;
use rostware23_lib::game::common::{Coordinate, Vector};
use rostware23_lib::game::direction::Direction;
use rostware23_lib::game::penguin::Penguin;
#[derive(Debug, PartialEq)]
pub struct PenguinRestriction {
restricting_penguin_coords: Coordinate,
direction_to_start_penguin: Direction,
highest_reachable_angle: f64
}
impl PenguinRestriction {
pub fn new(restricting_penguin_coords: Coordinate, direction_to_start_penguin: Direction) -> Self {
let left = Direction::Left.vector();
let top_right = Direction::TopRight.vector();
Self {
restricting_penguin_coords,
direction_to_start_penguin,
highest_reachable_angle: left.angle_to(top_right)
}
}
pub fn is_in_restriction(&self, coordinate: Coordinate) -> bool {
let vector = Vector::between_coordinates(coordinate, self.restricting_penguin_coords.clone());
let direction_vector = self.direction_to_start_penguin.vector();
let angle = direction_vector.angle_to(vector);
angle >= self.highest_reachable_angle
}
}
pub trait PenguinRestrictions {
fn for_penguin(penguin: &Penguin, board: &Board) -> Self;
fn is_restricted(&self, coordinate: Coordinate) -> bool;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn highest_reachable_angle_is_correct() {
let actual = PenguinRestriction::new(Coordinate::new(0, 0), Direction::Left).highest_reachable_angle;
let degrees: f64 = 135.0;
assert_eq!(degrees.to_radians(), actual);
}
#[test]
fn coordinate_towards_top_right_is_in_restriction_to_the_left() {
let restriction = PenguinRestriction::new(Coordinate::new(10, 6), Direction::Left);
let checked_coordinate = Coordinate::new(11, 5);
assert!(restriction.is_in_restriction(checked_coordinate));
}
#[test]
fn coordinate_towards_top_right_far_from_origin_is_in_restriction_to_the_left() {
let restriction = PenguinRestriction::new(Coordinate::new(10, 6), Direction::Left);
let checked_coordinate = Coordinate::new(15, 7);
assert!(restriction.is_in_restriction(checked_coordinate));
}
#[test]
fn coordinate_towards_top_left_is_not_in_restriction_to_the_left() {
let restriction = PenguinRestriction::new(Coordinate::new(10, 6), Direction::Left);
let checked_coordinate = Coordinate::new(9, 7);
assert!(!restriction.is_in_restriction(checked_coordinate));
}
}