|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* (c) 2018 Douglas Reith. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Reith\ToyRobot\Domain\Robot; |
|
12
|
|
|
|
|
13
|
|
|
use Assert\Assert; |
|
14
|
|
|
use Reith\ToyRobot\Domain\Space\SpaceInterface; |
|
15
|
|
|
use Reith\ToyRobot\Domain\Robot\Exception\NotPlacedInSpaceException; |
|
16
|
|
|
|
|
17
|
|
|
class Robot |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Purchased by Rupert, then tanked :). |
|
21
|
|
|
* |
|
22
|
|
|
* @var SpaceInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $mySpace; |
|
25
|
|
|
|
|
26
|
|
|
private $placement; |
|
27
|
|
|
|
|
28
|
5 |
|
private function __construct(SpaceInterface $space, Place $placement) |
|
29
|
|
|
{ |
|
30
|
5 |
|
$this->mySpace = $space; |
|
31
|
5 |
|
$this->placement = $placement; |
|
32
|
5 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param SpaceInterface $space |
|
36
|
|
|
* @param Place $placement |
|
37
|
|
|
* |
|
38
|
|
|
* @return Robot |
|
39
|
|
|
* |
|
40
|
|
|
* @throws Assert\AssertionFailedException |
|
41
|
|
|
*/ |
|
42
|
5 |
|
public static function create(SpaceInterface $space, Place $placement): Robot |
|
43
|
|
|
{ |
|
44
|
5 |
|
return new static($space, $placement); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
public function moveNorthward(): Robot |
|
48
|
|
|
{ |
|
49
|
2 |
|
$this->validateCanMove(); |
|
50
|
|
|
|
|
51
|
2 |
|
$this->placement = $this->mySpace->move( |
|
52
|
2 |
|
$this->placement, |
|
53
|
2 |
|
CompassMove::northward() |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
2 |
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
public function moveEastward(): Robot |
|
60
|
|
|
{ |
|
61
|
1 |
|
$this->validateCanMove(); |
|
62
|
|
|
|
|
63
|
1 |
|
$this->placement = $this->mySpace->move( |
|
64
|
1 |
|
$this->placement, |
|
65
|
1 |
|
CompassMove::eastward() |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
1 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
public function moveSouthward(): Robot |
|
72
|
|
|
{ |
|
73
|
2 |
|
$this->validateCanMove(); |
|
74
|
|
|
|
|
75
|
2 |
|
$this->placement = $this->mySpace->move( |
|
76
|
2 |
|
$this->placement, |
|
77
|
2 |
|
CompassMove::southward() |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
1 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
public function moveWestward(): Robot |
|
84
|
|
|
{ |
|
85
|
2 |
|
$this->validateCanMove(); |
|
86
|
|
|
|
|
87
|
2 |
|
$this->placement = $this->mySpace->move( |
|
88
|
2 |
|
$this->placement, |
|
89
|
2 |
|
CompassMove::westward() |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
1 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
public function getPlacement(): Place |
|
96
|
|
|
{ |
|
97
|
1 |
|
return $this->placement; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* validateCanMove. |
|
102
|
|
|
* |
|
103
|
|
|
* The robot requires a space during construction, however, |
|
104
|
|
|
* this is extra precautionary in case the space is removed |
|
105
|
|
|
* |
|
106
|
|
|
* @throws NotPlacedInSpaceException |
|
107
|
|
|
*/ |
|
108
|
4 |
|
private function validateCanMove(): void |
|
109
|
|
|
{ |
|
110
|
4 |
|
if (!$this->mySpace) { |
|
111
|
|
|
throw new NotPlacedInSpaceException( |
|
112
|
|
|
'I cannot move until placed in space' |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
4 |
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|