RiverCrossingPuzzle   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 47
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A afterMaking() 0 9 2
A movesSoFar() 0 3 1
A begin() 0 3 1
A representation() 0 3 1
A possibleMoves() 0 3 1
A __construct() 0 4 1
A isSolved() 0 3 1
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