-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.php
46 lines (40 loc) · 940 Bytes
/
point.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
class Point {
public function __construct($x, $y) {
$this->x = $x;
$this->y = $y;
}
public function x() {
return $this->x;
}
public function y() {
return $this->y;
}
public function next(Direction $direction) {
$x = $this->x;
$y = $this->y;
if ($direction === Direction::up()) {
$y += 1;
} elseif ($direction === Direction::down()) {
$y -= 1;
} elseif ($direction === Direction::right()) {
$x += 1;
} elseif ($direction === Direction::left()) {
$x -= 1;
} else {
throw new IllegalDirectionException($direction);
}
return new Point($x, $y);
}
public function neighbors() {
return [
$this->next(Direction::up()),
$this->next(Direction::right()),
$this->next(Direction::down()),
$this->next(Direction::left())
];
}
public function __toString() {
return "({$this->x()}, {$this->y()})";
}
}