1 | <?php |
||
22 | class Direction |
||
23 | { |
||
24 | // Counter clockwise |
||
25 | private const ROTATE_LEFT_MATRIX = [ |
||
26 | [0, -1], |
||
27 | [1, 0] |
||
28 | ]; |
||
29 | |||
30 | // Clockwise |
||
31 | private const ROTATE_RIGHT_MATRIX = [ |
||
32 | [0, 1], |
||
33 | [-1, 0] |
||
34 | ]; |
||
35 | |||
36 | private const CHAR_VECTOR_MAPPING = [ |
||
37 | 'N' => [0, 1], |
||
38 | 'E' => [1, 0], |
||
39 | 'S' => [0, -1], |
||
40 | 'W' => [-1, 0], |
||
41 | ]; |
||
42 | |||
43 | private $leftTransform; |
||
44 | |||
45 | private $rightTransform; |
||
46 | |||
47 | private $direction; |
||
48 | |||
49 | /** |
||
50 | * @param string|null $startingDirection |
||
51 | */ |
||
52 | 11 | public function __construct(?string $startingDirection = 'E') |
|
64 | |||
65 | /** |
||
66 | * @param string $direction |
||
67 | */ |
||
68 | 10 | private function setDirectionFromString(string $direction): void |
|
74 | |||
75 | /** |
||
76 | * @return Direction |
||
77 | */ |
||
78 | 1 | public function rotateLeft(): Direction |
|
86 | |||
87 | /** |
||
88 | * @return Direction |
||
89 | */ |
||
90 | 1 | public function rotateRight() |
|
98 | |||
99 | /** |
||
100 | * @return Vector |
||
101 | */ |
||
102 | public function getDirectionAsVector(): Vector |
||
106 | |||
107 | /** |
||
108 | * @return string |
||
109 | * @throws \LogicException |
||
110 | */ |
||
111 | 2 | public function getDirectionAsString(): string |
|
125 | } |
||
126 |