RiverCrossingPuzzle::begin()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\PuzzleSolver\Puzzle\WolfGoatCabbage;
4
5
use Stratadox\PuzzleSolver\Move;
6
use Stratadox\PuzzleSolver\Puzzle;
7
use Stratadox\PuzzleSolver\Moves;
8
use function assert;
9
10
final class RiverCrossingPuzzle implements Puzzle
11
{
12
    /** @var Farmer */
13
    private $farmer;
14
    /** @var Moves */
15
    private $crossings;
16
17
    private function __construct(Farmer $farmer, Moves $crossings)
18
    {
19
        $this->farmer = $farmer;
20
        $this->crossings = $crossings;
21
    }
22
23
    public static function begin(): Puzzle
24
    {
25
        return new self(Farmer::withWolfGoatAndCabbage(), Moves::none());
26
    }
27
28
    public function isSolved(): bool
29
    {
30
        return $this->farmer->hasMovedAllPurchasesAlong();
31
    }
32
33
    public function movesSoFar(): Moves
34
    {
35
        return $this->crossings;
36
    }
37
38
    public function representation(): string
39
    {
40
        return (string) $this->farmer;
41
    }
42
43
    public function possibleMoves(): Moves
44
    {
45
        return $this->farmer->possibleCrossings();
46
    }
47
48
    public function afterMaking(Move ...$moves): Puzzle
49
    {
50
        $new = clone $this;
51
        foreach ($moves as $move) {
52
            assert($move instanceof Crossing);
53
            $new->farmer = $new->farmer->after($move);
54
        }
55
        $new->crossings = $new->crossings->add(...$moves);
56
        return $new;
57
    }
58
}
59