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