forked from mackorone/mms-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Direction.py
83 lines (71 loc) · 2.65 KB
/
Direction.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import enum
class Direction(enum.Enum):
"""
Represents the cardinal directions: NORTH, EAST, SOUTH, WEST.
"""
NORTH = 0
EAST = 1
SOUTH = 2
WEST = 3
def minus_90(self) -> "Direction":
"""
Returns the direction obtained by rotating 90 degrees counter-clockwise.
"""
return Direction((self.value - 1) % 4)
def plus_90(self) -> "Direction":
"""
Returns the direction obtained by rotating 90 degrees clockwise.
"""
return Direction((self.value + 1) % 4)
def minus_180(self) -> "Direction":
"""
Returns the direction obtained by rotating 180 degrees.
"""
return Direction((self.value - 2) % 4)
def get_API_representation(self) -> str:
"""
Returns the API representation of the direction.
"""
if self == Direction.NORTH:
return "n"
elif self == Direction.EAST:
return "e"
elif self == Direction.SOUTH:
return "s"
elif self == Direction.WEST:
return "w"
def add_to_direction(self, direction: "Direction") -> "Direction":
"""
Adds the given direction to the current direction and returns the resulting direction.
"""
return (self.value + direction.value) % 4
def add_to_position(self, position: tuple[int, int]) -> tuple[int, int]:
"""
Adds the current direction to the given position and returns the resulting position.
"""
if self == Direction.NORTH:
return position[0], position[1] + 1
elif self == Direction.EAST:
return position[0] + 1, position[1]
elif self == Direction.SOUTH:
return position[0], position[1] - 1
elif self == Direction.WEST:
return position[0] - 1, position[1]
@staticmethod
def get_position_from_direction(position: tuple[int, int], direction: tuple[int, int]) -> tuple[int, int]:
"""
Returns the new position based on the given position and direction.
Args:
position (tuple): The current position as a tuple of (x, y) coordinates.
direction (Direction): The direction to move in.
Returns:
tuple: The new position as a tuple of (x, y) coordinates.
"""
if direction == Direction.NORTH:
return position[0], position[1] + 1
elif direction == Direction.EAST:
return position[0] + 1, position[1]
elif direction == Direction.SOUTH:
return position[0], position[1] - 1
elif direction == Direction.WEST:
return position[0] - 1, position[1]