-
Notifications
You must be signed in to change notification settings - Fork 0
/
direction.php
76 lines (64 loc) · 1.57 KB
/
direction.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
class Direction {
private static $initialized = false;
private static $left;
private static $right;
private static $up;
private static $down;
private static function initialized() {
return self::$intialized();
}
private static function initialize() {
self::$up = new Direction('up');
self::$down = new Direction('down');
self::$left = new Direction('left');
self::$right = new Direction('right');
self::$initialized = true;
}
public static function up() {
if (!self::$initialized) {
self::initialize();
}
return self::$up;
}
public static function down() {
if (!self::$initialized) {
self::initialize();
}
return self::$down;
}
public static function left() {
if (!self::$initialized) {
self::initialize();
}
return self::$left;
}
public static function right() {
if (!self::$initialized) {
self::initialize();
}
return self::$right;
}
private $name;
private function __construct($name) {
$this->name = $name;
}
public function opposite() {
if ($this->name() === Direction::up()->name()) {
return Direction::down();
} elseif ($this === Direction::down()) {
return Direction::up();
} elseif ($this === Direction::left()) {
return Direction::right();
} elseif ($this === Direction::right()) {
return Direction::left();
}
throw new IllegalDirectionException((string)$this);
}
public function name() {
return $this->name;
}
public function __toString() {
return $this->name();
}
}