1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Stratadox\PuzzleSolver\Puzzle\Maze; |
4
|
|
|
|
5
|
|
|
use Stratadox\PuzzleSolver\Move; |
6
|
|
|
use Stratadox\PuzzleSolver\Moves; |
7
|
|
|
|
8
|
|
|
final class Action implements Move |
9
|
|
|
{ |
10
|
|
|
private const LEFT = 'left'; |
11
|
|
|
private const RIGHT = 'right'; |
12
|
|
|
private const UP = 'up'; |
13
|
|
|
private const DOWN = 'down'; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
private $direction; |
17
|
|
|
|
18
|
|
|
private function __construct(string $direction) |
19
|
|
|
{ |
20
|
|
|
$this->direction = $direction; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public static function fromString(string $direction): self |
24
|
|
|
{ |
25
|
|
|
return new self($direction); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public static function all(): Moves |
29
|
|
|
{ |
30
|
|
|
return new Moves( |
31
|
|
|
self::fromString(self::LEFT), |
32
|
|
|
self::fromString(self::RIGHT), |
33
|
|
|
self::fromString(self::UP), |
34
|
|
|
self::fromString(self::DOWN) |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function newX(int $prevX): int |
39
|
|
|
{ |
40
|
|
|
switch ($this->direction) { |
41
|
|
|
case self::RIGHT: return $prevX + 1; |
42
|
|
|
case self::LEFT: return $prevX - 1; |
43
|
|
|
default: return $prevX; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function newY(int $prevY): int |
48
|
|
|
{ |
49
|
|
|
switch ($this->direction) { |
50
|
|
|
case self::UP: return $prevY - 1; |
51
|
|
|
case self::DOWN: return $prevY + 1; |
52
|
|
|
default: return $prevY; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function __toString(): string |
57
|
|
|
{ |
58
|
|
|
return 'move ' . $this->direction; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|