1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Stratadox\PuzzleSolver\Puzzle\SlidingCrates; |
4
|
|
|
|
5
|
|
|
use JsonSerializable; |
6
|
|
|
use Stratadox\PuzzleSolver\Move; |
7
|
|
|
use Stratadox\PuzzleSolver\Moves; |
8
|
|
|
use function in_array; |
9
|
|
|
|
10
|
|
|
final class Push implements Move, JsonSerializable |
11
|
|
|
{ |
12
|
|
|
private const LEFT = 'left'; |
13
|
|
|
private const RIGHT = 'right'; |
14
|
|
|
private const UP = 'up'; |
15
|
|
|
private const DOWN = 'down'; |
16
|
|
|
|
17
|
|
|
private const HORIZONTAL = [self::LEFT, self::RIGHT]; |
18
|
|
|
private const VERTICAL = [self::UP, self::DOWN]; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
private $direction; |
22
|
|
|
/** @var string */ |
23
|
|
|
private $crateId; |
24
|
|
|
|
25
|
|
|
public function __construct(string $direction, string $crateId) |
26
|
|
|
{ |
27
|
|
|
$this->direction = $direction; |
28
|
|
|
$this->crateId = $crateId; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function allFor(Crates $crates): Moves |
32
|
|
|
{ |
33
|
|
|
$moves = []; |
34
|
|
|
foreach ($crates as $crate) { |
35
|
|
|
$moves[] = Push::left($crate->id()); |
36
|
|
|
$moves[] = Push::right($crate->id()); |
37
|
|
|
$moves[] = Push::up($crate->id()); |
38
|
|
|
$moves[] = Push::down($crate->id()); |
39
|
|
|
} |
40
|
|
|
return new Moves(...$moves); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function left(string $rectangle): self |
44
|
|
|
{ |
45
|
|
|
return new self(self::LEFT, $rectangle); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function right(string $rectangle): self |
49
|
|
|
{ |
50
|
|
|
return new self(self::RIGHT, $rectangle); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function up(string $rectangle): self |
54
|
|
|
{ |
55
|
|
|
return new self(self::UP, $rectangle); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function down(string $rectangle): self |
59
|
|
|
{ |
60
|
|
|
return new self(self::DOWN, $rectangle); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function crateId(): string |
64
|
|
|
{ |
65
|
|
|
return $this->crateId; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function isHorizontal(): bool |
69
|
|
|
{ |
70
|
|
|
return in_array($this->direction, self::HORIZONTAL); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function isVertical(): bool |
74
|
|
|
{ |
75
|
|
|
return in_array($this->direction, self::VERTICAL); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function newX(int $oldX): int |
79
|
|
|
{ |
80
|
|
|
switch ($this->direction) { |
81
|
|
|
case self::RIGHT: return $oldX + 1; |
82
|
|
|
case self::LEFT: return $oldX - 1; |
83
|
|
|
default: return $oldX; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function newY(int $oldY): int |
88
|
|
|
{ |
89
|
|
|
switch ($this->direction) { |
90
|
|
|
case self::UP: return $oldY - 1; |
91
|
|
|
case self::DOWN: return $oldY + 1; |
92
|
|
|
default: return $oldY; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function __toString(): string |
97
|
|
|
{ |
98
|
|
|
return "Push {$this->crateId} {$this->direction}"; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function jsonSerialize(): array |
102
|
|
|
{ |
103
|
|
|
return [ |
104
|
|
|
'id' => $this->crateId, |
105
|
|
|
'direction' => $this->direction, |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|