|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kata\Rover; |
|
4
|
|
|
|
|
5
|
|
|
final class Rover |
|
6
|
|
|
{ |
|
7
|
|
|
private $map; |
|
8
|
|
|
private $coordinates; |
|
9
|
|
|
private $direction; |
|
10
|
|
|
|
|
11
|
8 |
|
public function __construct(Map $a_map, Coordinates $a_starting_point, Direction $a_direction) |
|
12
|
|
|
{ |
|
13
|
8 |
|
$this->map = $a_map; |
|
14
|
8 |
|
$this->coordinates = $a_starting_point; |
|
15
|
8 |
|
$this->direction = $a_direction; |
|
16
|
8 |
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function map(): Map |
|
19
|
|
|
{ |
|
20
|
|
|
return $this->map; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
8 |
|
public function coordinates(): Coordinates |
|
24
|
|
|
{ |
|
25
|
8 |
|
return $this->coordinates; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
5 |
|
public function direction(): Direction |
|
29
|
|
|
{ |
|
30
|
5 |
|
return $this->direction; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
7 |
|
public function commands(array $commands): void |
|
34
|
|
|
{ |
|
35
|
7 |
|
foreach ($commands as $command) { |
|
36
|
7 |
|
$this->process($command); |
|
37
|
|
|
} |
|
38
|
7 |
|
} |
|
39
|
|
|
|
|
40
|
7 |
|
private function process(string $command): void |
|
41
|
|
|
{ |
|
42
|
7 |
|
if ('f' === $command) { |
|
43
|
4 |
|
$this->coordinates = $this->moveForward(); |
|
44
|
|
|
} |
|
45
|
7 |
|
if ('b' === $command) { |
|
46
|
2 |
|
$this->coordinates = $this->moveBackwards(); |
|
47
|
|
|
} |
|
48
|
7 |
|
if ('r' === $command) { |
|
49
|
4 |
|
$this->direction = $this->direction->turnRight(); |
|
50
|
|
|
} |
|
51
|
7 |
|
if ('l' === $command) { |
|
52
|
3 |
|
$this->direction = $this->direction->turnLeft(); |
|
53
|
|
|
} |
|
54
|
7 |
|
} |
|
55
|
|
|
|
|
56
|
4 |
|
private function moveForward(): Coordinates |
|
57
|
|
|
{ |
|
58
|
4 |
|
return $this->move($this->direction); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
private function moveBackwards(): Coordinates |
|
62
|
|
|
{ |
|
63
|
2 |
|
return $this->move($this->direction->inverseDirection()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
5 |
|
private function move(Direction $a_direction): Coordinates |
|
67
|
|
|
{ |
|
68
|
5 |
|
switch ($a_direction->direction()) { |
|
69
|
|
|
case Direction::NORTH: |
|
70
|
4 |
|
return new Coordinates( |
|
71
|
4 |
|
$this->coordinates->coordinateX(), |
|
72
|
4 |
|
$this->coordinates->coordinateY() + 1 |
|
73
|
|
|
); |
|
74
|
|
|
break; |
|
|
|
|
|
|
75
|
|
|
case Direction::SOUTH: |
|
76
|
3 |
|
return new Coordinates( |
|
77
|
3 |
|
$this->coordinates->coordinateX(), |
|
78
|
3 |
|
$this->coordinates->coordinateY() - 1 |
|
79
|
|
|
); |
|
80
|
|
|
case Direction::EAST: |
|
81
|
3 |
|
return new Coordinates( |
|
82
|
3 |
|
$this->coordinates->coordinateX() + 1, |
|
83
|
3 |
|
$this->coordinates->coordinateY() |
|
84
|
|
|
); |
|
85
|
|
|
case Direction::WEST: |
|
86
|
1 |
|
return new Coordinates( |
|
87
|
1 |
|
$this->coordinates->coordinateX() - 1, |
|
88
|
1 |
|
$this->coordinates->coordinateY() |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.