1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kata\Rover; |
4
|
|
|
|
5
|
|
|
final class Rover |
6
|
|
|
{ |
7
|
|
|
private $map; |
8
|
|
|
private $coordinates; |
9
|
|
|
private $direction; |
10
|
|
|
private $obstacles_found; |
11
|
|
|
|
12
|
12 |
|
public function __construct(Map $a_map, Coordinates $a_starting_point, Direction $a_direction) |
13
|
|
|
{ |
14
|
12 |
|
$this->map = $a_map; |
15
|
12 |
|
$this->coordinates = $a_starting_point; |
16
|
12 |
|
$this->direction = $a_direction; |
17
|
12 |
|
$this->obstacles_found = []; |
18
|
12 |
|
} |
19
|
|
|
|
20
|
|
|
public function map(): Map |
21
|
|
|
{ |
22
|
|
|
return $this->map; |
23
|
|
|
} |
24
|
|
|
|
25
|
11 |
|
public function coordinates(): Coordinates |
26
|
|
|
{ |
27
|
11 |
|
return $this->coordinates; |
28
|
|
|
} |
29
|
|
|
|
30
|
8 |
|
public function direction(): Direction |
31
|
|
|
{ |
32
|
8 |
|
return $this->direction; |
33
|
|
|
} |
34
|
|
|
|
35
|
11 |
|
public function commands(array $commands): void |
36
|
|
|
{ |
37
|
11 |
|
foreach ($commands as $command) { |
38
|
11 |
|
$this->process($command); |
39
|
|
|
} |
40
|
11 |
|
} |
41
|
|
|
|
42
|
11 |
|
private function process(string $command): void |
43
|
|
|
{ |
44
|
11 |
|
if ('f' === $command) { |
45
|
8 |
|
$this->coordinates = $this->moveForward(); |
46
|
|
|
} |
47
|
11 |
|
if ('b' === $command) { |
48
|
3 |
|
$this->coordinates = $this->moveBackwards(); |
49
|
|
|
} |
50
|
11 |
|
if ('r' === $command) { |
51
|
6 |
|
$this->direction = $this->direction->turnRight(); |
52
|
|
|
} |
53
|
11 |
|
if ('l' === $command) { |
54
|
5 |
|
$this->direction = $this->direction->turnLeft(); |
55
|
|
|
} |
56
|
11 |
|
} |
57
|
|
|
|
58
|
8 |
|
private function moveForward(): Coordinates |
59
|
|
|
{ |
60
|
8 |
|
return $this->move($this->direction); |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
private function moveBackwards(): Coordinates |
64
|
|
|
{ |
65
|
3 |
|
return $this->move($this->direction->inverseDirection()); |
66
|
|
|
} |
67
|
|
|
|
68
|
9 |
|
private function move(Direction $a_direction): Coordinates |
69
|
|
|
{ |
70
|
9 |
|
switch ($a_direction->direction()) { |
71
|
|
|
case Direction::NORTH: |
72
|
6 |
|
$new_coordinates = new Coordinates( |
73
|
6 |
|
$this->coordinates->coordinateX(), |
74
|
6 |
|
$this->coordinates->coordinateY() + 1 |
75
|
|
|
); |
76
|
6 |
|
$new_coordinates = $this->map->normalizeCoordinate($new_coordinates); |
77
|
6 |
|
if (!$this->map->isObstacle($new_coordinates)) { |
78
|
6 |
|
return $new_coordinates; |
79
|
|
|
} |
80
|
|
|
$this->obstacles_found[] = $new_coordinates; |
81
|
|
|
return $this->coordinates; |
82
|
|
|
case Direction::SOUTH: |
83
|
4 |
|
$new_coordinates = new Coordinates( |
84
|
4 |
|
$this->coordinates->coordinateX(), |
85
|
4 |
|
$this->coordinates->coordinateY() - 1 |
86
|
|
|
); |
87
|
4 |
|
$new_coordinates = $this->map->normalizeCoordinate($new_coordinates); |
88
|
4 |
|
if (!$this->map->isObstacle($new_coordinates)) { |
89
|
4 |
|
return $new_coordinates; |
90
|
|
|
} |
91
|
|
|
$this->obstacles_found[] = $new_coordinates; |
92
|
|
|
return $this->coordinates; |
93
|
|
|
case Direction::EAST: |
94
|
4 |
|
$new_coordinates = new Coordinates( |
95
|
4 |
|
$this->coordinates->coordinateX() + 1, |
96
|
4 |
|
$this->coordinates->coordinateY() |
97
|
|
|
); |
98
|
4 |
|
$new_coordinates = $this->map->normalizeCoordinate($new_coordinates); |
99
|
4 |
|
if (!$this->map->isObstacle($new_coordinates)) { |
100
|
3 |
|
return $new_coordinates; |
101
|
|
|
} |
102
|
1 |
|
$this->obstacles_found[] = $new_coordinates; |
103
|
1 |
|
return $this->coordinates; |
104
|
|
|
case Direction::WEST: |
105
|
3 |
|
$new_coordinates = new Coordinates( |
106
|
3 |
|
$this->coordinates->coordinateX() - 1, |
107
|
3 |
|
$this->coordinates->coordinateY() |
108
|
|
|
); |
109
|
3 |
|
$new_coordinates = $this->map->normalizeCoordinate($new_coordinates); |
110
|
3 |
|
if (!$this->map->isObstacle($new_coordinates)) { |
111
|
3 |
|
return $new_coordinates; |
112
|
|
|
} |
113
|
|
|
$this->obstacles_found[] = $new_coordinates; |
114
|
|
|
return $this->coordinates; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
public function obstaclesFound(): array |
119
|
|
|
{ |
120
|
1 |
|
return $this->obstacles_found; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|