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 | 2 | public function northward(): Vector |
|
79 | |||
80 | 1 | public function eastward(): Vector |
|
84 | |||
85 | 2 | public function westward(): Vector |
|
89 | |||
90 | 2 | public function southward(): Vector |
|
94 | |||
95 | /** |
||
96 | * @return Direction |
||
97 | */ |
||
98 | 1 | public function rotateLeft(): Direction |
|
106 | |||
107 | /** |
||
108 | * @return Direction |
||
109 | */ |
||
110 | 1 | public function rotateRight() |
|
118 | |||
119 | /** |
||
120 | * @return Vector |
||
121 | */ |
||
122 | public function getDirectionAsVector(): Vector |
||
126 | |||
127 | /** |
||
128 | * @return string |
||
129 | * @throws \LogicException |
||
130 | */ |
||
131 | 2 | public function getDirectionAsString(): string |
|
145 | } |
||
146 |