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 | 16 | public function __construct(?string $startingDirection = 'E') |
|
64 | |||
65 | /** |
||
66 | * @param string $direction |
||
67 | * @return Direction |
||
68 | */ |
||
69 | 15 | private function setDirectionFromString(string $direction): Direction |
|
77 | |||
78 | 2 | public function northward(): Vector |
|
82 | |||
83 | 1 | public function eastward(): Vector |
|
87 | |||
88 | 2 | public function westward(): Vector |
|
92 | |||
93 | 2 | public function southward(): Vector |
|
97 | |||
98 | /** |
||
99 | * @return Direction |
||
100 | */ |
||
101 | 2 | public function rotateLeft(): Direction |
|
109 | |||
110 | /** |
||
111 | * @return Direction |
||
112 | */ |
||
113 | 2 | public function rotateRight() |
|
121 | |||
122 | /** |
||
123 | * @return Vector |
||
124 | */ |
||
125 | 5 | public function getDirectionAsVector(): Vector |
|
129 | |||
130 | /** |
||
131 | * @return string |
||
132 | * @throws \LogicException |
||
133 | */ |
||
134 | 10 | public function getDirectionAsString(): string |
|
148 | } |
||
149 |